Continue to refactor service
This commit is contained in:
@@ -171,20 +171,7 @@ export class ElsService {
|
|||||||
},
|
},
|
||||||
'size': ElsService.DEFAULT_SIZE
|
'size': ElsService.DEFAULT_SIZE
|
||||||
}), {headers: this.headers})
|
}), {headers: this.headers})
|
||||||
.map(res => this.responseToAlbum(res, albumName));
|
.map(res => this.responseToOneTypedResult<Album>(res, albumName));
|
||||||
}
|
|
||||||
|
|
||||||
private responseToAlbum(res: Response, name: string): Album {
|
|
||||||
const hits = res.json().hits.hits;
|
|
||||||
if (hits.length < 1) {
|
|
||||||
console.info('No album "' + name + '" found.');
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
if (hits.length > 1) {
|
|
||||||
// TODO Cumul results
|
|
||||||
console.error('More than one album "' + name + '" found (' + hits.length + '), return the first.');
|
|
||||||
}
|
|
||||||
return hits[0]._source;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getArtist(artistName: string): Observable<Artist> {
|
getArtist(artistName: string): Observable<Artist> {
|
||||||
@@ -196,20 +183,7 @@ export class ElsService {
|
|||||||
},
|
},
|
||||||
'size': ElsService.DEFAULT_SIZE
|
'size': ElsService.DEFAULT_SIZE
|
||||||
}), {headers: this.headers})
|
}), {headers: this.headers})
|
||||||
.map(res => res.json().hits.hits)
|
.map(res => this.responseToOneTypedResult<Artist>(res, artistName));
|
||||||
.map((hits: Array<any>) => {
|
|
||||||
// Theorically, my script prevent to found two documents with this query.
|
|
||||||
// But Prevention is better than cure as Shakespeare said
|
|
||||||
if (hits.length < 1) {
|
|
||||||
console.info('No artist "' + artistName + '" found.');
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
if (hits.length > 1) {
|
|
||||||
console.error('More than one artist "' + artistName + '" found (' + hits.length + '), return the first.');
|
|
||||||
console.error('This is not normal!');
|
|
||||||
}
|
|
||||||
return hits[0]._source;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getGenres(ordering: string = 'desc'): Observable<Bucket[]> {
|
getGenres(ordering: string = 'desc'): Observable<Bucket[]> {
|
||||||
@@ -297,6 +271,31 @@ export class ElsService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Process a result to return just one result.
|
||||||
|
* Used to get an album or an artist.
|
||||||
|
* Take a name to put in console output if no result or more than one result.
|
||||||
|
*
|
||||||
|
* @param res Response to process
|
||||||
|
* @param name The searched name - for console output
|
||||||
|
*/
|
||||||
|
private responseToOneTypedResult<T>(res: Response, name: string): T {
|
||||||
|
const hits = res.json().hits.hits;
|
||||||
|
|
||||||
|
if (hits.length < 1) {
|
||||||
|
console.info('No result found for name: "' + name);
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
if (hits.length > 1) {
|
||||||
|
// TODO Cumul results (for album)
|
||||||
|
console.error('More than one result for name: "' + name + '". Found (' + hits.length + '), return the first.');
|
||||||
|
}
|
||||||
|
return hits[0]._source;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Process a response to a array of songs.
|
||||||
|
*
|
||||||
|
* @param res Response to process
|
||||||
|
*/
|
||||||
private responseToSongs(res: Response): Song[] {
|
private responseToSongs(res: Response): Song[] {
|
||||||
const result: Array<Song> = [];
|
const result: Array<Song> = [];
|
||||||
res.json().hits.hits.forEach((hit) => {
|
res.json().hits.hits.forEach((hit) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user