Skip to content
Snippets Groups Projects
round.go 3.57 KiB
Newer Older
  • Learn to ignore specific revisions
  • package models
    
    import (
    	"encoding/json"
    	"net/http"
    	"strconv"
    	"strings"
    
    	"forge.grandlyon.com/apoyen/elections/internal/auth"
    )
    
    func (d *DataHandler) handleRound(w http.ResponseWriter, r *http.Request) {
    	id, _ := strconv.Atoi(strings.TrimPrefix(r.URL.Path, "/api/Round/"))
    	switch method := r.Method; method {
    	case "GET":
    		switch auth.GetLoggedUserTechnical(w, r).Role {
    		case "ADMIN", "CAPTURER", "VISUALIZER":
    			d.getRound(w, r, id)
    		default:
    			http.Error(w, ErrorRoleOfLoggedUser, http.StatusInternalServerError)
    		}
    	case "POST":
    		switch auth.GetLoggedUserTechnical(w, r).Role {
    		case "ADMIN":
    			d.postRound(w, r)
    		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.putRound(w, r, id)
    		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.deleteRound(w, r, id)
    		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) getRound(w http.ResponseWriter, r *http.Request, id int) {
    	if id != 0 {
    		var o Round
    		if err := d.db.Preload("DeskRounds").Preload("CandidateLists").First(&o, id).Error; err != nil {
    			http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
    			return
    		}
    		json.NewEncoder(w).Encode(o)
    	} else {
    		var o []Round
    		d.db.Preload("DeskRounds").Preload("CandidateLists").Find(&o)
    		json.NewEncoder(w).Encode(o)
    	}
    }
    
    func (d *DataHandler) postRound(w http.ResponseWriter, r *http.Request) {
    	var o Round
    	err := json.NewDecoder(r.Body).Decode(&o)
    	if err != nil {
    		http.Error(w, err.Error(), http.StatusInternalServerError)
    		return
    	}
    
    	d.db.Create(&o)
    	d.db.Last(&o)
    
    	var election Election
    	d.db.Preload("Areas").First(&election, o.ElectionID)
    	for _, area := range election.Areas {
    		d.db.Preload("Sections").First(&area, area.ID)
    		for _, section := range area.Sections {
    			d.db.Preload("Desks").First(&section, section.ID)
    			for _, desk := range section.Desks {
    				d.createDeskRound(o.ID, desk.ID)
    			}
    		}
    	}
    	json.NewEncoder(w).Encode(o)
    
    }
    
    func (d *DataHandler) putRound(w http.ResponseWriter, r *http.Request, id int) {
    	var o Round
    	if err := d.db.Preload("DeskRounds").Preload("CandidateLists").First(&o, id).Error; err != nil {
    		http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
    		return
    	}
    
    	var round Round
    	err := json.NewDecoder(r.Body).Decode(&round)
    	if err != nil {
    		http.Error(w, err.Error(), http.StatusInternalServerError)
    		return
    	}
    	o.Date = round.Date
    	o.Round = round.Round
    	d.db.Save(&o)
    	json.NewEncoder(w).Encode(o)
    
    }
    
    func (d *DataHandler) deleteRound(w http.ResponseWriter, r *http.Request, id int) {
    	if id != 0 {
    		var o Round
    		if err := d.db.Preload("DeskRounds").Preload("CandidateLists").First(&o, id).Error; err != nil {
    			http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
    			return
    		}
    
    		for _, deskRound := range o.DeskRounds {
    			d.deleteDeskRound(deskRound)
    		}
    
    
    		for _, candidateList := range o.CandidateLists {
    			d.deleteCandidateList(w, r, int(candidateList.ID))
    		}
    
    
    		d.db.Delete(&o)
    	} else {
    		http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
    	}
    }