diff --git a/internal/models/vote.go b/internal/models/vote.go index 1d504aa909009c4b5cbc090e6d6351b68550c35c..be7efe860f76508695a9bacc41fa347ef449399e 100644 --- a/internal/models/vote.go +++ b/internal/models/vote.go @@ -35,7 +35,7 @@ func (d *DataHandler) handleVote(w http.ResponseWriter, r *http.Request) { case "PUT": switch auth.GetLoggedUserTechnical(w, r).Role { case "ADMIN", "CAPTURER": - d.putVote(w, r, id) + d.putVote(w, r) case "VISUALIZER": http.Error(w, ErrorNotAuthorizeMethodOnRessource, http.StatusMethodNotAllowed) default: @@ -112,23 +112,20 @@ func (d *DataHandler) postVote(w http.ResponseWriter, r *http.Request) { } -func (d *DataHandler) putVote(w http.ResponseWriter, r *http.Request, id int) { - var o Vote - if err := d.db.First(&o, id).Error; err != nil { - http.Error(w, ErrorIDIsMissing, http.StatusNotFound) - return - } +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 } - // check that objects are the same - if o.CandidateListID != vote.CandidateListID || o.DeskRoundID != vote.DeskRoundID { - http.Error(w, "Les objets ne correspondent pas", http.StatusInternalServerError) + + 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 } + o.VoiceNumber = vote.VoiceNumber d.db.Save(&o) json.NewEncoder(w).Encode(o) diff --git a/internal/rootmux/admin_test.go b/internal/rootmux/admin_test.go index 0da5abb840d71b9a14bf1f33566c99f90961f409..d6fc54cb94fa1f17daeef590b4a636e71577bc6a 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, `{"ID":1,"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":true}`, 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 6e0e9df93e2ccf02128e056f7e37cb95456e020f..67bc7ed682ff26235c63f4f9dfdbe923bf4589aa 100644 --- a/internal/rootmux/rootmux_test.go +++ b/internal/rootmux/rootmux_test.go @@ -106,11 +106,6 @@ func appTests(t *testing.T) { // Verify that a DeskRound can't be validated witout being completed do("PUT", "/api/DeskRound/1", xsrfHeader, `{"ID":1,"Validated":true}`, 500, `Le bureau doit être complété avant de le valider`) - // Verify that you can't update a Vote if it's not the same - do("PUT", "/api/Vote/1", xsrfHeader, `{"ID":1,"DeskRoundID":2,"CandidateListID":1,"VoiceNumber":258,"Blank":false,"NullVote":true}`, 500, `Les objets ne correspondent pas`) - do("PUT", "/api/Vote/1", xsrfHeader, `{"ID":1,"DeskRoundID":2,"CandidateListID":2,"VoiceNumber":258,"Blank":false,"NullVote":true}`, 500, `Les objets ne correspondent pas`) - do("PUT", "/api/Vote/1", xsrfHeader, `{"ID":1,"DeskRoundID":1,"CandidateListID":2,"VoiceNumber":258,"Blank":false,"NullVote":true}`, 500, `Les objets ne correspondent pas`) - // Create Votes to complete a Desk do("POST", "/api/Vote", xsrfHeader, `{"DeskRoundID":1,"CandidateListID":null,"VoiceNumber":3,"Blank":true}`, 200, `{"ID":2,"DeskRoundID":1,"CandidateListID":0,"VoiceNumber":3,"Blank":true,"NullVote":false}`) 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}`)