Newer
Older
package models
import (
"encoding/json"
"net/http"
"strconv"
"strings"
"forge.grandlyon.com/gestion-des-assemblees/elections/internal/auth"
func (d *DataHandler) handleDesk(w http.ResponseWriter, r *http.Request) {
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
id, _ := strconv.Atoi(strings.TrimPrefix(r.URL.Path, "/api/Desk/"))
switch method := r.Method; method {
case "GET":
switch auth.GetLoggedUserTechnical(w, r).Role {
case "ADMIN", "CAPTURER", "VISUALIZER":
d.getDesk(w, r, id)
default:
http.Error(w, ErrorRoleOfLoggedUser, http.StatusInternalServerError)
}
case "POST":
switch auth.GetLoggedUserTechnical(w, r).Role {
case "ADMIN":
d.postDesk(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.putDesk(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.deleteDesk(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) getDesk(w http.ResponseWriter, r *http.Request, id int) {
if id != 0 {
var o Desk
if err := d.db.Preload("DeskRounds").First(&o, id).Error; err != nil {
http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
return
}
json.NewEncoder(w).Encode(o)
} else {
var o []Desk
json.NewEncoder(w).Encode(o)
}
}
func (d *DataHandler) postDesk(w http.ResponseWriter, r *http.Request) {
var o Desk
err := json.NewDecoder(r.Body).Decode(&o)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Check that SectionID exist
var section Section
if err := d.db.First(§ion, o.SectionID).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) putDesk(w http.ResponseWriter, r *http.Request, id int) {
var o Desk
if err := d.db.Preload("DeskRounds").First(&o, id).Error; err != nil {
http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
return
}
var desk Desk
err := json.NewDecoder(r.Body).Decode(&desk)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
o.Name = desk.Name
o.WitnessDesk = desk.WitnessDesk
o.Subscribed = desk.Subscribed
d.db.Save(&o)
json.NewEncoder(w).Encode(o)
}
func (d *DataHandler) deleteDesk(w http.ResponseWriter, r *http.Request, id int) {
if id != 0 {
var o Desk
if err := d.db.Preload("DeskRounds").First(&o, id).Error; err != nil {
http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
return
}
for _, deskRound := range o.DeskRounds {
d.deleteDeskRound(deskRound)
}
d.db.Delete(&o)
} else {
http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
}
}