Incomplete implementation of createVotes
This commit is contained in:
@ -3,7 +3,9 @@ package main
|
||||
import (
|
||||
api "code.dlmw.ch/dlmw/qv/internal"
|
||||
"code.dlmw.ch/dlmw/qv/internal/validator"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
@ -107,17 +109,16 @@ type createVotesRequestWithValidator struct {
|
||||
}
|
||||
|
||||
func (r *createVotesRequestWithValidator) isValid() bool {
|
||||
r.CheckField(validator.NotBlank(*r.VoterIdentity), "voterIdentity", "must not be blank")
|
||||
r.CheckField(validator.GreaterThan(*r.ElectionId, 0), "electionId", "must be greater than 0")
|
||||
r.CheckField(validator.GreaterThan(r.ElectionId, 0), "electionId", "must be greater than 0")
|
||||
|
||||
for _, choice := range *r.Choices {
|
||||
r.CheckField(validator.NotBlank(*choice.ChoiceText), "choiceText", "must not be blank")
|
||||
for _, choice := range r.Choices {
|
||||
r.CheckField(validator.NotBlank(choice.ChoiceText), "choiceText", "must not be blank")
|
||||
}
|
||||
|
||||
return r.Valid()
|
||||
}
|
||||
|
||||
func (app *application) createVote(w http.ResponseWriter, r *http.Request) {
|
||||
func (app *application) createVotes(w http.ResponseWriter, r *http.Request) {
|
||||
var request createVotesRequestWithValidator
|
||||
|
||||
if err := app.unmarshalRequest(r, &request); err != nil {
|
||||
@ -129,4 +130,32 @@ func (app *application) createVote(w http.ResponseWriter, r *http.Request) {
|
||||
app.unprocessableEntityError(w, request.Validator)
|
||||
return
|
||||
}
|
||||
|
||||
election, err := app.elections.GetById(request.ElectionId)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
app.unprocessableEntityErrorSingle(w, fmt.Errorf("election with id %v doesn't exist", request.ElectionId))
|
||||
return
|
||||
}
|
||||
app.serverError(w, r, err)
|
||||
}
|
||||
|
||||
// TODO check if he has voted
|
||||
|
||||
//var voterIdentity string
|
||||
if election.AreVotersKnown {
|
||||
if request.VoterIdentity == nil || validator.Blank(*request.VoterIdentity) {
|
||||
app.unprocessableEntityErrorSingle(w, fmt.Errorf("election has known voters; you must provide an identity provided by the organizer"))
|
||||
return
|
||||
}
|
||||
//voterIdentity = *request.VoterIdentity
|
||||
} else {
|
||||
// TODO: get requester's IP address as identity
|
||||
}
|
||||
|
||||
// TODO verify if choice exists
|
||||
// TODO count tokens to make sure user isn't trying to cheat
|
||||
|
||||
json, _ := json.Marshal(election)
|
||||
w.Write(json)
|
||||
}
|
||||
|
Reference in New Issue
Block a user