Newer
Older
package models
import (
"encoding/json"
"errors"
"net/http"
"strconv"
"strings"
"time"
"forge.grandlyon.com/apoyen/elections/internal/auth"
"github.com/jinzhu/gorm"
)
func (d *DataHandler) handleVote(w http.ResponseWriter, r *http.Request) {
id, _ := strconv.Atoi(strings.TrimPrefix(r.URL.Path, "/api/Vote/"))
switch method := r.Method; method {
case "GET":
switch auth.GetLoggedUserTechnical(w, r).Role {
case "ADMIN", "CAPTURER", "VISUALIZER":
d.getVote(w, r)
default:
http.Error(w, ErrorRoleOfLoggedUser, http.StatusInternalServerError)
}
case "POST":
switch auth.GetLoggedUserTechnical(w, r).Role {
case "CAPTURER":
d.postVoteCapturer(w, r)
case "VISUALIZER":
http.Error(w, ErrorNotAuthorizeMethodOnRessource, http.StatusMethodNotAllowed)
default:
http.Error(w, ErrorRoleOfLoggedUser, http.StatusInternalServerError)
}
case "PUT":
switch auth.GetLoggedUserTechnical(w, r).Role {
case "CAPTURER":
d.putVoteCapturer(w, r)
case "VISUALIZER":
http.Error(w, ErrorNotAuthorizeMethodOnRessource, http.StatusMethodNotAllowed)
default:
http.Error(w, ErrorRoleOfLoggedUser, http.StatusInternalServerError)
}
case "DELETE":
switch auth.GetLoggedUserTechnical(w, r).Role {
case "CAPTURER":
d.deleteVoteCapturer(w, r, id)
case "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) getVote(w http.ResponseWriter, r *http.Request) {
var o []Vote
d.db.Find(&o)
json.NewEncoder(w).Encode(o)
}
func (d *DataHandler) postVote(w http.ResponseWriter, r *http.Request) {
var o Vote
err := json.NewDecoder(r.Body).Decode(&o)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
d.addVote(w, r, o)
}
func (d *DataHandler) postVoteCapturer(w http.ResponseWriter, r *http.Request) {
var o Vote
err := json.NewDecoder(r.Body).Decode(&o)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var flag = false
var capturer = d.getLoggedUser(w, r).(Capturer)
for _, deskRound := range capturer.DeskRounds {
if deskRound.ID == o.DeskRoundID {
flag = true
}
}
if flag {
d.addVote(w, r, o)
} else {
http.Error(w, ErrorNotAuthorizeMethodOnRessource, http.StatusMethodNotAllowed)
return
}
}
func (d *DataHandler) addVote(w http.ResponseWriter, r *http.Request, o Vote) {
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
var voteFound Vote
if o.Blank {
d.db.First(&voteFound, "desk_round_id = ? AND blank = ?", o.DeskRoundID, o.Blank)
if voteFound.ID != 0 {
http.Error(w, ErrorVoteExist, http.StatusInternalServerError)
return
}
} else if o.NullVote {
d.db.First(&voteFound, "desk_round_id = ? AND null_vote = ?", o.DeskRoundID, o.NullVote)
if voteFound.ID != 0 {
http.Error(w, ErrorVoteExist, http.StatusInternalServerError)
return
}
} else {
d.db.First(&voteFound, "desk_round_id = ? AND candidate_list_id = ?", o.DeskRoundID, o.CandidateListID)
if voteFound.ID != 0 {
http.Error(w, ErrorVoteExist, http.StatusInternalServerError)
return
}
}
if !o.Blank && !o.NullVote {
// Check that CandidateListID exist
var candidateList CandidateList
if err := d.db.First(&candidateList, o.CandidateListID).Error; err != nil {
http.Error(w, ErrorParentNotFound, http.StatusInternalServerError)
return
}
}
// Check that deskRound exist
var deskRound DeskRound
if err := d.db.Preload("Votes").First(&deskRound, o.DeskRoundID).Error; err != nil {
http.Error(w, ErrorParentNotFound, http.StatusInternalServerError)
return
}
if deskRound.Validated {
http.Error(w, ErrorValidatedVote, http.StatusInternalServerError)
return
}
d.db.Create(&o)
d.db.Last(&o)
json.NewEncoder(w).Encode(o)
}
func (d *DataHandler) putVote(w http.ResponseWriter, r *http.Request) {
var vote Vote
err := json.NewDecoder(r.Body).Decode(&vote)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
d.updateVote(w, r, vote)
}
func (d *DataHandler) putVoteCapturer(w http.ResponseWriter, r *http.Request) {
var o Vote
err := json.NewDecoder(r.Body).Decode(&o)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var flag = false
var capturer = d.getLoggedUser(w, r).(Capturer)
for _, deskRound := range capturer.DeskRounds {
if deskRound.ID == o.DeskRoundID {
flag = true
}
}
if flag {
d.updateVote(w, r, o)
} else {
http.Error(w, ErrorNotAuthorizeMethodOnRessource, http.StatusMethodNotAllowed)
return
}
}
func (d *DataHandler) updateVote(w http.ResponseWriter, r *http.Request, vote Vote) {
var deskRound DeskRound
if err := d.db.First(&deskRound, vote.DeskRoundID).Error; err != nil {
http.Error(w, ErrorParentNotFound, http.StatusNotFound)
return
}
if deskRound.Validated {
http.Error(w, ErrorValidatedVote, http.StatusInternalServerError)
return
}
if vote.Blank {
if err := d.db.Where("blank = true and desk_round_id = ?", vote.DeskRoundID).Find(&o).Error; err != nil {
http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
return
}
} else if vote.NullVote {
if err := d.db.Where("null_vote = true and desk_round_id = ?", vote.DeskRoundID).Find(&o).Error; err != nil {
http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
return
}
} else {
if err := d.db.Where("candidate_list_id = ? and desk_round_id = ?", vote.CandidateListID, vote.DeskRoundID).Find(&o).Error; err != nil {
http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
return
}
o.VoiceNumber = vote.VoiceNumber
d.db.Save(&o)
json.NewEncoder(w).Encode(o)
}
func (d *DataHandler) deleteVote(w http.ResponseWriter, r *http.Request, id int) {
if id != 0 {
var o Vote
if err := d.db.First(&o, id).Error; err != nil {
http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
return
}
// Set completed to false for deskRound if deskRound is not validated
var deskRound DeskRound
if err := d.db.First(&deskRound, o.DeskRoundID).Error; err != nil {
http.Error(w, ErrorParentNotFound, http.StatusNotFound)
return
}
if deskRound.Validated {
http.Error(w, ErrorValidatedVote, http.StatusInternalServerError)
return
}
deskRound.Completed = false
d.db.Save(&deskRound)
} else {
http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
}
}
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
func (d *DataHandler) deleteVoteCapturer(w http.ResponseWriter, r *http.Request, id int) {
if id != 0 {
var o Vote
if err := d.db.First(&o, id).Error; err != nil {
http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
return
}
var flag = false
var capturer = d.getLoggedUser(w, r).(Capturer)
for _, deskRound := range capturer.DeskRounds {
if deskRound.ID == o.DeskRoundID {
flag = true
}
}
if flag {
d.deleteVote(w, r, id)
} else {
http.Error(w, ErrorNotAuthorizeMethodOnRessource, http.StatusMethodNotAllowed)
return
}
} else {
http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
}
}
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
func (vote *Vote) AfterSave(scope *gorm.Scope) error {
var deskRound DeskRound
if err := scope.DB().First(&deskRound, vote.DeskRoundID).Error; err != nil {
return errors.New(ErrorValidateVote)
}
// Check deskCompletion
var desk Desk
if err := scope.DB().First(&desk, deskRound.DeskID).Error; err != nil {
return errors.New(ErrorValidateVote)
}
var section Section
if err := scope.DB().First(§ion, desk.SectionID).Error; err != nil {
return errors.New(ErrorValidateVote)
}
var area Area
if err := scope.DB().First(&area, section.AreaID).Error; err != nil {
return errors.New(ErrorValidateVote)
}
var round Round
if err := scope.DB().First(&round, deskRound.RoundID).Error; err != nil {
return errors.New(ErrorValidateVote)
}
var candidateLists []CandidateList
if err := scope.DB().Where("area_id = ? and round_id = ?", area.ID, round.ID).Find(&candidateLists).Error; err != nil {
return errors.New(ErrorValidateVote)
}
var listNumberPerArea = len(candidateLists)
var votes []Vote
if err := scope.DB().Where("desk_round_id = ?", deskRound.ID).Find(&votes).Error; err != nil {
return errors.New(ErrorValidateVote)
}
var votesNumberPerDesk = len(votes)
if votesNumberPerDesk == (listNumberPerArea + 2) {
deskRound.Completed = true
deskRound.DateCompletion = time.Now()
scope.DB().Save(&deskRound)
}
return nil
}