Most played song - good way to retrieve data
This commit is contained in:
@@ -30,27 +30,32 @@ export class ElsService {
|
||||
.catch(this.handleError);
|
||||
}
|
||||
|
||||
getTrackCountO(type: string): Observable<number> {
|
||||
return this.http.get(this.elsUrl + type + "/_count")
|
||||
.map(res => res.json().count as number);
|
||||
}
|
||||
getMostPlayedTrack(): Observable<Song[]> {
|
||||
// Thank to http://chariotsolutions.com/blog/post/angular2-observables-http-separating-services-components/
|
||||
|
||||
getMostPlayedTrack(): Promise<Hits[]> {
|
||||
return this.http.post(this.elsUrl + "song/_search",
|
||||
JSON.stringify({"sort":[{"Play Count":{"order":"desc"}}],"size": 5}),
|
||||
{headers: this.headers})
|
||||
.toPromise()
|
||||
.then(res => res.json().hits.hits)
|
||||
.catch(this.handleError);
|
||||
}
|
||||
|
||||
getMostPlayedTrackO(): Observable<Object[]> {
|
||||
// Could be shorter but I think it's more readable like this.
|
||||
return this.http
|
||||
.post(this.elsUrl + "song/_search",
|
||||
JSON.stringify({"sort":[{"Play Count":{"order":"desc"}}],"size": 5}),
|
||||
{headers: this.headers})
|
||||
.map(response => response.json().hits.hits as Object[]);
|
||||
// TODO Treat data here to return song instead of in subscribe
|
||||
.post(this.elsUrl + "song/_search",
|
||||
JSON.stringify({"sort":[{"Play Count":{"order":"desc"}}],"size": 5}),
|
||||
{headers: this.headers})
|
||||
.map(res => {
|
||||
return res.json().hits.hits;
|
||||
})
|
||||
.map((hits: Array<any>) => {
|
||||
let result:Array<Song> = [];
|
||||
hits.forEach((hit) => {
|
||||
result.push(hit._source);
|
||||
});
|
||||
return result;
|
||||
});
|
||||
// Shorter way:
|
||||
// .map(res => {
|
||||
// let result: Array<Song> = [];
|
||||
// res.json().hits.hits.forEach(element => {
|
||||
// result.push(element._source);
|
||||
// });
|
||||
// return result;
|
||||
// });
|
||||
}
|
||||
|
||||
private handleError(error: any): Promise<any> {
|
||||
|
||||
Reference in New Issue
Block a user