Skip to content
Snippets Groups Projects
Commit b9271e96 authored by Balthazar Wilson's avatar Balthazar Wilson
Browse files

add: now possible to check results at the end of the vote

parent 3d08c833
No related branches found
No related tags found
1 merge request!8implémentation des votants par Balthazar
package ballotagent
import (
"encoding/json"
"fmt"
"net/http"
rad "gitlab.utc.fr/gvandevi/ia04binome2a"
)
func (rsa *BallotServerAgent) sendResults(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 := decodeRequest[rad.Ballot](r)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, err.Error())
return
}
// recupération des infos
scrutin := req.BallotID
voteBallot := rad.Ballot{BallotID: scrutin}
ballots := make([]rad.Ballot, len(rsa.ballots))
// Check if ballot exists
i := 0
for k := range rsa.ballots {
ballots[i] = k
i++
}
if !contains[rad.Ballot](ballots, voteBallot) {
w.WriteHeader(http.StatusNotFound)
msg := fmt.Sprintf("The ballot '%s' does not exist", scrutin)
w.Write([]byte(msg))
return
}
ballot := rsa.ballots[voteBallot]
// Check que la deadline n'est pas déjà passée
if ballot.isOpen {
w.WriteHeader(http.StatusTooEarly)
msg := fmt.Sprintf("The ballot '%s' is not closed yet.", scrutin)
w.Write([]byte(msg))
return
}
w.WriteHeader(http.StatusOK)
serial, _ := json.Marshal(rsa.ballots[voteBallot].results)
w.Write(serial)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment