diff --git a/internal/models/vote.go b/internal/models/vote.go index be7efe860f76508695a9bacc41fa347ef449399e..7aedbdb95aa7b8f59a5f21acf67c5a42798f80bc 100644 --- a/internal/models/vote.go +++ b/internal/models/vote.go @@ -121,9 +121,21 @@ func (d *DataHandler) putVote(w http.ResponseWriter, r *http.Request) { } var o Vote - 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 + 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 diff --git a/internal/rootmux/admin_test.go b/internal/rootmux/admin_test.go index d6fc54cb94fa1f17daeef590b4a636e71577bc6a..5e8fba7ceb6a1b715c879a8e96e2dad54d35a55b 100644 --- a/internal/rootmux/admin_test.go +++ b/internal/rootmux/admin_test.go @@ -124,7 +124,7 @@ func AdminTests(t *testing.T) { // Get Votes do("GET", "/api/Vote/", xsrfHeader, ``, 200, `[{"ID":1,"DeskRoundID":1,"CandidateListID":1,"VoiceNumber":158,"Blank":false,"NullVote":false}]`) // Update a Vote - do("PUT", "/api/Vote/1", xsrfHeader, `{"DeskRoundID":1,"CandidateListID":1,"VoiceNumber":258,"Blank":false,"NullVote":true}`, 200, `{"ID":1,"DeskRoundID":1,"CandidateListID":1,"VoiceNumber":258,"Blank":false,"NullVote":false}`) + do("PUT", "/api/Vote/1", xsrfHeader, `{"DeskRoundID":1,"CandidateListID":1,"VoiceNumber":258,"Blank":false,"NullVote":false}`, 200, `{"ID":1,"DeskRoundID":1,"CandidateListID":1,"VoiceNumber":258,"Blank":false,"NullVote":false}`) // TODO Update a DeskRound to Validated=true can only be done when votes are captured diff --git a/internal/rootmux/rootmux_test.go b/internal/rootmux/rootmux_test.go index 67bc7ed682ff26235c63f4f9dfdbe923bf4589aa..3d7a43a6907e026cbf45d3eb588c0997e226f747 100644 --- a/internal/rootmux/rootmux_test.go +++ b/internal/rootmux/rootmux_test.go @@ -111,6 +111,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`) + // Check to update the good vote + do("PUT", "/api/Vote/1", xsrfHeader, `{"DeskRoundID":1,"CandidateListID":1,"VoiceNumber":258,"Blank":false,"NullVote":false}`, 200, `{"ID":1,"DeskRoundID":1,"CandidateListID":1,"VoiceNumber":258,"Blank":false,"NullVote":false}`) + do("PUT", "/api/Vote/1", xsrfHeader, `{"DeskRoundID":1,"CandidateListID":null,"VoiceNumber":158,"Blank":true,"NullVote":false}`, 200, `{"ID":2,"DeskRoundID":1,"CandidateListID":0,"VoiceNumber":158,"Blank":true,"NullVote":false}`) + do("PUT", "/api/Vote/1", xsrfHeader, `{"DeskRoundID":1,"CandidateListID":null,"VoiceNumber":158,"Blank":false,"NullVote":true}`, 200, `{"ID":3,"DeskRoundID":1,"CandidateListID":0,"VoiceNumber":158,"Blank":false,"NullVote":true}`) + // 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`)