Skip to content
Snippets Groups Projects
election-model.js 2.68 KiB
Newer Older
  • Learn to ignore specific revisions
  • Alexis POYEN's avatar
    Alexis POYEN committed
    import * as Messages from "/services/messages/messages.js";
    
    
    let electionModel;
    
    Alexis POYEN's avatar
    Alexis POYEN committed
    
    
    export function getElectionModel() {
      if (electionModel == null) {
    
    Alexis POYEN's avatar
    Alexis POYEN committed
        electionModel = new ElectionModel();
      }
    
      return electionModel;
    
    Alexis POYEN's avatar
    Alexis POYEN committed
    }
    
    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,
        MapAreaFile,
        MapSectionFile
      ) {
    
    Alexis POYEN's avatar
    Alexis POYEN committed
        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,
    
              MapAreaFile: MapAreaFile,
              MapSectionFile: MapSectionFile,
    
    Alexis POYEN's avatar
    Alexis POYEN committed
            }),
          });
          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;
    
        await this.getElections();