From 773b5dbf60b8466f17aed1e5ffd4298111917af8 Mon Sep 17 00:00:00 2001
From: Alexis POYEN <punkylibre@localhost.localdomain>
Date: Wed, 17 Jun 2020 10:58:18 +0200
Subject: [PATCH] Fix : PUT update null and blank vote

---
 internal/models/vote.go          | 18 +++++++++++++++---
 internal/rootmux/admin_test.go   |  2 +-
 internal/rootmux/rootmux_test.go |  5 +++++
 3 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/internal/models/vote.go b/internal/models/vote.go
index be7efe8..7aedbdb 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 d6fc54c..5e8fba7 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 67bc7ed..3d7a43a 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`)
-- 
GitLab