From ecc216d15d91539a26ac8aecde9eff83a02d64fe Mon Sep 17 00:00:00 2001 From: "Maxence G. de Montauzan" Date: Thu, 4 May 2017 04:45:44 +0200 Subject: [PATCH] Start to show album --- dashboard/requestAlbum.txt | 44 ++++++++++++++++++++++ dashboard/src/app/album.component.html | 9 +++++ dashboard/src/app/album.component.ts | 36 ++++++++++++++++++ dashboard/src/app/app-routing.module.ts | 4 +- dashboard/src/app/app.module.ts | 7 +++- dashboard/src/app/dashboard.component.html | 2 +- dashboard/src/app/els.service.ts | 17 +++++++++ dashboard/src/app/object/song.ts | 2 + dashboard/src/app/sortby.pipe.ts | 35 +++++++++++++++++ 9 files changed, 153 insertions(+), 3 deletions(-) create mode 100644 dashboard/requestAlbum.txt create mode 100644 dashboard/src/app/album.component.html create mode 100644 dashboard/src/app/album.component.ts create mode 100644 dashboard/src/app/sortby.pipe.ts diff --git a/dashboard/requestAlbum.txt b/dashboard/requestAlbum.txt new file mode 100644 index 0000000..89d4185 --- /dev/null +++ b/dashboard/requestAlbum.txt @@ -0,0 +1,44 @@ +POST http://localhost:9200/itunessongs/_search +content-type: application/json +User-Agent: vscode-restclient + +{ + "query": { + "match" : { + "Album" : { + "query": "Our Love To Admire", + "operator" : "and" + } + } + }, + "size": 20 +} + +POST http://localhost:9200/itunessongs/_search +content-type: application/json +User-Agent: vscode-restclient + +{ + "query": { + "match_phrase" : { + "Album" : "Our Love" + } + }, + "size": 20 +} + +POST http://localhost:9200/itunessongs/_search +content-type: application/json +User-Agent: vscode-restclient + +{ + "query": { + "bool": { + "must": [ + {"match_phrase" : { "Album" : "Our Love" }}, + {"match_phrase" : { "Artist" : "Caribou." }} + ] + } + }, + "size": 20 +} \ No newline at end of file diff --git a/dashboard/src/app/album.component.html b/dashboard/src/app/album.component.html new file mode 100644 index 0000000..5807fa9 --- /dev/null +++ b/dashboard/src/app/album.component.html @@ -0,0 +1,9 @@ +

Test

+ +

{{albumName}}

