/**
* @namespace Services
* @memberOf Myintranet
*/
/**
* @memberof Myintranet.Services
*/
class CrudService {
constructor(Model) {
// if (!Model) {
// throw new Error("Main model is reauired, cant create a crudService without a data model");
// }
this.model = Model;
}
// capitalize(str) {
// let string = str.split(" ");
// for (let i = 0; i < string.length; i++) {
// string[i] = string[i].charAt(0).toUpperCase() + string[i].substring(1).toLowerCase();
// }
// return string.join(" ");
// }
/**
*
*/
getAll(request) {
return this.model.findAll( {});
}
__getIdFromRequest(request){
let id = null;
if (request.params && request.params.id) {
id = request.params.id;
} else if (request.query && request.query.id) {
id = request.query.id;
} else if (request.body && request.body.id) {
id = request.body.id;
}
if (id==null) {
throw new Error("cant update with no id")
}
return parseInt(id);
}
/**
*
* @param {*} request
*/
getOne(request) {
let id =this.__getIdFromRequest(request);
return this.model.findOne({ where: { id } });
}
/**
*
* @param {object} where
*/
getBy(where) {
return new Promise((resolve, reject) => {
});
}
/**
*
* @param {*} request
*/
update(request) {
let id = this.__getIdFromRequest(request);
let data = request.body;
return this.model.update(data, { where: { id } });
}
/**
*
* @param {*} request
*/
delete(request) {
let id = this.__getIdFromRequest(request);
return this.model.destroy({ where: { id } });
}
/**
*
* @param {*} request
*/
add(request) {
let data = request.body;
if(data.id){
delete data.id;
}
return this.model.create(data);
}
}
module.exports = CrudService;