From 462752e429736b207b993c39a34f889ef2a0d3de Mon Sep 17 00:00:00 2001
From: Balthazar Wilson <wilsonba@etu.utc.fr>
Date: Mon, 16 Oct 2023 21:33:03 +0200
Subject: [PATCH] WIP: vote

---
 agt/ballotagent/ballotagent.go |  4 ++--
 agt/ballotagent/vote.go        | 29 +++++++++++++++++++++++++++++
 2 files changed, 31 insertions(+), 2 deletions(-)
 create mode 100644 agt/ballotagent/vote.go

diff --git a/agt/ballotagent/ballotagent.go b/agt/ballotagent/ballotagent.go
index 1f53593..33f6b88 100644
--- a/agt/ballotagent/ballotagent.go
+++ b/agt/ballotagent/ballotagent.go
@@ -34,7 +34,7 @@ func (rsa *BallotServerAgent) checkMethod(method string, w http.ResponseWriter,
 	return true
 }
 
-func (*BallotServerAgent) decodeRequest(r *http.Request) (req rad.BallotRequest, err error) {
+func (*BallotServerAgent) decodeRequest[R any](r *http.Request) (req rad.BallotRequest, err error) {
 	buf := new(bytes.Buffer)
 	buf.ReadFrom(r.Body)
 	err = json.Unmarshal(buf.Bytes(), &req)
@@ -57,7 +57,7 @@ func (rsa *BallotServerAgent) Start() {
 	// création du multiplexer
 	mux := http.NewServeMux()
 	mux.HandleFunc("/new_ballot", rsa.createBallot)
-	mux.HandleFunc("/vote", rsa.doReqcount)
+	mux.HandleFunc("/vote", rsa.receiveVote)
 	mux.HandleFunc("/result", rsa.doReqcount)
 
 	rsa.ballots = make(map[rad.Ballot]BallotInfo)
diff --git a/agt/ballotagent/vote.go b/agt/ballotagent/vote.go
new file mode 100644
index 0000000..ac17b11
--- /dev/null
+++ b/agt/ballotagent/vote.go
@@ -0,0 +1,29 @@
+package ballotagent
+
+import (
+	"fmt"
+	"net/http"
+)
+
+func (rsa *BallotServerAgent) receiveVote(w http.ResponseWriter, r *http.Request) {
+	// mise à jour du nombre de requêtes
+	rsa.Lock()
+	defer rsa.Unlock()
+	rsa.reqCount++
+
+	// vérification de la méthode de la requête
+	if !rsa.checkMethod("POST", w, r) {
+		return
+	}
+
+	// décodage de la requête
+	req, err := rsa.decodeRequest(r)
+	if err != nil {
+		w.WriteHeader(http.StatusBadRequest)
+		fmt.Fprint(w, err.Error())
+		return
+	}
+
+	// recupération des infos
+	scrutin := req.
+}
-- 
GitLab