TOSQUASH Use a function to process response

This commit is contained in:
2017-10-29 02:47:49 +02:00
parent ab493346a5
commit 8fd9466fb1

View File

@@ -1,5 +1,5 @@
import { Injectable } from '@angular/core';
import { Headers, Http } from '@angular/http';
import { Headers, Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/toPromise';
@@ -111,13 +111,7 @@ export class ElsService {
} ],
'size': 5
}), {headers: this.headers})
.map(res => {
const result: Array<Song> = [];
res.json().hits.hits.forEach(element => {
result.push(element._source);
});
return result;
});
.map(res => this.responseToSongs(res));
}
getAlbumSongs(albumName: string, from: number = 0): Observable<Song[]> {
@@ -131,13 +125,7 @@ export class ElsService {
'size': ElsService.DEFAULT_SIZE,
'from': from
}), {headers: this.headers})
.map(res => {
const result: Array<Song> = [];
res.json().hits.hits.forEach((hit) => {
result.push(hit._source);
});
return result;
});
.map(res => this.responseToSongs(res));
}
getGenreSongs(genreName: string, from: number = 0): Observable<Song[]> {
@@ -152,13 +140,7 @@ export class ElsService {
'size': ElsService.DEFAULT_SIZE,
'from': from
}), {headers: this.headers})
.map(res => {
const result: Array<Song> = [];
res.json().hits.hits.forEach((hit) => {
result.push(hit._source);
});
return result;
});
.map(res => this.responseToSongs(res));
}
getArtistSongs(artistName: string, from: number = 0): Observable<Song[]> {
@@ -177,13 +159,7 @@ export class ElsService {
'size': ElsService.DEFAULT_SIZE,
'from': from
}), {headers: this.headers})
.map(res => {
const result: Array<Song> = [];
res.json().hits.hits.forEach((hit) => {
result.push(hit._source);
});
return result;
});
.map(res => this.responseToSongs(res));
}
getAlbum(albumName: string): Observable<Album> {
@@ -320,6 +296,14 @@ export class ElsService {
});
}
private responseToSongs(res: Response): Song[] {
const result: Array<Song> = [];
res.json().hits.hits.forEach((hit) => {
result.push(hit._source);
});
return result;
}
private hitsToBuckets(hits: Array<any>): Bucket[] {
const result: Array<Bucket> = [];
hits.forEach((bucket) => {