Skip to content
Snippets Groups Projects
section.go 3.12 KiB
Newer Older
  • Learn to ignore specific revisions
  • Alexis POYEN's avatar
    Alexis POYEN committed
    package models
    
    import (
    	"encoding/json"
    	"net/http"
    	"strconv"
    	"strings"
    
    	"forge.grandlyon.com/apoyen/elections/internal/auth"
    )
    
    
    func (d *DataHandler) handleSection(w http.ResponseWriter, r *http.Request) {
    
    Alexis POYEN's avatar
    Alexis POYEN committed
    	id, _ := strconv.Atoi(strings.TrimPrefix(r.URL.Path, "/api/Section/"))
    	switch method := r.Method; method {
    	case "GET":
    		switch auth.GetLoggedUserTechnical(w, r).Role {
    		case "ADMIN", "CAPTURER", "VISUALIZER":
    			d.getSection(w, r, id)
    		default:
    			http.Error(w, ErrorRoleOfLoggedUser, http.StatusInternalServerError)
    		}
    	case "POST":
    		switch auth.GetLoggedUserTechnical(w, r).Role {
    		case "ADMIN":
    			d.postSection(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.putSection(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":
    
    Alexis POYEN's avatar
    Alexis POYEN committed
    			d.deleteSection(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) getSection(w http.ResponseWriter, r *http.Request, id int) {
    	if id != 0 {
    		var o Section
    		if err := d.db.Preload("Desks").First(&o, id).Error; err != nil {
    			http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
    			return
    		}
    		json.NewEncoder(w).Encode(o)
    	} else {
    		var o []Section
    		d.db.Preload("Desks").Find(&o)
    		json.NewEncoder(w).Encode(o)
    	}
    }
    
    func (d *DataHandler) postSection(w http.ResponseWriter, r *http.Request) {
    	var o Section
    	err := json.NewDecoder(r.Body).Decode(&o)
    	if err != nil {
    		http.Error(w, err.Error(), http.StatusInternalServerError)
    		return
    	}
    	// Check that AreaID exist
    	var area Area
    	if err := d.db.First(&area, o.AreaID).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) putSection(w http.ResponseWriter, r *http.Request, id int) {
    	var o Section
    	if err := d.db.Preload("Desks").First(&o, id).Error; err != nil {
    		http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
    		return
    	}
    	var section Section
    	err := json.NewDecoder(r.Body).Decode(&section)
    	if err != nil {
    		http.Error(w, err.Error(), http.StatusInternalServerError)
    		return
    	}
    	o.Name = section.Name
    	o.MapID = section.MapID
    	d.db.Save(&o)
    	json.NewEncoder(w).Encode(o)
    
    }
    
    func (d *DataHandler) deleteSection(w http.ResponseWriter, r *http.Request, id int) {
    	if id != 0 {
    		var o Section
    		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)
    	}
    }