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

add server functions, response and request types, utils function ranking to give ballot result

parent e79d7a20
No related branches found
No related tags found
No related merge requests found
......@@ -126,6 +126,7 @@ func (rba *RestBallotAgent) doNewBallot(w http.ResponseWriter, r *http.Request)
return
}
// traitement de la requête
var resp ResponseNewBallot
......@@ -141,27 +142,29 @@ func (rba *RestBallotAgent) doNewBallot(w http.ResponseWriter, r *http.Request)
rba.alts[i] = Alternative(i)
}
deadline, err := time.Parse("Mon Jan _2 15:04:05 MST 2006", req.deadline)
if err != nil {
if rba.rule != "majority" && rba.rule != "approval" && rba.rule != "borda" && rba.rule != "copeland" && rba.rule != "condorcet"{
w.WriteHeader(http.StatusNotImplemented)
msg := fmt.Sprintf("Deadline \"'%s'\" is not in a correct format", req.deadline)
r.Write([]byte(msg))
msg := fmt.Sprintf("501: vote rule \"'%s'\" is not impemented in the system", req.rule)
w.Write([]byte(msg))
return
}
time.AfterFunc(time.Until(deadline), closeVote(rba))
// treat deadline
deadline, err := time.Parse("Mon Jan _2 15:04:05 MST 2006", req.deadline) // Unixdate layout
if err != nil {
w.WriteHeader(http.StatusBadRequest)
msg := fmt.Sprintf("Deadline \"'%s'\" is not in a correct format", req.deadline)
w.Write([]byte(msg))
return
}
// close vote after deadline
_ = time.AfterFunc(time.Until(deadline), func (){
rba.isOpen = false
})
w.WriteHeader(http.StatusOK)
serial, _ := json.Marshal(resp)
w.Write(serial)
w.WriteHeader(http.StatusCreated) // 201
}
func (rba *RestBallotAgent) doVote(w http.ResponseWriter, r *http.Request) {
......@@ -190,7 +193,7 @@ func (rba *RestBallotAgent) doVote(w http.ResponseWriter, r *http.Request) {
if comsoc.CheckProfileAlternative(rba.pfl, req.Vote) != nil {
w.WriteHeader(http.StatusNotImplemented)
msg := fmt.Sprintf("Vote of voter '%s' is not correct", req.ID)
r.Write([]byte(msg))
w.Write([]byte(msg))
return
}
......@@ -235,9 +238,6 @@ func (rba *RestBallotAgent) doResult(w http.ResponseWriter, r *http.Request) {
w.Write(serial)
}
func closeVote (rba *RestBallotAgent) {
rba.status = 0
}
func (rba *RestBallotAgent) Start() {
// création du routeur
......
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