import { HttpClient } from "@angular/common/http"; import { Injectable } from "@angular/core"; import { Observable } from "rxjs"; import { catchError, map } from "rxjs/operators"; import { Artist } from "../model/artist"; import { Song } from "../model/song"; import { ElsService } from "./els.service"; @Injectable() export class ElsArtistService extends ElsService { constructor(protected http: HttpClient) { super(http); } private getQuerySongsWithArtistName( artistName: string, sortFilter: boolean = false, size: number = 0, from: number = 0 ) { let query = { query: { bool: { should: [ { match_phrase: { "Album Artist": artistName } }, { match_phrase: { Artist: artistName } }, ], must_not: [], }, }, }; if (sortFilter) { console.log("ElsArtistService- TO SORT filter enabled"); query = this.addSortFilterToQuery(query); } if (size) { query["size"] = size; } if (from) { query["from"] = from; } return query; } public getArtist(artistName: string): Observable { return this.http .post( this.elsUrl + ElsService.ARTIST_INDEX_NAME + ElsService.ACTION_SEARCH, JSON.stringify({ query: { match_phrase: { Artist: artistName }, }, size: ElsService.DEFAULT_SIZE, }), { headers: this.headers } ) .pipe( map((res) => this.responseToOneTypedResult(res, artistName)), catchError((error) => this.handleError(error, "getArtist(" + artistName + ")") ) ); } public getArtistSongs( artistName: string, from: number = 0, sortFilter = false ): Observable { console.info( "getArtistSongs- Artist name: " + artistName + " - from: " + from ); let query = this.getQuerySongsWithArtistName( artistName, sortFilter, ElsService.DEFAULT_SIZE, from ); return this.http .post( this.elsUrl + ElsService.SONG_INDEX_NAME + ElsService.ACTION_SEARCH, JSON.stringify(query), { headers: this.headers } ) .pipe( map((res) => this.responseToSongs(res)), catchError((error) => this.handleError( error, "getArtistSongs(" + artistName + "," + from + ")" ) ) ); } public getCountArtistSong( artistName: string, sortFilter = false ): Observable { console.log("artistname: " + artistName); const query = this.getQuerySongsWithArtistName(artistName, sortFilter); return this.http .post( this.elsUrl + ElsService.SONG_INDEX_NAME + ElsService.ACTION_COUNT, JSON.stringify(query), { headers: this.headers } ) .pipe( map((res) => res.count as number), catchError((error) => this.handleError(error, "getCountArtistSong" + artistName + ")") ) ); } }