Skip to content
Snippets Groups Projects
Commit 549bc7fa authored by Alexis POYEN's avatar Alexis POYEN
Browse files

Feat : can't add the same vote several time

parent 59f398d2
No related branches found
No related tags found
1 merge request!39Resolve "API for Votes"
Pipeline #5942 passed
......@@ -35,9 +35,12 @@ const ErrorNotAuthorizeMethodOnRessource = "You're not authorize to execute this
// ErrorParentNotFound = "Could not get the parent associated to the object" with 500 http.StatusInternalServerError
const ErrorParentNotFound = "Could not get the parent associated to the object"
// ErrorValidateVote = "Could not get the parent associated to the object" with 500 http.StatusInternalServerError
// ErrorValidateVote = "Error in the process to validate a vote" with 500 http.StatusInternalServerError
const ErrorValidateVote = "Error in the process to validate a vote"
// ErrorVoteExist = "Error the vote have already been captured" with 500 http.StatusInternalServerError
const ErrorVoteExist = "Error the vote have already been captured"
// Election represent an election divided in areas with 1 or several rounds
type Election struct {
ID uint `gorm:"primary_key"`
......
......@@ -69,6 +69,27 @@ func (d *DataHandler) postVote(w http.ResponseWriter, r *http.Request) {
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
......
......@@ -116,6 +116,11 @@ func appTests(t *testing.T) {
do("POST", "/api/Vote", xsrfHeader, `{"DeskRoundID":1,"CandidateListID":null,"VoiceNumber":5,"NullVote":true}`, 200, `{"ID":3,"DeskRoundID":1,"CandidateListID":0,"VoiceNumber":5,"Blank":false,"NullVote":true}`)
do("GET", "/api/DeskRound/1", xsrfHeader, ``, 200, `{"ID":1,"RoundID":1,"DeskID":1,"Capturers":[],"Completed":true,"DateCompletion":"20`)
// Can't add the same vote several time
do("POST", "/api/Vote", xsrfHeader, `{"DeskRoundID":1,"CandidateListID":1,"VoiceNumber":158}`, 500, `Error the vote have already been captured`)
do("POST", "/api/Vote", xsrfHeader, `{"DeskRoundID":1,"CandidateListID":null,"VoiceNumber":3,"Blank":true}`, 500, `Error the vote have already been captured`)
do("POST", "/api/Vote", xsrfHeader, `{"DeskRoundID":1,"CandidateListID":null,"VoiceNumber":5,"NullVote":true}`, 500, `Error the vote have already been captured`)
// Verify that on Desk deletion deskRounds are deleted
do("GET", "/api/Desk/1", xsrfHeader, ``, 200, `{"ID":1,"SectionID":1,"Name":"Desk 1","WitnessDesk":true,"Subscribed":9587,"DeskRounds":[{"ID":1,"RoundID":1,"DeskID":1,"Capturers":null,"Completed":true,"DateCompletion":"20`)
do("DELETE", "/api/Desk/1", xsrfHeader, ``, 200, ``)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment