125 lines
2.8 KiB
TypeScript

/** @file
* @author Edouard DUPIN
* @copyright 2018, Edouard DUPIN, all right reserved
* @license PROPRIETARY (see license file)
*/
import { Injectable } from '@angular/core';
import { Artist, ArtistResource, UUID } from 'app/back-api';
import { RESTConfig } from 'app/back-api/rest-tools';
import { environment } from 'environments/environment';
import { GenericDataService } from './GenericDataService';
import { DataStore, SessionService } from '@kangaroo-and-rabbit/kar-cw';
@Injectable()
export class ArtistService extends GenericDataService<Artist> {
getRestConfig(): RESTConfig {
return {
server: environment.server.karusic,
token: this.session.getToken()
}
}
private lambdaGets(): Promise<Artist[]> {
return ArtistResource.gets({ restConfig: this.getRestConfig() });
}
constructor(private session: SessionService) {
super();
console.log('Start ArtistService');
const self = this;
this.setStore(new DataStore<Artist>(() => self.lambdaGets()));
}
insert(data: Artist): Promise<Artist> {
const self = this;
return new Promise((resolve, reject) => {
ArtistResource.post({
restConfig: this.getRestConfig(),
data
}).then((value: Artist) => {
self.dataStore.updateValue(value);
resolve(value);
}).catch((error) => {
reject(error);
});
});
}
patch(id: number, data: Artist): Promise<Artist> {
const self = this;
return new Promise((resolve, reject) => {
ArtistResource.patch({
restConfig: this.getRestConfig(),
params: {
id
},
data
}).then((value: Artist) => {
self.dataStore.updateValue(value);
resolve(value);
}).catch((error) => {
reject(error);
});
});
}
delete(id: number): Promise<void> {
const self = this;
return new Promise((resolve, reject) => {
ArtistResource.remove({
restConfig: this.getRestConfig(),
params: {
id
}
}).then(() => {
self.dataStore.delete(id);
resolve();
}).catch((error) => {
reject(error);
});
});
}
deleteCover(id: number, coverId: UUID): Promise<Artist> {
let self = this;
return new Promise((resolve, reject) => {
ArtistResource.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<Artist> {
let self = this;
return new Promise((resolve, reject) => {
ArtistResource.uploadCover({
restConfig: this.getRestConfig(),
params: {
id,
},
data: {
file,
fileName: file.name
}
}).then((value) => {
self.dataStore.updateValue(value);
resolve(value);
}).catch((error) => {
reject(error);
});
});
}
}