From f9942ff046102ea682b084e433eecfd206e8b6c5 Mon Sep 17 00:00:00 2001 From: Alexis POYEN <punkylibre@localhost.localdomain> Date: Wed, 17 Jun 2020 10:43:51 +0200 Subject: [PATCH] Refactor : PUT vote with CandidateListID and DeskRoundID --- internal/models/vote.go | 17 +++++++---------- internal/rootmux/admin_test.go | 2 +- internal/rootmux/rootmux_test.go | 5 ----- 3 files changed, 8 insertions(+), 16 deletions(-) diff --git a/internal/models/vote.go b/internal/models/vote.go index 1d504aa..be7efe8 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 0da5abb..d6fc54c 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 6e0e9df..67bc7ed 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}`) -- GitLab