Newer
Older
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) {
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":
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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(§ion)
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)
}
}