karusic/front/src/app/service/playlist.ts

130 lines
3.0 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 { Playlist, PlaylistResource, UUID } from 'app/back-api';
import { RESTConfig } from 'app/back-api/rest-tools';
import { DataStore } from 'common/utils/data-store';
import { environment } from 'environments/environment';
import { GenericDataService } from './GenericDataService';
@Injectable()
export class PlaylistService extends GenericDataService<Playlist> {
getRestConfig(): RESTConfig {
return {
server: environment.server.karusic,
token: this.session.getToken()
}
}
private lambdaGets(): Promise<Playlist[]> {
return PlaylistResource.gets({ restConfig: this.getRestConfig() });
}
constructor(private session: SessionService) {
super();
const self = this;
this.setStore(new DataStore<Playlist>(() => this.lambdaGets()));
}
getSubArtist(id: bigint, select: Array<string> = []): any {
// this.checkLocalBdd();
}
getSubTrack(id: bigint, select: Array<string> = []): any {
// this.checkLocalBdd();
} insert(data: Playlist): Promise<Playlist> {
const self = this;
return new Promise((resolve, reject) => {
PlaylistResource.post({
restConfig: this.getRestConfig(),
data
}).then((value: Playlist) => {
self.dataStore.updateValue(value);
resolve(value);
}).catch((error) => {
reject(error);
});
});
}
patch(id: number, data: Playlist): Promise<Playlist> {
const self = this;
return new Promise((resolve, reject) => {
PlaylistResource.patch({
restConfig: this.getRestConfig(),
params: {
id
},
data
}).then((value: Playlist) => {
self.dataStore.updateValue(value);
resolve(value);
}).catch((error) => {
reject(error);
});
});
}
delete(id: number): Promise<void> {
const self = this;
return new Promise((resolve, reject) => {
PlaylistResource.remove({
restConfig: this.getRestConfig(),
params: {
id
}
}).then(() => {
self.dataStore.delete(id);
resolve();
}).catch((error) => {
reject(error);
});
});
}
deleteCover(id: number, coverId: UUID): Promise<Playlist> {
let self = this;
return new Promise((resolve, reject) => {
PlaylistResource.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<Playlist> {
let self = this;
return new Promise((resolve, reject) => {
PlaylistResource.uploadCover({
restConfig: this.getRestConfig(),
params: {
id,
},
data: {
file,
fileName: file.name
}
}).then((value) => {
self.dataStore.updateValue(value);
resolve(value);
}).catch((error) => {
reject(error);
});
});
}
}