182 lines
4.7 KiB
TypeScript
182 lines
4.7 KiB
TypeScript
import { isNodeData, NodeData } from "common/model";
|
|
import { HttpWrapperService, BddService } from "common/service";
|
|
import { DataInterface, isArrayOf, isNullOrUndefined, TypeCheck } from "common/utils";
|
|
|
|
|
|
export class GenericInterfaceModelDB {
|
|
constructor(
|
|
protected serviceName:string,
|
|
protected http: HttpWrapperService,
|
|
protected bdd: BddService) {
|
|
// nothing to do ...
|
|
}
|
|
|
|
|
|
gets(): Promise<NodeData[]> {
|
|
let self = this;
|
|
return new Promise((resolve, reject) => {
|
|
self.bdd.get(self.serviceName)
|
|
.then((response: DataInterface) => {
|
|
let data = response.gets();
|
|
if(isNullOrUndefined(data)) {
|
|
reject('Data does not exist in the local BDD');
|
|
return;
|
|
}
|
|
resolve(data);
|
|
return;
|
|
}).catch((response) => {
|
|
reject(response);
|
|
});
|
|
});
|
|
}
|
|
|
|
getLike(nameArtist:string):any {
|
|
let self = this;
|
|
return new Promise((resolve, reject) => {
|
|
self.bdd.get(self.serviceName)
|
|
.then((response:DataInterface) => {
|
|
let data = response.getNameLike(nameArtist);
|
|
if(data === null || data === undefined || data.length === 0) {
|
|
reject('Data does not exist in the local BDD');
|
|
return;
|
|
}
|
|
resolve(data);
|
|
return;
|
|
}).catch((response) => {
|
|
reject(response);
|
|
});
|
|
});
|
|
}
|
|
getOrder(): Promise<NodeData[]> {
|
|
let self = this;
|
|
return new Promise((resolve, reject) => {
|
|
self.bdd.get(self.serviceName)
|
|
.then((response: DataInterface) => {
|
|
let data = response.getsWhere([
|
|
{
|
|
check: TypeCheck.NOT_EQUAL,
|
|
key: 'id',
|
|
value: [undefined, null],
|
|
},
|
|
],
|
|
[ 'name', 'id' ]);
|
|
//data = response.gets();
|
|
if (isArrayOf(data, isNodeData)) {
|
|
resolve(data);
|
|
}
|
|
reject("The model is wrong ...");
|
|
}).catch((response) => {
|
|
console.log(`[E] ${ self.constructor.name }: can not retrive BDD values`);
|
|
reject(response);
|
|
});
|
|
});
|
|
}
|
|
get(id:number): Promise<NodeData> {
|
|
let self = this;
|
|
return new Promise((resolve, reject) => {
|
|
self.bdd.get(self.serviceName)
|
|
.then((response: DataInterface) => {
|
|
let data = response.get(id);
|
|
if(isNullOrUndefined(data)) {
|
|
reject('Data does not exist in the local BDD');
|
|
return;
|
|
}
|
|
resolve(data);
|
|
return;
|
|
}).catch((response) => {
|
|
reject(response);
|
|
});
|
|
});
|
|
}
|
|
getAll(ids: number[]): Promise<NodeData[]> {
|
|
let self = this;
|
|
return new Promise((resolve, reject) => {
|
|
self.bdd.get(self.serviceName)
|
|
.then((response: DataInterface) => {
|
|
let data = response.getsWhere([
|
|
{
|
|
check: TypeCheck.EQUAL,
|
|
key: 'id',
|
|
value: ids,
|
|
},
|
|
],
|
|
[ 'name', 'id' ]);
|
|
resolve(data);
|
|
return;
|
|
}).catch((response) => {
|
|
reject(response);
|
|
});
|
|
});
|
|
}
|
|
|
|
getData(): Promise<NodeData[]> {
|
|
let self = this;
|
|
return new Promise((resolve, reject) => {
|
|
self.bdd.get(self.serviceName)
|
|
.then((response:DataInterface) => {
|
|
let data = response.gets();
|
|
resolve(data);
|
|
}).catch((response) => {
|
|
console.log(`[E] ${ self.constructor.name }: can not retrive BDD values`);
|
|
reject(response);
|
|
});
|
|
});
|
|
}
|
|
|
|
insert(data: any): any {
|
|
let ret = this.http.postSpecific([this.serviceName], data);
|
|
return this.bdd.addAfterPost(this.serviceName, ret);
|
|
|
|
}
|
|
put(id:number, data:any):any {
|
|
let ret = this.http.putSpecific([this.serviceName, id], data);
|
|
return this.bdd.setAfterPut(this.serviceName, id, ret);
|
|
}
|
|
|
|
delete(id:number):any {
|
|
let ret = this.http.deleteSpecific([this.serviceName, id]);
|
|
return this.bdd.delete(this.serviceName, id, ret);
|
|
}
|
|
|
|
deleteCover(nodeId:number, coverId:number) {
|
|
let self = this;
|
|
return new Promise((resolve, reject) => {
|
|
self.http.getSpecific([this.serviceName, nodeId, 'rm_cover', coverId])
|
|
.then((response) => {
|
|
let data = response;
|
|
if(data === null || data === undefined) {
|
|
reject('error retrive data from server');
|
|
return;
|
|
}
|
|
self.bdd.asyncSetInDB(self.serviceName, nodeId, data);
|
|
resolve(data);
|
|
}).catch((response) => {
|
|
reject(response);
|
|
});
|
|
});
|
|
}
|
|
uploadCover(file:File,
|
|
nodeId:number,
|
|
progress:any = null) {
|
|
const formData = new FormData();
|
|
formData.append('fileName', file.name);
|
|
formData.append('id', nodeId.toString());
|
|
formData.append('file', file);
|
|
let self = this;
|
|
return new Promise((resolve, reject) => {
|
|
self.http.uploadMultipart(`${this.serviceName }/${nodeId}/add_cover/`, formData, progress)
|
|
.then((response) => {
|
|
let data = response;
|
|
if(data === null || data === undefined) {
|
|
reject('error retrive data from server');
|
|
return;
|
|
}
|
|
self.bdd.asyncSetInDB(self.serviceName, nodeId, data);
|
|
resolve(data);
|
|
}).catch((response) => {
|
|
reject(response);
|
|
});
|
|
});
|
|
}
|
|
|
|
} |