diff --git a/dashboard/src/app/els.service.ts b/dashboard/src/app/els.service.ts index 7a3e23f..38a8032 100644 --- a/dashboard/src/app/els.service.ts +++ b/dashboard/src/app/els.service.ts @@ -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 = []; - res.json().hits.hits.forEach(element => { - result.push(element._source); - }); - return result; - }); + .map(res => this.responseToSongs(res)); } getAlbumSongs(albumName: string, from: number = 0): Observable { @@ -131,13 +125,7 @@ export class ElsService { 'size': ElsService.DEFAULT_SIZE, 'from': from }), {headers: this.headers}) - .map(res => { - const result: Array = []; - res.json().hits.hits.forEach((hit) => { - result.push(hit._source); - }); - return result; - }); + .map(res => this.responseToSongs(res)); } getGenreSongs(genreName: string, from: number = 0): Observable { @@ -152,13 +140,7 @@ export class ElsService { 'size': ElsService.DEFAULT_SIZE, 'from': from }), {headers: this.headers}) - .map(res => { - const result: Array = []; - res.json().hits.hits.forEach((hit) => { - result.push(hit._source); - }); - return result; - }); + .map(res => this.responseToSongs(res)); } getArtistSongs(artistName: string, from: number = 0): Observable { @@ -177,13 +159,7 @@ export class ElsService { 'size': ElsService.DEFAULT_SIZE, 'from': from }), {headers: this.headers}) - .map(res => { - const result: Array = []; - res.json().hits.hits.forEach((hit) => { - result.push(hit._source); - }); - return result; - }); + .map(res => this.responseToSongs(res)); } getAlbum(albumName: string): Observable { @@ -320,6 +296,14 @@ export class ElsService { }); } + private responseToSongs(res: Response): Song[] { + const result: Array = []; + res.json().hits.hits.forEach((hit) => { + result.push(hit._source); + }); + return result; + } + private hitsToBuckets(hits: Array): Bucket[] { const result: Array = []; hits.forEach((bucket) => {