From 2261ea857a0e56d5fe716a4e48ec263d5d3d94a2 Mon Sep 17 00:00:00 2001 From: Alexis POYEN <apoyen@grandlyon.com> Date: Mon, 8 Jun 2020 16:42:25 +0200 Subject: [PATCH] Resolve "Data Store" --- web/components/management/area.js | 150 ++++---------------- web/components/management/desk.js | 131 +++++------------- web/components/management/election.js | 113 +++------------ web/components/management/round-desks.js | 167 ++++++----------------- web/components/management/rounds-card.js | 108 ++++----------- web/components/management/section.js | 128 ++++------------- web/components/users/handleUser.js | 51 ++----- web/services/common/getters.js | 59 -------- web/services/model/area-model.js | 101 ++++++++++++++ web/services/model/capturer-model.js | 148 ++++++++++++++++++++ web/services/model/desk-model.js | 99 ++++++++++++++ web/services/model/deskRound-model.js | 78 +++++++++++ web/services/model/election-model.js | 103 ++++++++++++++ web/services/model/round-model.js | 101 ++++++++++++++ web/services/model/section-model.js | 97 +++++++++++++ 15 files changed, 912 insertions(+), 722 deletions(-) delete mode 100644 web/services/common/getters.js create mode 100644 web/services/model/area-model.js create mode 100644 web/services/model/capturer-model.js create mode 100644 web/services/model/desk-model.js create mode 100644 web/services/model/deskRound-model.js create mode 100644 web/services/model/election-model.js create mode 100644 web/services/model/round-model.js create mode 100644 web/services/model/section-model.js diff --git a/web/components/management/area.js b/web/components/management/area.js index 6508523..0bbaf36 100644 --- a/web/components/management/area.js +++ b/web/components/management/area.js @@ -1,14 +1,11 @@ // Imports import * as Auth from "/services/auth/auth.js"; import * as Common from "/services/common/common.js"; -import * as Messages from "/services/messages/messages.js"; +import * as AreaModel from "/services/model/area-model.js"; import { Delete } from "/services/common/delete.js"; // DOM elements -// local variables -let current_user; - export async function mount(where, parent) { const areaComponent = new Area(parent); await areaComponent.mount(where); @@ -18,9 +15,11 @@ export async function mount(where, parent) { class Area { constructor(parent) { this.parent = parent; + this.AreaModel = AreaModel.getAreaModel(); } async mount(where) { + this.AreaModel.current_user = await Auth.GetUser(); const mountpoint = where; document.getElementById(mountpoint).innerHTML = /* HTML */ ` <header class="card-header"> @@ -39,7 +38,6 @@ class Area { </div> </div> `; - current_user = await Auth.GetUser(); this.mountModal("area-modal"); this.handleDom(); } @@ -242,39 +240,17 @@ class Area { } async saveArea() { - let area; if (this.method == "POST") document.getElementById("area-modal-id").value = null; - try { - const response = await fetch( - "/api/Area/" + document.getElementById("area-modal-id").value, - { - method: this.method, - headers: new Headers({ - "XSRF-Token": current_user.xsrftoken, - }), - body: JSON.stringify({ - ID: parseInt(document.getElementById("area-modal-id").value), - ElectionID: this.election.ID, - Name: document.getElementById("area-modal-name").value, - SeatNumber: parseInt( - document.getElementById("area-modal-seat-number").value - ), - }), - } - ); - if (response.status !== 200) { - throw new Error( - `Area could not be updated or created (status ${response.status})` - ); - } - area = await response.json(); - await this.displayAreas(); - } catch (e) { - Messages.Show("is-warning", e.message); - console.error(e); - } + let area = await this.AreaModel.saveArea( + this.method, + parseInt(document.getElementById("area-modal-id").value), + this.election.ID, + document.getElementById("area-modal-name").value, + parseInt(document.getElementById("area-modal-seat-number").value) + ); + await this.displayAreas(); Common.toggleModal("area-modal", "area-modal-card"); this.activateArea(area); this.parent.sectionHandler.area = area; @@ -284,46 +260,15 @@ class Area { async updateAreas() { let areaHandler = this; - try { - const response = await fetch("/api/Area/", { - method: "GET", - headers: new Headers({ - "XSRF-Token": current_user.xsrftoken, - }), - }); - if (response.status !== 200) { - throw new Error( - `Areas could not be fetched (status ${response.status})` - ); - } - let areas = await response.json(); - return areas.filter(function (area) { - return area.ElectionID == areaHandler.election.ID; - }); - } catch (e) { - Messages.Show("is-warning", e.message); - console.error(e); - } + let areas = await this.AreaModel.getAreas(); + return areas.filter(function (area) { + return area.ElectionID == areaHandler.election.ID; + }); } async deleteArea(area) { - try { - const response = await fetch("/api/Area/" + area.ID, { - method: "delete", - headers: new Headers({ - "XSRF-Token": current_user.xsrftoken, - }), - }); - if (response.status !== 200) { - throw new Error( - `Area could not be deleted (status ${response.status})` - ); - } - } catch (e) { - Messages.Show("is-warning", e.message); - console.error(e); - } - this.displayAreas(); + await this.AreaModel.deleteArea(area.ID); + await this.displayAreas(); this.parent.sectionHandler.emptySections(); this.parent.deskHandler.emptyDesks(); document.getElementById("desk-new").setAttribute("disabled", "true"); @@ -333,54 +278,19 @@ class Area { async cloneAreas(electionCloned, electionToClone) { let areaHandler = this; await electionToClone.Areas.forEach(async (areaToClone) => { - try { - areaToClone = await areaHandler.getArea(areaToClone.ID); - const response = await fetch("/api/Area/", { - method: "POST", - headers: new Headers({ - "XSRF-Token": current_user.xsrftoken, - }), - body: JSON.stringify({ - ID: null, - ElectionID: electionCloned.ID, - Name: areaToClone.Name, - SeatNumber: areaToClone.SeatNumber, - }), - }); - if (response.status !== 200) { - throw new Error( - `Area could not be cloned (status ${response.status})` - ); - } - let areaCloned = await response.json(); - await areaHandler.parent.sectionHandler.cloneSections( - areaCloned, - areaToClone - ); - } catch (e) { - Messages.Show("is-warning", e.message); - console.error(e); - } - }); - } + areaToClone = await areaHandler.AreaModel.getArea(areaToClone.ID); + let areaCloned = await areaHandler.AreaModel.saveArea( + "POST", + null, + electionCloned.ID, + areaToClone.Name, + areaToClone.SeatNumber + ); - async getArea(id) { - try { - const response = await fetch("/api/Area/" + id, { - method: "GET", - headers: new Headers({ - "XSRF-Token": current_user.xsrftoken, - }), - }); - if (response.status !== 200) { - throw new Error( - `Area could not be fetched (status ${response.status})` - ); - } - return await response.json(); - } catch (e) { - Messages.Show("is-warning", e.message); - console.error(e); - } + await areaHandler.parent.sectionHandler.cloneSections( + areaCloned, + areaToClone + ); + }); } } diff --git a/web/components/management/desk.js b/web/components/management/desk.js index ecddcca..88b5e04 100644 --- a/web/components/management/desk.js +++ b/web/components/management/desk.js @@ -2,13 +2,11 @@ import * as Auth from "/services/auth/auth.js"; import * as Common from "/services/common/common.js"; import * as Messages from "/services/messages/messages.js"; +import * as DeskModel from "/services/model/desk-model.js"; import { Delete } from "/services/common/delete.js"; // DOM elements -// local variables -let current_user; - export async function mount(where) { const deskComponent = new Desk(); await deskComponent.mount(where); @@ -16,9 +14,12 @@ export async function mount(where) { } class Desk { - constructor() {} + constructor() { + this.DeskModel = DeskModel.getDeskModel(); + } async mount(where) { + this.DeskModel.current_user = await Auth.GetUser(); const mountpoint = where; document.getElementById(mountpoint).innerHTML = /* HTML */ ` <header class="card-header"> @@ -37,7 +38,6 @@ class Desk { </div> </div> `; - current_user = await Auth.GetUser(); this.mountModal("desk-modal"); this.handleDom(); } @@ -45,7 +45,7 @@ class Desk { handleDom() { let deskHandler = this; document.getElementById(`desk-new`).addEventListener("click", function () { - deskHandler.newDesk(deskHandler.section) + deskHandler.newDesk(deskHandler.section); }); document @@ -154,10 +154,10 @@ class Desk { } emptyDesks() { - document.getElementById("desk-title").innerHTML = - "Bureaux"; + document.getElementById("desk-title").innerHTML = "Bureaux"; this.section = null; - document.getElementById("desk-list").innerHTML = "Veuillez sélectionner une section"; + document.getElementById("desk-list").innerHTML = + "Veuillez sélectionner une section"; } deskTemplate(desk) { @@ -214,112 +214,45 @@ class Desk { } async saveDesk() { - let desk; if (this.method == "POST") document.getElementById("desk-modal-id").value = null; - try { - const response = await fetch( - "/api/Desk/" + document.getElementById("desk-modal-id").value, - { - method: this.method, - headers: new Headers({ - "XSRF-Token": current_user.xsrftoken, - }), - body: JSON.stringify({ - ID: parseInt(document.getElementById("desk-modal-id").value), - SectionID: this.section.ID, - Name: document.getElementById("desk-modal-name").value, - Subscribed: parseInt( - document.getElementById("desk-modal-subscribed").value - ), - WitnessDesk: document.getElementById("desk-modal-witness").checked, - }), - } - ); - if (response.status !== 200) { - throw new Error( - `Desk could not be updated or created (status ${response.status})` - ); - } - await response.json(); - await this.displayDesks(); - } catch (e) { - Messages.Show("is-warning", e.message); - console.error(e); - } + await this.DeskModel.saveDesk( + this.method, + parseInt(document.getElementById("desk-modal-id").value), + this.section.ID, + document.getElementById("desk-modal-name").value, + parseInt(document.getElementById("desk-modal-subscribed").value), + document.getElementById("desk-modal-witness").checked + ); + await this.displayDesks(); Common.toggleModal("desk-modal", "desk-modal-card"); } async updateDesks() { let deskHandler = this; - try { - const response = await fetch("/api/Desk/", { - method: "GET", - headers: new Headers({ - "XSRF-Token": current_user.xsrftoken, - }), - }); - if (response.status !== 200) { - throw new Error( - `Desks could not be fetched (status ${response.status})` - ); - } - let desks = await response.json(); - return desks.filter(function (desk) { - return desk.SectionID == deskHandler.section.ID; - }); - } catch (e) { - Messages.Show("is-warning", e.message); - console.error(e); - } + let desks = await this.DeskModel.getDesks(); + return desks.filter(function (desk) { + return desk.SectionID == deskHandler.section.ID; + }); } async deleteDesk(desk) { - try { - const response = await fetch("/api/Desk/" + desk.ID, { - method: "delete", - headers: new Headers({ - "XSRF-Token": current_user.xsrftoken, - }), - }); - if (response.status !== 200) { - throw new Error( - `Desk could not be deleted (status ${response.status})` - ); - } - } catch (e) { - Messages.Show("is-warning", e.message); - console.error(e); - } + await this.DeskModel.deleteDesk(desk.ID); this.displayDesks(); } async cloneDesks(sectionCloned, sectionToClone) { + let deskHandler = this; await sectionToClone.Desks.forEach(async (deskToClone) => { - try { - const response = await fetch("/api/Desk/", { - method: "POST", - headers: new Headers({ - "XSRF-Token": current_user.xsrftoken, - }), - body: JSON.stringify({ - ID: null, - SectionID: sectionCloned.ID, - Name: deskToClone.Name, - Subscribed: deskToClone.Subscribed, - WitnessDesk: deskToClone.WitnessDesk, - }), - }); - if (response.status !== 200) { - throw new Error( - `Desk could not be cloned (status ${response.status})` - ); - } - } catch (e) { - Messages.Show("is-warning", e.message); - console.error(e); - } + await deskHandler.DeskModel.saveDesk( + "POST", + null, + sectionCloned.ID, + deskToClone.Name, + deskToClone.Subscribed, + deskToClone.WitnessDesk + ); }); } } diff --git a/web/components/management/election.js b/web/components/management/election.js index 508692e..4907425 100644 --- a/web/components/management/election.js +++ b/web/components/management/election.js @@ -1,15 +1,9 @@ // Imports import * as Auth from "/services/auth/auth.js"; import * as Common from "/services/common/common.js"; -import * as Getters from "/services/common/getters.js"; -import * as Messages from "/services/messages/messages.js"; +import * as ElectionModel from "/services/model/election-model.js"; import { Delete } from "/services/common/delete.js"; -// DOM elements - -// local variables -let current_user; - export async function mount(where, parent) { const electionComponent = new Election(parent); await electionComponent.mount(where); @@ -20,9 +14,11 @@ class Election { constructor(parent) { this.method = null; this.parent = parent; + this.ElectionModel = ElectionModel.getElectionModel(); } async mount(where) { + this.ElectionModel.current_user = await Auth.GetUser(); const mountpoint = where; document.getElementById(mountpoint).innerHTML = /* HTML */ ` <header class="card-header"> @@ -39,7 +35,6 @@ class Election { <div id="election-list" class="content">Liste des élections</div> </div> `; - current_user = await Auth.GetUser(); this.mountModal("election-modal"); this.mountCloneModal("election-clone-modal"); this.handleDom(); @@ -208,7 +203,7 @@ class Election { } async displayElections() { - let elections = await Getters.getElections(current_user); + let elections = await this.ElectionModel.getElections(); const markup = elections .map((election) => this.electionTemplate(election)) .join(""); @@ -295,7 +290,7 @@ class Election { this.parent.areaHandler.emptyAreas(); this.parent.sectionHandler.emptySections(); this.parent.deskHandler.emptyDesks(); - let elections = await Getters.getElections(current_user); + let elections = await this.ElectionModel.getElections(); elections.forEach((election) => { document .getElementById(`elections-election-${election.ID}`) @@ -328,39 +323,14 @@ class Election { if (this.method == "POST") document.getElementById("election-modal-id").value = null; - try { - const response = await fetch( - "/api/Election/" + document.getElementById("election-modal-id").value, - { - method: this.method, - headers: new Headers({ - "XSRF-Token": current_user.xsrftoken, - }), - body: JSON.stringify({ - ID: parseInt(document.getElementById("election-modal-id").value), - Name: document.getElementById("election-modal-name").value, - BallotType: document.getElementById("election-modal-ballot-type") - .value, - }), - } - ); - if (response.status == 409) { - throw new Error( - `The name of the election already exist (status ${response.status})` - ); - } - if (response.status !== 200) { - throw new Error( - `Election could not be updated or created (status ${response.status})` - ); - } - election = await response.json(); - this.displayElections(); - } catch (e) { - Messages.Show("is-warning", e.message); - console.error(e); - return; - } + election = await this.ElectionModel.saveElection( + this.method, + parseInt(document.getElementById("election-modal-id").value), + document.getElementById("election-modal-name").value, + document.getElementById("election-modal-ballot-type").value + ); + + await this.displayElections(); Common.toggleModal("election-modal", "election-modal-card"); this.activateElection(election); this.parent.areaHandler.election = election; @@ -369,22 +339,7 @@ class Election { } async deleteElection(election) { - try { - const response = await fetch("/api/Election/" + election.ID, { - method: "delete", - headers: new Headers({ - "XSRF-Token": current_user.xsrftoken, - }), - }); - if (response.status !== 200) { - throw new Error( - `Election could not be deleted (status ${response.status})` - ); - } - } catch (e) { - Messages.Show("is-warning", e.message); - console.error(e); - } + await this.ElectionModel.deleteElection(election.ID); this.displayElections(); this.parent.areaHandler.emptyAreas(); this.parent.sectionHandler.emptySections(); @@ -405,41 +360,17 @@ class Election { async saveCloneElection() { let electionCloned; - let electionToClone = await getters.getElection( - current_user, + let electionToClone = await this.ElectionModel.getElection( document.getElementById("election-modal-id-clone").value ); - try { - const response = await fetch("/api/Election/", { - method: "POST", - headers: new Headers({ - "XSRF-Token": current_user.xsrftoken, - }), - body: JSON.stringify({ - ID: null, - Name: document.getElementById("election-modal-name-clone").value, - BallotType: document.getElementById( - "election-modal-ballot-type-clone" - ).value, - }), - }); - if (response.status == 409) { - throw new Error( - `The name of the election already exist (status ${response.status})` - ); - } - if (response.status !== 200) { - throw new Error( - `Election could not be cloned (status ${response.status})` - ); - } - electionCloned = await response.json(); - } catch (e) { - Messages.Show("is-warning", e.message); - console.error(e); - return; - } + electionCloned = await this.ElectionModel.saveElection( + "POST", + null, + document.getElementById("election-modal-name-clone").value, + document.getElementById("election-modal-ballot-type-clone").value + ); + await this.parent.areaHandler.cloneAreas(electionCloned, electionToClone); Common.toggleModal("election-clone-modal", "election-clone-modal-card"); await this.displayElections(); diff --git a/web/components/management/round-desks.js b/web/components/management/round-desks.js index 6d82d70..17d2ae6 100644 --- a/web/components/management/round-desks.js +++ b/web/components/management/round-desks.js @@ -1,14 +1,13 @@ // Imports import * as Auth from "/services/auth/auth.js"; import * as Common from "/services/common/common.js"; -import * as Getters from "/services/common/getters.js"; +import * as DeskModel from "/services/model/desk-model.js"; +import * as DeskRoundModel from "/services/model/deskRound-model.js"; +import * as CapturerModel from "/services/model/capturer-model.js"; import * as Messages from "/services/messages/messages.js"; // DOM elements -// local variables -let current_user; - export async function mount(where, parent) { const roundDesksComponent = new RoundDesk(parent); await roundDesksComponent.mount(where); @@ -18,9 +17,15 @@ export async function mount(where, parent) { class RoundDesk { constructor(parent) { this.parent = parent; + this.DeskModel = DeskModel.getDeskModel(); + this.DeskRoundModel = DeskRoundModel.getDeskRoundModel(); + this.CapturerModel = CapturerModel.getCapturerModel(); } async mount(where) { + this.DeskModel.current_user = await Auth.GetUser(); + this.DeskRoundModel.current_user = await Auth.GetUser(); + this.CapturerModel.current_user = await Auth.GetUser(); const mountpoint = where; document.getElementById(mountpoint).innerHTML = /* HTML */ ` <header class="card-header"> @@ -43,7 +48,6 @@ class RoundDesk { </div> </div> `; - current_user = await Auth.GetUser(); this.mountModal("capturers-modal"); this.handleDom(); } @@ -200,15 +204,17 @@ class RoundDesk { let deskRoundHandler = this; deskRounds.map(async (deskRound) => { - let desk = await Getters.getDesk(current_user, deskRound.DeskID); + let desk = await this.DeskModel.getDesk(deskRound.DeskID); document.getElementById( `deskrounds-deskround-desc-${deskRound.ID}` ).innerHTML = desk.Name; document .getElementById(`deskrounds-deskround-${deskRound.ID}`) - .addEventListener("click", function () { + .addEventListener("click", async function () { deskRoundHandler.activateDeskRound(deskRound); - deskRoundHandler.deskRound = deskRound; + deskRoundHandler.deskRound = await deskRoundHandler.DeskRoundModel.getDeskRound( + deskRound.ID + ); deskRoundHandler.displayDeskRoundsDetails(); }); document @@ -246,52 +252,23 @@ class RoundDesk { async updateDeskRounds() { let deskRoundHandler = this; - try { - const response = await fetch("/api/DeskRound/", { - method: "GET", - headers: new Headers({ - "XSRF-Token": current_user.xsrftoken, - }), - }); - if (response.status !== 200) { - throw new Error( - `DeskRounds could not be fetched (status ${response.status})` - ); - } - let deskRounds = await response.json(); - return deskRounds.filter(function (deskRound) { - return deskRound.RoundID == deskRoundHandler.round.ID; - }); - } catch (e) { - Messages.Show("is-warning", e.message); - console.error(e); - } + let deskRounds = await this.DeskRoundModel.getDeskRounds(); + return deskRounds.filter(function (deskRound) { + return deskRound.RoundID == deskRoundHandler.round.ID; + }); } async saveDeskRound() { try { - const response = await fetch("/api/DeskRound/" + this.deskRound.ID, { - method: "PUT", - headers: new Headers({ - "XSRF-Token": current_user.xsrftoken, - }), - body: JSON.stringify({ - ID: this.deskRound.ID, - Validated: document.getElementById("deskround-validated").checked, - }), - }); - if (response.status !== 200) { - throw new Error( - `DeskRound could not be updated (status ${response.status})` - ); - } - await response.json(); + this.DeskRoundModel.saveDeskRound( + this.deskRound.ID, + document.getElementById("deskround-validated").checked + ); } catch (e) { Messages.Show("is-warning", e.message); console.error(e); return; } - Messages.Show("is-success", "Bureau de vote mis à jour"); this.displayDesks(); } @@ -299,37 +276,21 @@ class RoundDesk { let deskRoundHandler = this; let capturersAvailable; let capturersActive; - try { - const response = await fetch("/api/Capturer/", { - method: "GET", - headers: new Headers({ - "XSRF-Token": current_user.xsrftoken, - }), - }); - if (response.status !== 200) { - throw new Error( - `Capturers could not be fetched (status ${response.status})` - ); - } - let capturers = await response.json(); - capturersAvailable = capturers.filter(function (capturer) { - let flag = true; - capturer.DeskRounds.forEach((deskRound) => { - if (deskRound.ID === deskRoundHandler.deskRound.ID) flag = false; - }); - return flag; + let capturers = await this.CapturerModel.getCapturers(); + capturersAvailable = capturers.filter(function (capturer) { + let flag = true; + capturer.DeskRounds.forEach((deskRound) => { + if (deskRound.ID === deskRoundHandler.deskRound.ID) flag = false; }); - capturersActive = capturers.filter(function (capturer) { - let flag = false; - capturer.DeskRounds.forEach((deskRound) => { - if (deskRound.ID === deskRoundHandler.deskRound.ID) flag = true; - }); - return flag; + return flag; + }); + capturersActive = capturers.filter(function (capturer) { + let flag = false; + capturer.DeskRounds.forEach((deskRound) => { + if (deskRound.ID === deskRoundHandler.deskRound.ID) flag = true; }); - } catch (e) { - Messages.Show("is-warning", e.message); - console.error(e); - } + return flag; + }); let markup = capturersAvailable .map((capturer) => this.capturersTemplate(capturer)) @@ -345,7 +306,10 @@ class RoundDesk { document .getElementById(`capturers-capturer-${capturer.ID}`) .addEventListener("click", async function () { - await deskRoundHandler.addCapturerToDesk(capturer); + await deskRoundHandler.CapturerModel.addCapturerToDesk( + capturer.ID, + deskRoundHandler.deskRound.ID + ); await deskRoundHandler.refreshCapturers(); }); }); @@ -354,57 +318,12 @@ class RoundDesk { document .getElementById(`capturers-capturer-${capturer.ID}`) .addEventListener("click", async function () { - await deskRoundHandler.removeCapturerFromDesk(capturer); + await deskRoundHandler.CapturerModel.removeCapturerFromDesk( + capturer.ID, + deskRoundHandler.deskRound.ID + ); await deskRoundHandler.refreshCapturers(); }); }); } - - async addCapturerToDesk(capturer) { - try { - const response = await fetch("/api/CapturerDeskRound/", { - method: "POST", - headers: new Headers({ - "XSRF-Token": current_user.xsrftoken, - }), - body: JSON.stringify({ - CapturerID: capturer.ID, - DeskRoundID: this.deskRound.ID, - }), - }); - if (response.status !== 200) { - throw new Error( - `DeskRound could not be added to capturer (status ${response.status})` - ); - } - await response.json(); - } catch (e) { - Messages.Show("is-warning", e.message); - console.error(e); - return; - } - } - - async removeCapturerFromDesk(capturer) { - try { - const response = await fetch("/api/CapturerDeskRound/", { - method: "delete", - headers: new Headers({ - "XSRF-Token": current_user.xsrftoken, - }), - body: JSON.stringify({ - CapturerID: capturer.ID, - DeskRoundID: this.deskRound.ID, - }), - }); - if (response.status !== 200) { - throw new Error( - `DeskRound could not be removed from capturer (status ${response.status})` - ); - } - } catch (e) { - Messages.Show("is-warning", e.message); - console.error(e); - } - } } diff --git a/web/components/management/rounds-card.js b/web/components/management/rounds-card.js index 561b6b0..e3a1c4c 100644 --- a/web/components/management/rounds-card.js +++ b/web/components/management/rounds-card.js @@ -1,15 +1,13 @@ // Imports import * as Auth from "/services/auth/auth.js"; import * as Common from "/services/common/common.js"; -import * as Getters from "/services/common/getters.js"; +import * as ElectionModel from "/services/model/election-model.js"; +import * as RoundModel from "/services/model/round-model.js"; import * as Messages from "/services/messages/messages.js"; import { Delete } from "/services/common/delete.js"; // DOM elements -// local variables -let current_user; - export async function mount(where, parent) { const roundsComponent = new Round(parent); await roundsComponent.mount(where); @@ -20,9 +18,13 @@ class Round { constructor(parent) { this.method = null; this.parent = parent; + this.ElectionModel = ElectionModel.getElectionModel(); + this.RoundModel = RoundModel.getRoundModel(); } async mount(where) { + this.ElectionModel.current_user = await Auth.GetUser(); + this.RoundModel.current_user = await Auth.GetUser(); const mountpoint = where; document.getElementById(mountpoint).innerHTML = /* HTML */ ` <header class="card-header"> @@ -39,7 +41,6 @@ class Round { <div id="round-list" class="content">Liste des tours</div> </div> `; - current_user = await Auth.GetUser(); this.mountModal("round-modal"); this.handleDom(); this.displayRounds(); @@ -75,7 +76,7 @@ class Round { async refreshElections() { let selectElection = document.getElementById("round-modal-election-id"); - let elections = await Getters.getElections(current_user); + let elections = await this.ElectionModel.getElections(); elections.forEach((election) => {}); for (let i = selectElection.options.length - 1; i >= 0; i--) { selectElection.remove(i); @@ -143,7 +144,7 @@ class Round { } async displayRounds() { - let rounds = await this.updateRounds(); + let rounds = await this.RoundModel.getRounds(); const markup = rounds.map((round) => this.roundTemplate(round)).join(""); document.getElementById("round-list").innerHTML = markup; @@ -154,7 +155,7 @@ class Round { .addEventListener("click", function () { roundHandler.editRound(round); }); - let election = await Getters.getElection(current_user, round.ElectionID); + let election = await this.ElectionModel.getElection(round.ElectionID); document.getElementById(`rounds-round-desc-${round.ID}`).innerHTML = election.Name + " " + @@ -172,7 +173,9 @@ class Round { roundHandler.activateRound(round); roundHandler.parent.deskRoundsHandler.round = round; await roundHandler.parent.deskRoundsHandler.displayDesks(); - document.getElementById("candidate-list-new").setAttribute("disabled", "true"); + document + .getElementById("candidate-list-new") + .setAttribute("disabled", "true"); // TODO open candidate list }); }); @@ -214,15 +217,15 @@ class Round { } async activateRound(roundToActivate) { - let rounds = await this.updateRounds(); + let rounds = await this.RoundModel.getRounds(); rounds.forEach((round) => { document - .getElementById(`rounds-round-${round.ID}`) - .classList.remove("active-card"); + .getElementById(`rounds-round-${round.ID}`) + .classList.remove("active-card"); }); document - .getElementById(`rounds-round-${roundToActivate.ID}`) - .classList.add("active-card"); + .getElementById(`rounds-round-${roundToActivate.ID}`) + .classList.add("active-card"); // TODO unselect candidateLists } @@ -246,40 +249,16 @@ class Round { } async saveRound() { - let round; if (this.method == "POST") document.getElementById("round-modal-id").value = null; - try { - const response = await fetch( - "/api/Round/" + document.getElementById("round-modal-id").value, - { - method: this.method, - headers: new Headers({ - "XSRF-Token": current_user.xsrftoken, - }), - body: JSON.stringify({ - ID: parseInt(document.getElementById("round-modal-id").value), - ElectionID: parseInt( - document.getElementById("round-modal-election-id").value - ), - Date: document.getElementById("round-modal-date").value, - Round: parseInt(document.getElementById("round-modal-round").value), - }), - } - ); - if (response.status !== 200) { - throw new Error( - `Round could not be updated or created (status ${response.status})` - ); - } - round = await response.json(); - this.displayRounds(); - } catch (e) { - Messages.Show("is-warning", e.message); - console.error(e); - return; - } + let round = this.RoundModel.saveRound( + this.method, + parseInt(document.getElementById("round-modal-id").value), + document.getElementById("round-modal-date").value, + parseInt(document.getElementById("round-modal-round").value) + ); + await this.displayRounds(); Common.toggleModal("round-modal", "round-modal-card"); this.activateRound(round); // TODO open desks @@ -287,44 +266,9 @@ class Round { return round; } - async updateRounds() { - try { - const response = await fetch("/api/Round/", { - method: "GET", - headers: new Headers({ - "XSRF-Token": current_user.xsrftoken, - }), - }); - if (response.status !== 200) { - throw new Error( - `Rounds could not be fetched (status ${response.status})` - ); - } - return await response.json(); - } catch (e) { - Messages.Show("is-warning", e.message); - console.error(e); - } - } - async deleteRound(round) { - try { - const response = await fetch("/api/Round/" + round.ID, { - method: "delete", - headers: new Headers({ - "XSRF-Token": current_user.xsrftoken, - }), - }); - if (response.status !== 200) { - throw new Error( - `Round could not be deleted (status ${response.status})` - ); - } - } catch (e) { - Messages.Show("is-warning", e.message); - console.error(e); - } - this.displayRounds(); + await this.RoundModel.deleteRound(round.ID) + await this.displayRounds(); // TODO empty desks // TODO empty candidateLists document diff --git a/web/components/management/section.js b/web/components/management/section.js index 522db0f..ffe99b4 100644 --- a/web/components/management/section.js +++ b/web/components/management/section.js @@ -2,13 +2,11 @@ import * as Auth from "/services/auth/auth.js"; import * as Common from "/services/common/common.js"; import * as Messages from "/services/messages/messages.js"; +import * as SectionModel from "/services/model/section-model.js"; import { Delete } from "/services/common/delete.js"; // DOM elements -// local variables -let current_user; - export async function mount(where, parent) { const sectionComponent = new Section(parent); await sectionComponent.mount(where); @@ -18,9 +16,11 @@ export async function mount(where, parent) { class Section { constructor(parent) { this.parent = parent; + this.SectionModel = SectionModel.getSectionModel(); } async mount(where) { + this.SectionModel.current_user = await Auth.GetUser(); const mountpoint = where; document.getElementById(mountpoint).innerHTML = /* HTML */ ` <header class="card-header"> @@ -39,7 +39,6 @@ class Section { </div> </div> `; - current_user = await Auth.GetUser(); this.mountModal("section-modal"); this.handleDom(); } @@ -245,32 +244,13 @@ class Section { if (this.method == "POST") document.getElementById("section-modal-id").value = null; - try { - const response = await fetch( - "/api/Section/" + document.getElementById("section-modal-id").value, - { - method: this.method, - headers: new Headers({ - "XSRF-Token": current_user.xsrftoken, - }), - body: JSON.stringify({ - ID: parseInt(document.getElementById("section-modal-id").value), - AreaID: this.area.ID, - Name: document.getElementById("section-modal-name").value, - }), - } - ); - if (response.status !== 200) { - throw new Error( - `Section could not be updated or created (status ${response.status})` - ); - } - section = await response.json(); - await this.displaySections(); - } catch (e) { - Messages.Show("is-warning", e.message); - console.error(e); - } + section = await this.SectionModel.saveSection( + this.method, + parseInt(document.getElementById("section-modal-id").value), + this.area.ID, + document.getElementById("section-modal-name").value + ); + await this.displaySections(); Common.toggleModal("section-modal", "section-modal-card"); this.activateSection(section); this.parent.deskHandler.section = section; @@ -280,45 +260,14 @@ class Section { async updateSections() { let sectionHandler = this; - try { - const response = await fetch("/api/Section/", { - method: "GET", - headers: new Headers({ - "XSRF-Token": current_user.xsrftoken, - }), - }); - if (response.status !== 200) { - throw new Error( - `Sections could not be fetched (status ${response.status})` - ); - } - let sections = await response.json(); - return sections.filter(function (section) { - return section.AreaID == sectionHandler.area.ID; - }); - } catch (e) { - Messages.Show("is-warning", e.message); - console.error(e); - } + let sections = await this.SectionModel.getSections(); + return sections.filter(function (section) { + return section.AreaID == sectionHandler.area.ID; + }); } async deleteSection(section) { - try { - const response = await fetch("/api/Section/" + section.ID, { - method: "delete", - headers: new Headers({ - "XSRF-Token": current_user.xsrftoken, - }), - }); - if (response.status !== 200) { - throw new Error( - `Section could not be deleted (status ${response.status})` - ); - } - } catch (e) { - Messages.Show("is-warning", e.message); - console.error(e); - } + await this.SectionModel.deleteSections(section.ID); this.displaySections(); this.parent.deskHandler.emptyDesks(); document.getElementById("desk-new").setAttribute("disabled", "true"); @@ -328,24 +277,15 @@ class Section { let sectionHandler = this; await areaToClone.Sections.forEach(async (sectionToClone) => { try { - sectionToClone = await sectionHandler.getSection(sectionToClone.ID); - const response = await fetch("/api/Section/", { - method: "POST", - headers: new Headers({ - "XSRF-Token": current_user.xsrftoken, - }), - body: JSON.stringify({ - ID: null, - AreaID: areaCloned.ID, - Name: sectionToClone.Name, - }), - }); - if (response.status !== 200) { - throw new Error( - `Section could not be cloned (status ${response.status})` - ); - } - let sectionCloned = await response.json(); + sectionToClone = await sectionHandler.SectionModel.getSection( + sectionToClone.ID + ); + let sectionCloned = await sectionHandler.SectionModel.saveSection( + "POST", + null, + areaCloned.ID, + sectionToClone.Name + ); await sectionHandler.parent.deskHandler.cloneDesks( sectionCloned, sectionToClone @@ -356,24 +296,4 @@ class Section { } }); } - - async getSection(id) { - try { - const response = await fetch("/api/Section/" + id, { - method: "GET", - headers: new Headers({ - "XSRF-Token": current_user.xsrftoken, - }), - }); - if (response.status !== 200) { - throw new Error( - `Section could not be fetched (status ${response.status})` - ); - } - return await response.json(); - } catch (e) { - Messages.Show("is-warning", e.message); - console.error(e); - } - } } diff --git a/web/components/users/handleUser.js b/web/components/users/handleUser.js index 1778178..33ce4ac 100644 --- a/web/components/users/handleUser.js +++ b/web/components/users/handleUser.js @@ -1,6 +1,7 @@ // Imports import { RandomString } from "/services/common/common.js"; import * as Messages from "/services/messages/messages.js"; +import * as CapturerModel from "/services/model/capturer-model.js"; import * as Auth from "/services/auth/auth.js"; import * as Common from "/services/common/common.js"; @@ -26,9 +27,11 @@ export async function mount(where, parent) { class HandleUser { constructor(parent) { this.parent = parent; + this.CapturerModel = CapturerModel.getCapturerModel(); } async mount(where) { + this.CapturerModel.current_user = await Auth.GetUser(); document.getElementById(where).innerHTML = /* HTML */ ` <div class="modal-background"></div> <div class="modal-card" id="users-modal-card"> @@ -152,7 +155,8 @@ class HandleUser { } if (user.role == "CAPTURER") { let capturer = await Auth.getCapturerByUserID(user.id, current_user); - if (capturer != undefined) await this.deleteCapturer(capturer); + if (capturer != undefined) + await this.CapturerModel.deleteCapturer(capturer.ID); } } @@ -232,48 +236,9 @@ class HandleUser { async postCapturer(user, method) { let capturer; - if (method === "PUT") capturer = await Auth.getCapturerByUserID(user.id, current_user); + if (method === "PUT") + capturer = await Auth.getCapturerByUserID(user.id, current_user); else capturer = {}; - console.log(capturer) - try { - const response = await fetch("/api/Capturer/" + capturer.ID, { - method: method, - headers: new Headers({ - "XSRF-Token": current_user.xsrftoken, - }), - body: JSON.stringify({ - ID: capturer.ID, - UserID: user.id, - Name: user.name, - }), - }); - if (response.status !== 200) { - throw new Error( - `Capturer could not be deleted (status ${response.status})` - ); - } - } catch (e) { - Messages.Show("is-warning", e.message); - console.error(e); - } - } - - async deleteCapturer(capturer) { - try { - const response = await fetch("/api/Capturer/" + capturer.ID, { - method: "delete", - headers: new Headers({ - "XSRF-Token": current_user.xsrftoken, - }), - }); - if (response.status !== 200) { - throw new Error( - `Capturer could not be deleted (status ${response.status})` - ); - } - } catch (e) { - Messages.Show("is-warning", e.message); - console.error(e); - } + this.CapturerModel.saveCapturer(method, capturer.ID, user.ID, user.name); } } diff --git a/web/services/common/getters.js b/web/services/common/getters.js deleted file mode 100644 index 25b4e4e..0000000 --- a/web/services/common/getters.js +++ /dev/null @@ -1,59 +0,0 @@ -export async function getElections(current_user) { - try { - const response = await fetch("/api/Election/", { - method: "GET", - headers: new Headers({ - "XSRF-Token": current_user.xsrftoken, - }), - }); - if (response.status !== 200) { - throw new Error( - `Elections could not be fetched (status ${response.status})` - ); - } - return await response.json(); - } catch (e) { - Messages.Show("is-warning", e.message); - console.error(e); - } -} - -export async function getElection(current_user, id) { - try { - const response = await fetch("/api/Election/" + id, { - method: "GET", - headers: new Headers({ - "XSRF-Token": current_user.xsrftoken, - }), - }); - if (response.status !== 200) { - throw new Error( - `Election could not be fetched (status ${response.status})` - ); - } - return await response.json(); - } catch (e) { - Messages.Show("is-warning", e.message); - console.error(e); - } -} - -export async function getDesk(current_user, id) { - try { - const response = await fetch("/api/Desk/" + id, { - method: "GET", - headers: new Headers({ - "XSRF-Token": current_user.xsrftoken, - }), - }); - if (response.status !== 200) { - throw new Error( - `Desk could not be fetched (status ${response.status})` - ); - } - return await response.json(); - } catch (e) { - Messages.Show("is-warning", e.message); - console.error(e); - } -} \ No newline at end of file diff --git a/web/services/model/area-model.js b/web/services/model/area-model.js new file mode 100644 index 0000000..d60a9f1 --- /dev/null +++ b/web/services/model/area-model.js @@ -0,0 +1,101 @@ +import * as Messages from "/services/messages/messages.js"; + +let areaModel; + +export function getAreaModel() { + if (areaModel == null) { + areaModel = new AreaModel(); + } + return areaModel; +} + +class AreaModel { + constructor() {} + + async getArea(id) { + if (this.areas == null) await this.refreshAreas(); + let areaToGet; + this.areas.forEach((area) => { + if (area.ID == id) areaToGet = area; + }); + return areaToGet; + } + + async getAreas() { + if (this.areas == null) { + try { + const response = await fetch("/api/Area/", { + method: "GET", + headers: new Headers({ + "XSRF-Token": this.current_user.xsrftoken, + }), + }); + if (response.status !== 200) { + throw new Error( + `Areas could not be fetched (status ${response.status})` + ); + } + this.areas = await response.json(); + } catch (e) { + Messages.Show("is-warning", e.message); + console.error(e); + } + } + return this.areas; + } + + async saveArea(method, ID, ElectionID, Name, SeatNumber) { + try { + const response = await fetch( + "/api/Area/" + ID, + { + method: method, + headers: new Headers({ + "XSRF-Token": this.current_user.xsrftoken, + }), + body: JSON.stringify({ + ID: ID, + ElectionID: ElectionID, + Name: Name, + SeatNumber: SeatNumber, + }), + } + ); + if (response.status !== 200) { + throw new Error( + `Area could not be updated or created (status ${response.status})` + ); + } + this.refreshAreas(); + return await response.json(); + } catch (e) { + Messages.Show("is-warning", e.message); + console.error(e); + return; + } + } + + async deleteElection(ID) { + try { + const response = await fetch("/api/Area/" + ID, { + method: "delete", + headers: new Headers({ + "XSRF-Token": this.current_user.xsrftoken, + }), + }); + if (response.status !== 200) { + throw new Error( + `Area could not be deleted (status ${response.status})` + ); + } + } catch (e) { + Messages.Show("is-warning", e.message); + console.error(e); + } + } + + async refreshAreas() { + this.areas = null; + await this.getAreas(); + } +} diff --git a/web/services/model/capturer-model.js b/web/services/model/capturer-model.js new file mode 100644 index 0000000..3569c89 --- /dev/null +++ b/web/services/model/capturer-model.js @@ -0,0 +1,148 @@ +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 deskRoundToGet; + this.capturers.forEach((deskRound) => { + if (deskRound.ID == id) deskRoundToGet = deskRound; + }); + return deskRoundToGet; + } + + 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/" + capturer.ID, { + method: method, + headers: new Headers({ + "XSRF-Token": current_user.xsrftoken, + }), + body: JSON.stringify({ + ID: ID, + UserID: UserID, + Name: Name, + }), + }); + if (response.status !== 200) { + throw new Error( + `Capturer could not be deleted (status ${response.status})` + ); + } + this.refreshCapturers(); + Messages.Show("is-success", "Bureau de vote mis à jour"); + 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})` + ); + } + } 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); + } + } +} diff --git a/web/services/model/desk-model.js b/web/services/model/desk-model.js new file mode 100644 index 0000000..ab7c6e4 --- /dev/null +++ b/web/services/model/desk-model.js @@ -0,0 +1,99 @@ +import * as Messages from "/services/messages/messages.js"; + +let deskModel; + +export function getDeskModel() { + if (deskModel == null) { + deskModel = new DeskModel(); + } + return deskModel; +} + +class DeskModel { + constructor() {} + + async getDesk(id) { + if (this.desks == null) await this.refreshDesks(); + let deskToGet; + this.desks.forEach((desk) => { + if (desk.ID == id) deskToGet = desk; + }); + return deskToGet; + } + + async getDesks() { + if (this.desks == null) { + try { + const response = await fetch("/api/Desk/", { + method: "GET", + headers: new Headers({ + "XSRF-Token": this.current_user.xsrftoken, + }), + }); + if (response.status !== 200) { + throw new Error( + `Sections could not be fetched (status ${response.status})` + ); + } + this.desks = await response.json(); + } catch (e) { + Messages.Show("is-warning", e.message); + console.error(e); + } + } + return this.desks; + } + + async saveDesk(method, ID, SectionID, Name, Subscribed, WitnessDesk) { + try { + const response = await fetch("/api/Desk/" + ID, { + method: method, + headers: new Headers({ + "XSRF-Token": this.current_user.xsrftoken, + }), + body: JSON.stringify({ + ID: ID, + SectionID: SectionID, + Name: Name, + Subscribed : Subscribed, + WitnessDesk : WitnessDesk + }), + }); + if (response.status !== 200) { + throw new Error( + `Desk could not be updated or created (status ${response.status})` + ); + } + this.refreshDesks(); + return await response.json(); + } catch (e) { + Messages.Show("is-warning", e.message); + console.error(e); + return; + } + } + + async deleteDesks(ID) { + try { + const response = await fetch("/api/Desk/" + ID, { + method: "delete", + headers: new Headers({ + "XSRF-Token": this.current_user.xsrftoken, + }), + }); + if (response.status !== 200) { + throw new Error( + `Desk could not be deleted (status ${response.status})` + ); + } + } catch (e) { + Messages.Show("is-warning", e.message); + console.error(e); + } + } + + async refreshDesks() { + this.desks = null; + await this.getDesks(); + } +} diff --git a/web/services/model/deskRound-model.js b/web/services/model/deskRound-model.js new file mode 100644 index 0000000..e9075c7 --- /dev/null +++ b/web/services/model/deskRound-model.js @@ -0,0 +1,78 @@ +import * as Messages from "/services/messages/messages.js"; + +let deskRoundModel; + +export function getDeskRoundModel() { + if (deskRoundModel == null) { + deskRoundModel = new DeskRoundModel(); + } + return deskRoundModel; +} + +class DeskRoundModel { + constructor() {} + + async getDeskRound(id) { + if (this.deskRounds == null) await this.refreshDeskRounds(); + let deskRoundToGet; + this.deskRounds.forEach((deskRound) => { + if (deskRound.ID == id) deskRoundToGet = deskRound; + }); + return deskRoundToGet; + } + + async getDeskRounds() { + if (this.deskRounds == null) { + try { + const response = await fetch("/api/DeskRound/", { + method: "GET", + headers: new Headers({ + "XSRF-Token": this.current_user.xsrftoken, + }), + }); + if (response.status !== 200) { + throw new Error( + `DeskRounds could not be fetched (status ${response.status})` + ); + } + this.deskRounds = await response.json(); + } catch (e) { + Messages.Show("is-warning", e.message); + console.error(e); + } + } + return this.deskRounds; + } + + async saveDeskRound(ID, Validated) { + try { + const response = await fetch("/api/DeskRound/" + ID, { + method: "PUT", + headers: new Headers({ + "XSRF-Token": this.current_user.xsrftoken, + }), + body: JSON.stringify({ + ID: ID, + Validated: Validated, + }), + }); + if (response.status !== 200) { + throw new Error( + `DeskRound could not be updated or created (status ${response.status})` + ); + } + this.refreshDeskRounds(); + Messages.Show("is-success", "Bureau de vote mis à jour"); + return await response.json(); + } catch (e) { + Messages.Show("is-warning", e.message); + console.error(e); + return; + } + } + + async refreshDeskRounds() { + this.deskRounds = null; + await this.getDeskRounds(); + } +} diff --git a/web/services/model/election-model.js b/web/services/model/election-model.js new file mode 100644 index 0000000..9f1aaf6 --- /dev/null +++ b/web/services/model/election-model.js @@ -0,0 +1,103 @@ +import * as Messages from "/services/messages/messages.js"; + +let electionModel + +export function getElectionModel(){ + if(electionModel == null) { + electionModel = new ElectionModel(); + } + return electionModel +} + +class ElectionModel { + constructor() {} + + async getElection(id) { + if (this.elections == null) await this.refreshElections(); + let electionToGet; + this.elections.forEach((election) => { + if (election.ID == id) electionToGet = election; + }); + return electionToGet; + } + + async getElections() { + if (this.elections == null) { + try { + const response = await fetch("/api/Election/", { + method: "GET", + headers: new Headers({ + "XSRF-Token": this.current_user.xsrftoken, + }), + }); + if (response.status !== 200) { + throw new Error( + `Elections could not be fetched (status ${response.status})` + ); + } + this.elections = await response.json(); + } catch (e) { + Messages.Show("is-warning", e.message); + console.error(e); + } + } + return this.elections; + } + + async saveElection(method, ID, Name, BallotType) { + try { + const response = await fetch("/api/Election/" + ID, { + method: method, + headers: new Headers({ + "XSRF-Token": this.current_user.xsrftoken, + }), + body: JSON.stringify({ + ID: ID, + Name: Name, + BallotType: BallotType, + }), + }); + if (response.status == 409) { + throw new Error( + `The name of the election already exist (status ${response.status})` + ); + } + if (response.status !== 200) { + throw new Error( + `Election could not be updated or created (status ${response.status})` + ); + } + this.refreshElections(); + return await response.json(); + } catch (e) { + Messages.Show("is-warning", e.message); + console.error(e); + return; + } + } + + async deleteElection(ID) { + try { + const response = await fetch("/api/Election/" + ID, { + method: "delete", + headers: new Headers({ + "XSRF-Token": this.current_user.xsrftoken, + }), + }); + if (response.status !== 200) { + throw new Error( + `Election could not be deleted (status ${response.status})` + ); + } + this.refreshElections(); + } catch (e) { + Messages.Show("is-warning", e.message); + console.error(e); + } + } + + async refreshElections() { + this.elections = null; + this.getElections(); + } +} diff --git a/web/services/model/round-model.js b/web/services/model/round-model.js new file mode 100644 index 0000000..c82872c --- /dev/null +++ b/web/services/model/round-model.js @@ -0,0 +1,101 @@ +import * as Messages from "/services/messages/messages.js"; + +let roundModel; + +export function getRoundModel() { + if (roundModel == null) { + roundModel = new RoundModel(); + } + return roundModel; +} + +class RoundModel { + constructor() {} + + async getRound(id) { + if (this.rounds == null) await this.refreshRounds(); + let roundToGet; + this.rounds.forEach((round) => { + if (round.ID == id) roundToGet = round; + }); + return roundToGet; + } + + async getRounds() { + if (this.rounds == null) { + try { + const response = await fetch("/api/Round/", { + method: "GET", + headers: new Headers({ + "XSRF-Token": this.current_user.xsrftoken, + }), + }); + if (response.status !== 200) { + throw new Error( + `Rounds could not be fetched (status ${response.status})` + ); + } + this.rounds = await response.json(); + } catch (e) { + Messages.Show("is-warning", e.message); + console.error(e); + } + } + return this.rounds; + } + + async saveRound(method, ID, ElectionID, Date, Round) { + try { + const response = await fetch( + "/api/Round/" + ID, + { + method: method, + headers: new Headers({ + "XSRF-Token": current_user.xsrftoken, + }), + body: JSON.stringify({ + ID: ID, + ElectionID: ElectionID, + Date: Date, + Round: Round, + }), + } + ); + if (response.status !== 200) { + throw new Error( + `Round could not be updated or created (status ${response.status})` + ); + } + this.refreshRounds(); + return await response.json(); + } catch (e) { + Messages.Show("is-warning", e.message); + console.error(e); + return; + } + } + + async deleteRounds(ID) { + try { + const response = await fetch("/api/Round/" + ID, { + method: "delete", + headers: new Headers({ + "XSRF-Token": this.current_user.xsrftoken, + }), + }); + if (response.status !== 200) { + throw new Error( + `Round could not be deleted (status ${response.status})` + ); + } + } catch (e) { + Messages.Show("is-warning", e.message); + console.error(e); + } + } + + async refreshRounds() { + this.rounds = null; + await this.getRounds(); + } +} diff --git a/web/services/model/section-model.js b/web/services/model/section-model.js new file mode 100644 index 0000000..96c4a28 --- /dev/null +++ b/web/services/model/section-model.js @@ -0,0 +1,97 @@ +import * as Messages from "/services/messages/messages.js"; + +let sectionModel; + +export function getSectionModel() { + if (sectionModel == null) { + sectionModel = new SectionModel(); + } + return sectionModel; +} + +class SectionModel { + constructor() {} + + async getSection(id) { + if (this.sections == null) await this.refreshSections(); + let sectionToGet; + this.sections.forEach((section) => { + if (section.ID == id) sectionToGet = section; + }); + return sectionToGet; + } + + async getSections() { + if (this.sections == null) { + try { + const response = await fetch("/api/Section/", { + method: "GET", + headers: new Headers({ + "XSRF-Token": this.current_user.xsrftoken, + }), + }); + if (response.status !== 200) { + throw new Error( + `Sections could not be fetched (status ${response.status})` + ); + } + this.sections = await response.json(); + } catch (e) { + Messages.Show("is-warning", e.message); + console.error(e); + } + } + return this.sections; + } + + async saveSection(method, ID, AreaID, Name) { + try { + const response = await fetch("/api/Section/" + ID, { + method: method, + headers: new Headers({ + "XSRF-Token": this.current_user.xsrftoken, + }), + body: JSON.stringify({ + ID: ID, + AreaID: AreaID, + Name: Name, + }), + }); + if (response.status !== 200) { + throw new Error( + `Section could not be updated or created (status ${response.status})` + ); + } + this.refreshSections(); + return await response.json(); + } catch (e) { + Messages.Show("is-warning", e.message); + console.error(e); + return; + } + } + + async deleteSections(ID) { + try { + const response = await fetch("/api/Section/" + ID, { + method: "delete", + headers: new Headers({ + "XSRF-Token": this.current_user.xsrftoken, + }), + }); + if (response.status !== 200) { + throw new Error( + `Section could not be deleted (status ${response.status})` + ); + } + } catch (e) { + Messages.Show("is-warning", e.message); + console.error(e); + } + } + + async refreshSections() { + this.sections = null; + await this.getSections(); + } +} -- GitLab