Newer
Older
import * as Messages from "/services/messages/messages.js";
let capturerModel;
export function getCapturerModel() {
if (capturerModel == null) {
capturerModel = new CapturerModel();
}
return capturerModel;
}
class CapturerModel {
constructor() {}
async getCapturer(id) {
if (this.capturers == null) await this.refreshCapturers();
let capturerToGet;
this.capturers.forEach((capturer) => {
if (capturer.ID == id) capturerToGet = capturer;
return capturerToGet;
}
async getCapturerByUserID(userID) {
if (this.capturers == null) await this.refreshCapturers();
let capturerToGet;
this.capturers.forEach((capturer) => {
if (capturer.UserID == userID) capturerToGet = capturer;
});
return capturerToGet;
}
async getCapturers() {
if (this.capturers == null) {
try {
const response = await fetch("/api/Capturer/", {
method: "GET",
headers: new Headers({
"XSRF-Token": this.current_user.xsrftoken,
}),
});
if (response.status !== 200) {
throw new Error(
`Capturers could not be fetched (status ${response.status})`
);
}
this.capturers = await response.json();
} catch (e) {
Messages.Show("is-warning", e.message);
console.error(e);
}
}
return this.capturers;
}
async saveCapturer(method, ID, UserID, Name) {
try {
const response = await fetch("/api/Capturer/" + ID, {
"XSRF-Token": this.current_user.xsrftoken,
}),
body: JSON.stringify({
ID: ID,
UserID: UserID,
Name: Name,
}),
});
if (response.status !== 200) {
throw new Error(
`Capturer could not be saved (status ${response.status})`
);
}
this.refreshCapturers();
return await response.json();
} catch (e) {
Messages.Show("is-warning", e.message);
console.error(e);
return;
}
}
async deleteCapturer(ID) {
try {
const response = await fetch("/api/Capturer/" + ID, {
method: "delete",
headers: new Headers({
"XSRF-Token": this.current_user.xsrftoken,
}),
});
if (response.status !== 200) {
throw new Error(
`Capturer could not be deleted (status ${response.status})`
);
}
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
} catch (e) {
Messages.Show("is-warning", e.message);
console.error(e);
}
}
async refreshCapturers() {
this.capturers = null;
await this.getCapturers();
}
async addCapturerToDesk(CapturerID, DeskRoundID) {
try {
const response = await fetch("/api/CapturerDeskRound/", {
method: "POST",
headers: new Headers({
"XSRF-Token": this.current_user.xsrftoken,
}),
body: JSON.stringify({
CapturerID: CapturerID,
DeskRoundID: DeskRoundID,
}),
});
if (response.status !== 200) {
throw new Error(
`DeskRound could not be added to capturer (status ${response.status})`
);
}
await response.json();
this.refreshCapturers();
} catch (e) {
Messages.Show("is-warning", e.message);
console.error(e);
return;
}
}
async removeCapturerFromDesk(CapturerID, DeskRoundID) {
try {
const response = await fetch("/api/CapturerDeskRound/", {
method: "delete",
headers: new Headers({
"XSRF-Token": this.current_user.xsrftoken,
}),
body: JSON.stringify({
CapturerID: CapturerID,
DeskRoundID: DeskRoundID,
}),
});
if (response.status !== 200) {
throw new Error(
`DeskRound could not be removed from capturer (status ${response.status})`
);
}
this.refreshCapturers();
} catch (e) {
Messages.Show("is-warning", e.message);
console.error(e);
}
}
}