2019-05-09 04:48:10 +00:00
|
|
|
import Api from "common/api";
|
|
|
|
import Form from "common/form";
|
2020-04-14 11:13:45 +00:00
|
|
|
import Model from "./model";
|
2018-07-27 15:31:39 +00:00
|
|
|
|
2020-04-14 11:13:45 +00:00
|
|
|
class Rest extends Model {
|
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();
|
|
|
|
}
|
|
|
|
|
2020-03-29 19:24:44 +00:00
|
|
|
clone() {
|
|
|
|
return new this.constructor(this.getValues());
|
|
|
|
}
|
|
|
|
|
2018-07-27 15:31:39 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-04-08 11:24:06 +00:00
|
|
|
addLink(password, expires, comment, edit) {
|
|
|
|
expires = expires ? parseInt(expires) : 0;
|
|
|
|
comment = !!comment;
|
|
|
|
edit = !!edit;
|
|
|
|
const values = {password, expires, comment, edit};
|
|
|
|
return Api.post(this.getEntityResource() + "/link", values).then((response) => Promise.resolve(this.setValues(response.data)));
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-14 11:13:45 +00:00
|
|
|
export default Rest;
|