reveil/ui/src/lib/alarmsingle.js

64 lines
1.6 KiB
JavaScript
Raw Normal View History

2022-10-05 20:33:31 +00:00
export class AlarmSingle {
constructor(res) {
if (res) {
this.update(res);
}
}
update({ id, time, routines, comment }) {
this.id = id;
this.time = new Date(time);
this.routines = routines;
this.comment = comment;
}
async delete() {
const res = await fetch(`api/alarms/single/${this.id}`, {
method: 'DELETE',
headers: {'Accept': 'application/json'}
});
if (res.status == 200) {
return true;
} else {
throw new Error((await res.json()).errmsg);
}
}
async save() {
const res = await fetch(this.id?`api/alarms/single/${this.id}`:'api/alarms/single', {
method: this.id?'PUT':'POST',
headers: {'Accept': 'application/json'},
body: JSON.stringify(this),
});
if (res.status == 200) {
const data = await res.json();
this.update(data);
return data;
} else {
throw new Error((await res.json()).errmsg);
}
}
}
export async function getAlarmsSingle() {
const res = await fetch(`api/alarms/single`, {headers: {'Accept': 'application/json'}})
if (res.status == 200) {
const data = await res.json();
if (data === null)
return [];
else
return data.map((t) => new AlarmSingle(t));
} else {
throw new Error((await res.json()).errmsg);
}
}
export async function getAlarmSingle(aid) {
const res = await fetch(`api/alarms/single/${aid}`, {headers: {'Accept': 'application/json'}})
if (res.status == 200) {
return new AlarmSingle(await res.json());
} else {
throw new Error((await res.json()).errmsg);
}
}