Skip to content
Snippets Groups Projects
Commit 13d401a1 authored by Alexis POYEN's avatar Alexis POYEN
Browse files

Feat : add data store for vote

parent 07fcdc8f
No related branches found
No related tags found
1 merge request!42Resolve "Capture votes"
import * as Messages from "/services/messages/messages.js";
let voteModel;
export function getVoteModel() {
if (voteModel == null) {
voteModel = new VoteModel();
}
return voteModel;
}
class VoteModel {
constructor() {}
async getVote(id) {
if (this.votes == null) await this.refreshVotes();
let voteToGet;
this.votes.forEach((vote) => {
if (vote.ID == id) voteToGet = vote;
});
return voteToGet;
}
async getVotes() {
if (this.votes == null) {
try {
const response = await fetch("/api/Vote/", {
method: "GET",
headers: new Headers({
"XSRF-Token": this.current_user.xsrftoken,
}),
});
if (response.status !== 200) {
throw new Error(
`Votes could not be fetched (status ${response.status})`
);
}
this.votes = await response.json();
} catch (e) {
Messages.Show("is-warning", e.message);
console.error(e);
}
}
return this.votes;
}
async saveVote(
method,
ID,
DeskRoundID,
CandidateListID,
VoiceNumber,
Blank,
NullVote
) {
try {
const response = await fetch("/api/Vote/" + ID, {
method: method,
headers: new Headers({
"XSRF-Token": this.current_user.xsrftoken,
}),
body: JSON.stringify({
ID: ID,
DeskRoundID: DeskRoundID,
CandidateListID: CandidateListID,
VoiceNumber: VoiceNumber,
Blank: Blank,
NullVote: NullVote,
}),
});
if (response.status !== 200) {
throw new Error(
`Vote could not be updated or created (status ${response.status})`
);
}
this.refreshVotes();
return await response.json();
} catch (e) {
Messages.Show("is-warning", e.message);
console.error(e);
return;
}
}
async deleteVote(ID) {
try {
const response = await fetch("/api/Vote/" + ID, {
method: "delete",
headers: new Headers({
"XSRF-Token": this.current_user.xsrftoken,
}),
});
if (response.status !== 200) {
throw new Error(
`Vote could not be deleted (status ${response.status})`
);
}
} catch (e) {
Messages.Show("is-warning", e.message);
console.error(e);
}
}
async refreshVotes() {
this.votes = null;
await this.getVotes();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment