Quickly add genre component

This commit is contained in:
2017-10-11 00:33:43 +02:00
parent 331458edbd
commit 11c64405eb
10 changed files with 136 additions and 4 deletions

View File

@@ -93,6 +93,7 @@
<th>Album</th>
<th>Album Artist</th>
<th>Play Count</th>
<th>Genre</th>
</tr>
</thead>
<tbody>
@@ -103,6 +104,7 @@
<td [title]="song.Album"><a [routerLink]="['/album', song.Album]">{{song.Album}}</a></td>
<td [title]="song.Album">{{song['Album Artist']}}</td>
<td [title]="song['Play Count']">{{song['Play Count']}}</td>
<td><a [routerLink]="['/genre', song.Genre]">{{song.Genre}}</a></td>
</tr>
</tbody>
</table>

View File

@@ -4,12 +4,14 @@ import { RouterModule, Routes } from '@angular/router';
import { DashboardComponent } from './dashboard.component';
import { AlbumComponent } from './album/album.component';
import { ArtistComponent } from './artist/artist.component';
import { GenreComponent } from './genre/genre.component';
const routes: Routes = [
{ path: '', redirectTo: '/dashboard', pathMatch: 'full' },
{ path: 'dashboard', component: DashboardComponent },
{ path: 'album/:name', component: AlbumComponent },
{ path: 'artist/:name', component: ArtistComponent }
{ path: 'artist/:name', component: ArtistComponent },
{ path: 'genre/:name', component: GenreComponent }
];
@NgModule({

View File

@@ -6,6 +6,7 @@ import { AppComponent } from './app.component';
import { DashboardComponent } from './dashboard.component';
import { AlbumComponent } from './album/album.component';
import { ArtistComponent } from './artist/artist.component';
import { GenreComponent } from './genre/genre.component';
import { ElsService } from './els.service';
@@ -25,6 +26,7 @@ import { SortByPipe } from './sort-by.pipe';
DashboardComponent,
AlbumComponent,
ArtistComponent,
GenreComponent,
ConvertMsPipe,
SortByPipe
],

View File

@@ -95,6 +95,7 @@
<th>Artist</th>
<th>Album Artist</th>
<th>Play Count</th>
<th>Genre</th>
</tr>
</thead>
<tbody>
@@ -106,6 +107,7 @@
<td><a [routerLink]="['/artist', song.Artist]">{{song.Artist}}</a></td>
<td>{{song['Album Artist'] ? song['Album Artist'] : "-" }}</td>
<td>{{song['Play Count']}}</td>
<td><a [routerLink]="['/genre', song.Genre]">{{song.Genre}}</a></td>
</tr>
</tbody>
</table>

View File

@@ -127,7 +127,7 @@
</thead>
<tbody>
<tr *ngFor="let genre of topGenres">
<td>{{genre.key}}</td>
<td><a [routerLink]="['/genre', genre.key]">{{genre.key}}</a></td>
<td>{{genre.doc_count}}</td>
</tr>
</tbody>
@@ -144,10 +144,10 @@
</thead>
<tbody>
<tr *ngFor="let genre of bottomGenres">
<td>{{genre.key}}</td>
<td><a [routerLink]="['/genre', genre.key]">{{genre.key}}</a></td>
<td>{{genre.doc_count}}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>

View File

@@ -156,6 +156,30 @@ export class ElsService {
});
}
getGenreSongs(genreName: string, from: number = 0): Observable<Song[]> {
console.info('getGenreSongs- Genre name: ' + genreName + ' - from: ' + from);
// TODO Code repetition => to refactor
return this.http
.post(this.elsUrl + 'song' + ElsService.ACTION_SEARCH,
JSON.stringify({
'query': {
'match_phrase': { 'Genre': genreName }
},
'size': ElsService.DEFAULT_SIZE,
'from': from
}), {headers: this.headers})
.map(res => {
return res.json().hits.hits;
})
.map((hits: Array<any>) => {
const result: Array<Song> = [];
hits.forEach((hit) => {
result.push(hit._source);
});
return result;
});
}
getArtistSongs(artistName: string, from: number = 0): Observable<Song[]> {
console.info('getArtistSongs- Artist name: ' + artistName + ' - from: ' + from);
return this.http

View File

@@ -0,0 +1,28 @@
<div class="container">
<h1>{{genreName}}</h1>
<table class="table table-striped">
<thead>
<tr>
<th></th>
<th>Name</th>
<th>Artist</th>
<th>Album</th>
<th>Album Artist</th>
<th>Play Count</th>
<th>Genre</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let song of songs | sortBy : 'Track Number'">
<td>{{song['Track Number'] ? (("0" + song['Track Number']).slice(-2)) : "--"}}</td>
<td>{{song.Name}}</td>
<td><a [routerLink]="['/artist', song.Artist]">{{song.Artist}}</a></td>
<td><a [routerLink]="['/album', song.Album]">{{song.Album}}</a></td>
<td>{{song['Album Artist']}}</td>
<td>{{song['Play Count']}}</td>
<td>{{song.Genre}}</td>
</tr>
</tbody>
</table>
</div>

View File

@@ -0,0 +1,25 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { GenreComponent } from './genre.component';
describe('GenreComponent', () => {
let component: GenreComponent;
let fixture: ComponentFixture<GenreComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ GenreComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(GenreComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should be created', () => {
expect(component).toBeTruthy();
});
});

View File

@@ -0,0 +1,47 @@
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Params } from '@angular/router';
import { ElsService } from '../els.service';
import { Song } from '../object/song';
@Component({
selector: 'app-genre',
templateUrl: './genre.component.html',
styleUrls: ['./genre.component.css']
})
export class GenreComponent implements OnInit {
genreName = '';
songs: Array<Song> = [];
constructor(
private elsService: ElsService,
private route: ActivatedRoute
) { }
ngOnInit() {
this.route.params
.subscribe((params: Params) => this.genreName = params['name']);
this.loadSongs();
}
loadSongs(): any {
this.elsService.getGenreSongs(this.genreName, this.songs.length).subscribe(
data => {
// this.moreDataAvailable = data.length === ElsService.DEFAULT_SIZE;
// Erase song array with result for first load, then add elements one by one
// instead use concat => concat will sort table at each load, very consuming! and not user friendly
if (this.songs.length === 0) {
this.songs = data;
} else {
data.forEach(song => {
this.songs.push(song);
});
}
}
);
}
}