+ + \ No newline at end of file diff --git a/dashboard/src/app/album.component.ts b/dashboard/src/app/album.component.ts new file mode 100644 index 0000000..24162e8 --- /dev/null +++ b/dashboard/src/app/album.component.ts @@ -0,0 +1,36 @@ +import { Component, OnInit } from '@angular/core'; +import { ActivatedRoute, Params } from '@angular/router' +import { Location } from '@angular/common' + +import { ElsService } from './els.service' +import { Song } from './object/song'; + +@Component({ + selector: 'album-component', + templateUrl: './album.component.html' +// styleUrls: [ './album.component.css' ] +}) + +export class AlbumComponent implements OnInit { + constructor( + private elsService: ElsService, + private route: ActivatedRoute, + private location: Location + ) { } + + albumName = ""; + + songs: Array = []; + + ngOnInit(): void { + this.route.params + .subscribe((params: Params) => this.albumName = params['name']); + + this.elsService.getAlbumSongs(this.albumName).subscribe( + data => { + console.log(data); + this.songs = data; + } + ); + } +} \ No newline at end of file diff --git a/dashboard/src/app/app-routing.module.ts b/dashboard/src/app/app-routing.module.ts index 73f447f..72804be 100644 --- a/dashboard/src/app/app-routing.module.ts +++ b/dashboard/src/app/app-routing.module.ts @@ -2,6 +2,7 @@ import { NgModule } from '@angular/core'; import { RouterModule, Routes } from '@angular/router'; import { DashboardComponent } from './dashboard.component'; +import { AlbumComponent } from './album.component'; import { HeroesComponent } from './heroes.component'; import { HeroDetailComponent } from './hero-detail.component'; @@ -9,7 +10,8 @@ const routes: Routes = [ { path: '', redirectTo: '/dashboard', pathMatch: 'full' }, { path: 'dashboard', component: DashboardComponent }, { path: 'detail/:id', component: HeroDetailComponent }, - { path: 'heroes', component: HeroesComponent } + { path: 'heroes', component: HeroesComponent }, + { path: 'album/:name', component: AlbumComponent } ]; @NgModule({ diff --git a/dashboard/src/app/app.module.ts b/dashboard/src/app/app.module.ts index 0065399..89112ca 100644 --- a/dashboard/src/app/app.module.ts +++ b/dashboard/src/app/app.module.ts @@ -11,12 +11,15 @@ import { AppComponent } from './app.component'; import { HeroDetailComponent } from './hero-detail.component'; import { HeroesComponent } from './heroes.component'; import { DashboardComponent } from './dashboard.component' +import { AlbumComponent } from './album.component' import { HeroService } from './hero.service'; import { ElsService } from './els.service'; import { AppRoutingModule } from './app-routing.module' +import { SortPipe } from './sortby.pipe' + @NgModule({ imports: [ BrowserModule, @@ -29,7 +32,9 @@ import { AppRoutingModule } from './app-routing.module' AppComponent, HeroDetailComponent, HeroesComponent, - DashboardComponent + DashboardComponent, + AlbumComponent, + SortPipe ], providers: [ HeroService, diff --git a/dashboard/src/app/dashboard.component.html b/dashboard/src/app/dashboard.component.html index 98e1ab1..03798af 100644 --- a/dashboard/src/app/dashboard.component.html +++ b/dashboard/src/app/dashboard.component.html @@ -16,5 +16,5 @@
-

{{e['Play Count']}}: {{e.Name}} - {{e.Artist}}

+

{{e['Play Count']}}: {{e.Name}} - {{e.Artist}} - {{e.Album}}

\ No newline at end of file diff --git a/dashboard/src/app/els.service.ts b/dashboard/src/app/els.service.ts index 85d6a2d..0eb2a05 100644 --- a/dashboard/src/app/els.service.ts +++ b/dashboard/src/app/els.service.ts @@ -64,6 +64,23 @@ export class ElsService { // }); } + getAlbumSongs(albumName: string): Observable { + return this.http + .post(this.elsUrl + "song/_search", + JSON.stringify({"query":{"match_phrase":{"Album":albumName}},"size": 20}), // TODO Dynamic size + {headers: this.headers}) + .map(res => { + return res.json().hits.hits; + }) + .map((hits: Array) => { + let result:Array = []; + hits.forEach((hit) => { + result.push(hit._source); + }); + return result; + }); + } + private handleError(error: any): Promise { console.error('An error occurred', error); // for demo purposes only return Promise.reject(error.message || error); diff --git a/dashboard/src/app/object/song.ts b/dashboard/src/app/object/song.ts index a3c6ac1..0f98ba1 100644 --- a/dashboard/src/app/object/song.ts +++ b/dashboard/src/app/object/song.ts @@ -2,4 +2,6 @@ export class Song { Name: string; Artist: string; "Play Count": number; + Album: string; + "Track Number": number; // TODO Default property } \ No newline at end of file diff --git a/dashboard/src/app/sortby.pipe.ts b/dashboard/src/app/sortby.pipe.ts new file mode 100644 index 0000000..7ff497d --- /dev/null +++ b/dashboard/src/app/sortby.pipe.ts @@ -0,0 +1,35 @@ +import { Pipe, PipeTransform } from '@angular/core'; + +@Pipe({name: "sortBy"}) +export class SortPipe implements PipeTransform { + transform(array: Array, args: string): Array { + array.sort((a: any, b: any) => { + console.log("a: " + a[args]); + console.log("b: " + b[args]); + if (a[args] == undefined && b[args] == undefined) { + console.log("und: " + 0) + return 0; + } + if (a[args] == undefined && b[args] != undefined) { + console.log("und: " + "-1") + return -1; + } + if (a[args] != undefined && b[args] == undefined) { + console.log("und: " + "-1") + return 1; + } + + if ( a[args] < b[args] ){ + console.log(-1); + return -1; + }else if( a[args] > b[args] ){ + console.log(1); + return 1; + }else{ + console.log(0); + return 0; + } + }); + return array; + } +} \ No newline at end of file