2019-05-09 04:48:10 +00:00
|
|
|
import Api from "common/api";
|
|
|
|
import Form from "common/form";
|
2018-07-27 15:31:39 +00:00
|
|
|
|
|
|
|
class Abstract {
|
|
|
|
constructor(values) {
|
|
|
|
this.__originalValues = {};
|
|
|
|
|
|
|
|
if (values) {
|
|
|
|
this.setValues(values);
|
2020-01-23 23:45:59 +00:00
|
|
|
} else {
|
|
|
|
this.setValues(this.getDefaults());
|
2018-07-27 15:31:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setValues(values) {
|
2020-01-24 09:25:08 +00:00
|
|
|
if (!values) return;
|
2018-07-27 15:31:39 +00:00
|
|
|
|
2020-01-24 09:25:08 +00:00
|
|
|
for (let key in values) {
|
|
|
|
if (values.hasOwnProperty(key) && key !== "__originalValues") {
|
2018-07-27 15:31:39 +00:00
|
|
|
this[key] = values[key];
|
2020-03-26 08:41:33 +00:00
|
|
|
if(typeof values[key] === "object") {
|
|
|
|
this.__originalValues[key] = JSON.parse(JSON.stringify(values[key]));
|
|
|
|
} else {
|
|
|
|
this.__originalValues[key] = values[key];
|
|
|
|
}
|
|
|
|
|
2018-07-27 15:31:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-01-24 09:25:08 +00:00
|
|
|
getValues(changed) {
|
2018-07-27 15:31:39 +00:00
|
|
|
const result = {};
|
2020-01-24 09:25:08 +00:00
|
|
|
const defaults = this.getDefaults();
|
|
|
|
|
|
|
|
for (let key in this.__originalValues) {
|
|
|
|
if (this.__originalValues.hasOwnProperty(key) && key !== "__originalValues") {
|
|
|
|
let val;
|
|
|
|
if (defaults.hasOwnProperty(key)) {
|
|
|
|
switch (typeof defaults[key]) {
|
2020-01-31 14:29:06 +00:00
|
|
|
case "bigint":
|
|
|
|
case "number":
|
|
|
|
val = parseFloat(this[key]);
|
|
|
|
break;
|
|
|
|
case "boolean":
|
|
|
|
val = !!this[key];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
val = this[key];
|
2020-01-24 09:25:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
val = this[key];
|
|
|
|
}
|
|
|
|
|
2020-03-26 08:41:33 +00:00
|
|
|
if(!changed || JSON.stringify(val) !== JSON.stringify(this.__originalValues[key])) {
|
2020-01-24 09:25:08 +00:00
|
|
|
result[key] = val;
|
|
|
|
}
|
2018-07-27 15:31:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-01-23 23:45:59 +00:00
|
|
|
getDefaults() {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-07-27 15:31:39 +00:00
|
|
|
getId() {
|
2019-05-21 09:54:39 +00:00
|
|
|
return this.ID;
|
2018-07-27 15:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hasId() {
|
|
|
|
return !!this.getId();
|
|
|
|
}
|
|
|
|
|
|
|
|
find(id, params) {
|
2019-12-05 13:11:45 +00:00
|
|
|
return Api.get(this.getEntityResource(id), params).then((response) => Promise.resolve(new this.constructor(response.data)));
|
2018-07-27 15:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
save() {
|
|
|
|
if (this.hasId()) {
|
|
|
|
return this.update();
|
|
|
|
}
|
|
|
|
|
2020-01-30 08:51:23 +00:00
|
|
|
return Api.post(this.constructor.getCollectionResource(), this.getValues()).then((response) => Promise.resolve(this.setValues(response.data)));
|
2018-07-27 15:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
update() {
|
2020-01-30 08:51:23 +00:00
|
|
|
return Api.put(this.getEntityResource(), this.getValues(true)).then((response) => Promise.resolve(this.setValues(response.data)));
|
2018-07-27 15:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
remove() {
|
|
|
|
return Api.delete(this.getEntityResource()).then(() => Promise.resolve(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
getEditForm() {
|
|
|
|
return Api.options(this.getEntityResource()).then(response => Promise.resolve(new Form(response.data)));
|
|
|
|
}
|
|
|
|
|
|
|
|
getEntityResource(id) {
|
|
|
|
if (!id) {
|
|
|
|
id = this.getId();
|
|
|
|
}
|
|
|
|
|
2019-05-09 04:48:10 +00:00
|
|
|
return this.constructor.getCollectionResource() + "/" + id;
|
2018-07-27 15:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getEntityName() {
|
2019-05-09 04:48:10 +00:00
|
|
|
return this.constructor.getModelName() + " " + this.getId();
|
2018-07-27 15:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static getCollectionResource() {
|
2019-05-09 04:48:10 +00:00
|
|
|
throw new Error("getCollectionResource() needs to be implemented");
|
2018-07-27 15:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static getCreateResource() {
|
2019-06-17 19:45:06 +00:00
|
|
|
return this.getCollectionResource();
|
2018-07-27 15:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static getCreateForm() {
|
|
|
|
return Api.options(this.getCreateResource()).then(response => Promise.resolve(new Form(response.data)));
|
|
|
|
}
|
|
|
|
|
|
|
|
static getModelName() {
|
2019-05-09 04:48:10 +00:00
|
|
|
return "Item";
|
2018-07-27 15:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static getSearchForm() {
|
|
|
|
return Api.options(this.getCollectionResource()).then(response => Promise.resolve(new Form(response.data)));
|
|
|
|
}
|
|
|
|
|
|
|
|
static search(params) {
|
|
|
|
const options = {
|
|
|
|
params: params,
|
|
|
|
};
|
|
|
|
|
|
|
|
return Api.get(this.getCollectionResource(), options).then((response) => {
|
|
|
|
response.models = [];
|
|
|
|
|
2020-01-24 09:25:08 +00:00
|
|
|
for (let i = 0; i < response.data.length; i++) {
|
2018-07-27 15:31:39 +00:00
|
|
|
response.models.push(new this(response.data[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.resolve(response);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Abstract;
|