160 lines
3.7 KiB
TypeScript
160 lines
3.7 KiB
TypeScript
/** @file
|
|
* @author Edouard DUPIN
|
|
* @copyright 2018, Edouard DUPIN, all right reserved
|
|
* @license PROPRIETARY (see license file)
|
|
*/
|
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { SessionService } from 'common/service';
|
|
import { DataInterface, TypeCheck, isArrayOf, isNumber } from 'common/utils';
|
|
import { Album, AlbumResource, UUID } from 'app/back-api';
|
|
import { DataStore } from '../../common/utils/data-store';
|
|
import { RESTConfig } from 'app/back-api/rest-tools';
|
|
import { environment } from 'environments/environment';
|
|
import { DataTools } from 'common/utils/data-tools';
|
|
import { GenericDataService } from './GenericDataService';
|
|
|
|
@Injectable()
|
|
export class AlbumService extends GenericDataService<Album> {
|
|
|
|
getRestConfig(): RESTConfig {
|
|
return {
|
|
server: environment.server.karusic,
|
|
token: this.session.getToken()
|
|
}
|
|
}
|
|
private lambdaGets(): Promise<Album[]> {
|
|
const self = this;
|
|
return AlbumResource.gets({ restConfig: this.getRestConfig() });
|
|
}
|
|
|
|
constructor(private session: SessionService) {
|
|
super();
|
|
this.setStore(new DataStore<Album>(() => this.lambdaGets()));
|
|
}
|
|
|
|
insert(data: Album): Promise<Album> {
|
|
const self = this;
|
|
return new Promise((resolve, reject) => {
|
|
AlbumResource.post({
|
|
restConfig: this.getRestConfig(),
|
|
data
|
|
}).then((value: Album) => {
|
|
self.dataStore.updateValue(value);
|
|
resolve(value);
|
|
}).catch((error) => {
|
|
reject(error);
|
|
});
|
|
});
|
|
}
|
|
patch(id: number, data: Album): Promise<Album> {
|
|
const self = this;
|
|
return new Promise((resolve, reject) => {
|
|
AlbumResource.patch({
|
|
restConfig: this.getRestConfig(),
|
|
params: {
|
|
id
|
|
},
|
|
data
|
|
}).then((value: Album) => {
|
|
self.dataStore.updateValue(value);
|
|
resolve(value);
|
|
}).catch((error) => {
|
|
reject(error);
|
|
});
|
|
});
|
|
}
|
|
|
|
delete(id: number): Promise<void> {
|
|
const self = this;
|
|
return new Promise((resolve, reject) => {
|
|
AlbumResource.remove({
|
|
restConfig: this.getRestConfig(),
|
|
params: {
|
|
id
|
|
}
|
|
}).then(() => {
|
|
self.dataStore.delete(id);
|
|
resolve();
|
|
}).catch((error) => {
|
|
reject(error);
|
|
});
|
|
});
|
|
}
|
|
|
|
deleteCover(id: number, coverId: UUID): Promise<Album> {
|
|
let self = this;
|
|
return new Promise((resolve, reject) => {
|
|
AlbumResource.removeCover({
|
|
restConfig: this.getRestConfig(),
|
|
params: {
|
|
id,
|
|
coverId
|
|
}
|
|
}).then((value) => {
|
|
self.dataStore.updateValue(value);
|
|
resolve(value);
|
|
}).catch((error) => {
|
|
reject(error);
|
|
});
|
|
});
|
|
}
|
|
uploadCover(id: number,
|
|
file: File,
|
|
progress: any = null): Promise<Album> {
|
|
let self = this;
|
|
return new Promise((resolve, reject) => {
|
|
AlbumResource.uploadCover({
|
|
restConfig: this.getRestConfig(),
|
|
params: {
|
|
id,
|
|
},
|
|
data: {
|
|
file,
|
|
fileName: file.name
|
|
}
|
|
}).then((value) => {
|
|
self.dataStore.updateValue(value);
|
|
resolve(value);
|
|
}).catch((error) => {
|
|
reject(error);
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Get all the artists for a specific album
|
|
* @param idAlbum - Id of the album.
|
|
* @returns a promise on the list of Artist ID for this album
|
|
*/
|
|
getArtists(idAlbum: number): Promise<number[]> {
|
|
let self = this;
|
|
return new Promise((resolve, reject) => {
|
|
self.gets()
|
|
.then((response: Album[]) => {
|
|
let data = DataTools.getsWhere(response,
|
|
[
|
|
{
|
|
check: TypeCheck.EQUAL,
|
|
key: 'albumId',
|
|
value: idAlbum,
|
|
},
|
|
],
|
|
['name']);
|
|
// filter with artist- ID !!!
|
|
const listArtistId = DataInterface.extractLimitOneList(data, "artists");
|
|
if (isArrayOf(listArtistId, isNumber)) {
|
|
resolve(listArtistId);
|
|
} else {
|
|
reject(`Fail to get the ids (impossible case) ${listArtistId}`);
|
|
}
|
|
}).catch((response) => {
|
|
reject(response);
|
|
});
|
|
});
|
|
}
|
|
|
|
}
|
|
|