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