Skip to content
Snippets Groups Projects
election.go 3.41 KiB
Newer Older
  • Learn to ignore specific revisions
  • Alexis POYEN's avatar
    Alexis POYEN committed
    package models
    
    import (
    	"encoding/json"
    	"net/http"
    	"strconv"
    	"strings"
    
    
    Alexis POYEN's avatar
    Alexis POYEN committed
    	"forge.grandlyon.com/gestion-des-assemblees/elections/internal/auth"
    
    func (d *DataHandler) handleElection(w http.ResponseWriter, r *http.Request) {
    
    Alexis POYEN's avatar
    Alexis POYEN committed
    	id, _ := strconv.Atoi(strings.TrimPrefix(r.URL.Path, "/api/Election/"))
    	switch method := r.Method; method {
    	case "GET":
    		switch auth.GetLoggedUserTechnical(w, r).Role {
    
    		case "ADMIN", "CAPTURER", "VISUALIZER":
    
    			d.getElection(w, r, id)
    
    Alexis POYEN's avatar
    Alexis POYEN committed
    		default:
    			http.Error(w, ErrorRoleOfLoggedUser, http.StatusInternalServerError)
    		}
    	case "POST":
    		switch auth.GetLoggedUserTechnical(w, r).Role {
    		case "ADMIN":
    
    			d.postElection(w, r)
    
    Alexis POYEN's avatar
    Alexis POYEN committed
    		case "CAPTURER", "VISUALIZER":
    			http.Error(w, ErrorNotAuthorizeMethodOnRessource, http.StatusMethodNotAllowed)
    		default:
    			http.Error(w, ErrorRoleOfLoggedUser, http.StatusInternalServerError)
    		}
    
    	case "PUT":
    		switch auth.GetLoggedUserTechnical(w, r).Role {
    		case "ADMIN":
    
    			d.putElection(w, r, id)
    
    Alexis POYEN's avatar
    Alexis POYEN committed
    		case "CAPTURER", "VISUALIZER":
    			http.Error(w, ErrorNotAuthorizeMethodOnRessource, http.StatusMethodNotAllowed)
    		default:
    			http.Error(w, ErrorRoleOfLoggedUser, http.StatusInternalServerError)
    		}
    	case "DELETE":
    		switch auth.GetLoggedUserTechnical(w, r).Role {
    		case "ADMIN":
    
    			d.deleteElection(w, r, id)
    
    Alexis POYEN's avatar
    Alexis POYEN committed
    		case "CAPTURER", "VISUALIZER":
    			http.Error(w, ErrorNotAuthorizeMethodOnRessource, http.StatusMethodNotAllowed)
    		default:
    			http.Error(w, ErrorRoleOfLoggedUser, http.StatusInternalServerError)
    		}
    	default:
    		http.Error(w, "method not allowed", 400)
    	}
    }
    
    
    func (d *DataHandler) getElection(w http.ResponseWriter, r *http.Request, id int) {
    
    Alexis POYEN's avatar
    Alexis POYEN committed
    	if id != 0 {
    		var o Election
    		if err := d.db.Preload("Areas").First(&o, id).Error; err != nil {
    			http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
    			return
    		}
    		json.NewEncoder(w).Encode(o)
    	} else {
    		var o []Election
    		d.db.Preload("Areas").Find(&o)
    		json.NewEncoder(w).Encode(o)
    	}
    }
    
    
    func (d *DataHandler) postElection(w http.ResponseWriter, r *http.Request) {
    
    Alexis POYEN's avatar
    Alexis POYEN committed
    	var o Election
    	err := json.NewDecoder(r.Body).Decode(&o)
    	if err != nil {
    		http.Error(w, err.Error(), http.StatusInternalServerError)
    
    Alexis POYEN's avatar
    Alexis POYEN committed
    		return
    
    Alexis POYEN's avatar
    Alexis POYEN committed
    	}
    
    
    	var elections []Election
    	d.db.Find(&elections)
    	for _, val := range elections {
    		if o.Name == val.Name {
    			http.Error(w, "Name Already exist.", 409)
    			return
    		}
    	}
    
    
    Alexis POYEN's avatar
    Alexis POYEN committed
    	d.db.Create(&o)
    	d.db.Last(&o)
    	json.NewEncoder(w).Encode(o)
    
    }
    
    
    func (d *DataHandler) putElection(w http.ResponseWriter, r *http.Request, id int) {
    
    Alexis POYEN's avatar
    Alexis POYEN committed
    	var o Election
    	if err := d.db.Preload("Areas").First(&o, id).Error; err != nil {
    		http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
    		return
    	}
    	var election Election
    	err := json.NewDecoder(r.Body).Decode(&election)
    	if err != nil {
    		http.Error(w, err.Error(), http.StatusInternalServerError)
    
    Alexis POYEN's avatar
    Alexis POYEN committed
    		return
    
    Alexis POYEN's avatar
    Alexis POYEN committed
    	}
    	o.Name = election.Name
    	o.BallotType = election.BallotType
    
    	o.MapAreaFile = election.MapAreaFile
    	o.MapSectionFile = election.MapSectionFile
    
    Alexis POYEN's avatar
    Alexis POYEN committed
    	d.db.Save(&o)
    	json.NewEncoder(w).Encode(o)
    
    }
    
    
    func (d *DataHandler) deleteElection(w http.ResponseWriter, r *http.Request, id int) {
    
    Alexis POYEN's avatar
    Alexis POYEN committed
    	if id != 0 {
    		var o Election
    
    		if err := d.db.Preload("Areas").Preload("Rounds").First(&o, id).Error; err != nil {
    
    Alexis POYEN's avatar
    Alexis POYEN committed
    			http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
    			return
    		}
    
    
    		for _, area := range o.Areas {
    			d.deleteArea(w, r, int(area.ID))
    		}
    
    
    		for _, round := range o.Rounds {
    			d.deleteRound(w, r, int(round.ID))
    		}
    
    
    Alexis POYEN's avatar
    Alexis POYEN committed
    		d.db.Delete(&o)
    	} else {
    		http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
    	}
    }