Skip to content
Snippets Groups Projects
Commit dd6a9053 authored by Antoine Kryus's avatar Antoine Kryus
Browse files

update vote route function

parent 225f8360
No related branches found
No related tags found
No related merge requests found
......@@ -59,6 +59,8 @@ type RestBallotAgent struct {
isOpen bool // open: 1 / closed: 0
voterIDs []string
alts []comsoc.Alternative
count comsoc.Count
bestAlts []comsoc.Alternative
}
......@@ -158,11 +160,25 @@ func (rba *RestBallotAgent) doNewBallot(w http.ResponseWriter, r *http.Request)
// close vote after deadline
_ = time.AfterFunc(time.Until(deadline), func (){
rba.isOpen = false
})
var err error
switch rba.rule {
case "borda":
rba.count, err = comsoc.BordaSWF(rba.pfl)
rba.bestAlts, err = comsoc.BordaSCF(rba.pfl)
case "copeland":
rba.count, err = comsoc.CopelandSWF(rba.pfl)
rba.bestAlts, err = comsoc.CopelandSCF(rba.pfl)
case "majority":
rba.count, err = comsoc.MajoritySWF(rba.pfl)
rba.bestAlts, err = comsoc.MajoritySCF(rba.pfl)
}
}) // missing case approval
resp.ballotID = fmt.Sprintf("vote%03d", rba.ID)
w.WriteHeader(http.StatusCreated) // 201
serial, _ := json.Marshal(resp)
w.Write(serial)
}
func (rba *RestBallotAgent) doVote(w http.ResponseWriter, r *http.Request) {
......@@ -204,6 +220,14 @@ func (rba *RestBallotAgent) doVote(w http.ResponseWriter, r *http.Request) {
return
}
// ballot ID checking
if fmt.Sprintf("vote%03d", rba.ID) != req.voteID {
w.WriteHeader(http.StatusNotFound)
msg := fmt.Sprintf("404: ballot ID '%s' not found.", req.voteID)
w.Write([]byte(msg))
return
}
// Process options case
if len(req.options) > 0 { // modify conditions according to options implemented
......@@ -224,16 +248,10 @@ func (rba *RestBallotAgent) doVote(w http.ResponseWriter, r *http.Request) {
return
}
switch rba.rule {
case "borda":
//call borda SCF and SWF and store results
}
// code here
w.WriteHeader(http.StatusOK)
// serial, _ := json.Marshal(resp)
// w.Write(serial)
msg := fmt.Sprintf("200: OK. Vote taken in account.", req.agentID)
w.Write([]byte(msg))
return
}
func (rba *RestBallotAgent) doResult(w http.ResponseWriter, r *http.Request) {
......@@ -254,10 +272,28 @@ func (rba *RestBallotAgent) doResult(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, err.Error())
return
}
// Deadline checking
if rba.isOpen {
w.WriteHeader(http.StatusTooEarly)
msg := fmt.Sprintf("425: Deadline of vote '%s' has not passed yet. Too early.", req.ballotID)
w.Write([]byte(msg))
return
}
// ballot ID checking
if fmt.Sprintf("vote%03d", rba.ID) != req.ballotID {
w.WriteHeader(http.StatusNotFound)
msg := fmt.Sprintf("404: ballot ID '%s' not found.", req.ballotID)
w.Write([]byte(msg))
return
}
// traitement de la requête
var resp ResponseResult
resp.winner = int(rba.bestAlts[0]) // gérer les tie-break
resp.ranking = comsoc.ranking(rba.count)
// code here
w.WriteHeader(http.StatusOK)
......@@ -269,9 +305,9 @@ func (rba *RestBallotAgent) doResult(w http.ResponseWriter, r *http.Request) {
func (rba *RestBallotAgent) Start() {
// création du routeur
mux := http.NewServeMux()
mux.HandleFunc("/new_ballot")
mux.HandleFunc("/vote")
mux.HandleFunc("/result")
mux.HandleFunc("/new_ballot", rba.doNewBallot)
mux.HandleFunc("/vote", rba.doVote)
mux.HandleFunc("/result", rba.doResult)
// création du serveur http
s := &http.Server{
......
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