Skip to content
Snippets Groups Projects
candidate.go 3.37 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) handleCandidate(w http.ResponseWriter, r *http.Request) {
    	id, _ := strconv.Atoi(strings.TrimPrefix(r.URL.Path, "/api/Candidate/"))
    	switch method := r.Method; method {
    	case "GET":
    		switch auth.GetLoggedUserTechnical(w, r).Role {
    		case "ADMIN", "CAPTURER", "VISUALIZER":
    			d.getCandidate(w, r, id)
    		default:
    			http.Error(w, ErrorRoleOfLoggedUser, http.StatusInternalServerError)
    		}
    	case "POST":
    		switch auth.GetLoggedUserTechnical(w, r).Role {
    		case "ADMIN":
    			d.postCandidate(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.putCandidate(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.deleteCandidate(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) getCandidate(w http.ResponseWriter, r *http.Request, id int) {
    	if id != 0 {
    		var o Candidate
    		if err := d.db.First(&o, id).Error; err != nil {
    			http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
    			return
    		}
    		json.NewEncoder(w).Encode(o)
    	} else {
    		var o []Candidate
    		d.db.Find(&o)
    		json.NewEncoder(w).Encode(o)
    	}
    }
    
    func (d *DataHandler) postCandidate(w http.ResponseWriter, r *http.Request) {
    	var o Candidate
    	err := json.NewDecoder(r.Body).Decode(&o)
    	if err != nil {
    		http.Error(w, err.Error(), http.StatusInternalServerError)
    		return
    	}
    	// Check that CandidateList exist
    	var candidateList CandidateList
    	if err := d.db.First(&candidateList, o.CandidateListID).Error; err != nil {
    		http.Error(w, ErrorParentNotFound, http.StatusInternalServerError)
    		return
    	}
    
    	d.db.Create(&o)
    	d.db.Last(&o)
    	json.NewEncoder(w).Encode(o)
    }
    
    func (d *DataHandler) putCandidate(w http.ResponseWriter, r *http.Request, id int) {
    	var o Candidate
    	if err := d.db.First(&o, id).Error; err != nil {
    		http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
    		return
    	}
    	var candidate Candidate
    	err := json.NewDecoder(r.Body).Decode(&candidate)
    	if err != nil {
    		http.Error(w, err.Error(), http.StatusInternalServerError)
    		return
    	}
    	o.FullName = candidate.FullName
    	o.Rank = candidate.Rank
    	o.CommunityCounseller = candidate.CommunityCounseller
    	o.Birthdate = candidate.Birthdate
    	o.PotentialIncompatibility = candidate.PotentialIncompatibility
    	o.Refused = candidate.Refused
    	o.Removed = candidate.Removed
    	d.db.Save(&o)
    	json.NewEncoder(w).Encode(o)
    
    }
    
    func (d *DataHandler) deleteCandidate(w http.ResponseWriter, r *http.Request, id int) {
    	if id != 0 {
    		var o Candidate
    		if err := d.db.First(&o, id).Error; err != nil {
    			http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
    			return
    		}
    
    		d.db.Delete(&o)
    	} else {
    		http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
    	}
    }