Files
iTunes/dashboard/src/app/els-service/els-artist.service.ts
Maxence G. de Montauzan 25828bb789 (front) Service refactoring for sorting operations
- Create an ElsArtistService to use common query
- Move services in a subfolder

Use a function to add sort filter
2023-02-06 23:08:56 +01:00

122 lines
3.0 KiB
TypeScript

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<Artist> {
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<Artist>(res, artistName)),
catchError((error) =>
this.handleError(error, "getArtist(" + artistName + ")")
)
);
}
public getArtistSongs(
artistName: string,
from: number = 0,
sortFilter = false
): Observable<Song[]> {
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<number> {
console.log("artistname: " + artistName);
const query = this.getQuerySongsWithArtistName(artistName, sortFilter);
return this.http
.post<any>(
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 + ")")
)
);
}
}