Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
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
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 "ADMIN", "CAPTURER":
d.postVote(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 "ADMIN", "CAPTURER":
39
40
41
42
43
44
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
case "VISUALIZER":
http.Error(w, ErrorNotAuthorizeMethodOnRessource, http.StatusMethodNotAllowed)
default:
http.Error(w, ErrorRoleOfLoggedUser, http.StatusInternalServerError)
}
case "DELETE":
switch auth.GetLoggedUserTechnical(w, r).Role {
case "ADMIN", "CAPTURER":
d.deleteVote(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
}
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
}
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
}
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
}
d.db.Delete(&o)
// Set completed to false for deskRound
var deskRound DeskRound
if err := d.db.First(&deskRound, o.DeskRoundID).Error; err != nil {
http.Error(w, ErrorParentNotFound, http.StatusNotFound)
return
}
deskRound.Completed = false
d.db.Save(&deskRound)
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
} else {
http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
}
}
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
}