Move voting to a new endpoint
/election/{id}/votes
This commit is contained in:
@ -11,6 +11,7 @@ import (
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"slices"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -111,8 +112,6 @@ type createVotesRequestWithValidator struct {
|
||||
}
|
||||
|
||||
func (r *createVotesRequestWithValidator) isValid() bool {
|
||||
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")
|
||||
}
|
||||
@ -133,10 +132,16 @@ func (app *application) createVotes(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
election, err := app.elections.GetById(request.ElectionId) // get from path instead of from the JSON
|
||||
electionID, err := strconv.Atoi(r.PathValue("id"))
|
||||
if err != nil {
|
||||
app.clientError(w, http.StatusBadRequest, "Couldn't convert the id you provided to a number")
|
||||
return
|
||||
}
|
||||
|
||||
election, err := app.elections.GetById(electionID)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
app.unprocessableEntityErrorSingle(w, fmt.Errorf("election with id %v doesn't exist", request.ElectionId)) // TODO: return 404
|
||||
app.clientError(w, http.StatusNotFound, "Couldn't find an election with the ID you provided")
|
||||
return
|
||||
}
|
||||
app.serverError(w, r, err)
|
||||
@ -145,7 +150,7 @@ func (app *application) createVotes(w http.ResponseWriter, r *http.Request) {
|
||||
for _, c := range request.Choices {
|
||||
choiceExists := slices.Contains(election.Choices, c.ChoiceText)
|
||||
if !choiceExists {
|
||||
app.unprocessableEntityErrorSingle(w, fmt.Errorf("choice %v doesn't exist", c.ChoiceText))
|
||||
app.clientError(w, http.StatusUnprocessableEntity, fmt.Sprintf("choice %v doesn't exist", c.ChoiceText))
|
||||
return
|
||||
}
|
||||
}
|
||||
@ -156,13 +161,13 @@ func (app *application) createVotes(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
if tokensUsed > election.Tokens {
|
||||
app.unprocessableEntityErrorSingle(w, fmt.Errorf("you used too many tokens; must not exceed %v tokens", election.Tokens))
|
||||
app.clientError(w, http.StatusUnprocessableEntity, fmt.Sprintf("you used too many tokens; must not exceed %v tokens", election.Tokens))
|
||||
return
|
||||
}
|
||||
|
||||
electionHasExpired := election.ExpiresAt.Before(time.Now())
|
||||
if electionHasExpired {
|
||||
app.unprocessableEntityErrorSingle(w, fmt.Errorf("election has expired"))
|
||||
app.clientError(w, http.StatusUnprocessableEntity, "election has expired")
|
||||
return
|
||||
}
|
||||
|
||||
@ -193,9 +198,9 @@ func (app *application) createVotes(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func (app *application) createVotesHandleKnownVotersElection(w http.ResponseWriter, r *http.Request, request createVotesRequestWithValidator, election *models.Election) (string, error) {
|
||||
if request.VoterIdentity == nil || validator.Blank(*request.VoterIdentity) {
|
||||
err := fmt.Errorf("election has known voters; you must provide an identity provided by the organizer")
|
||||
app.unprocessableEntityErrorSingle(w, err)
|
||||
return "", err
|
||||
message := "election has known voters; you must provide an identity provided by the organizer"
|
||||
app.clientError(w, http.StatusUnprocessableEntity, message)
|
||||
return "", fmt.Errorf(message)
|
||||
}
|
||||
|
||||
voterIdentity := *request.VoterIdentity
|
||||
@ -205,9 +210,9 @@ func (app *application) createVotesHandleKnownVotersElection(w http.ResponseWrit
|
||||
return "", err
|
||||
}
|
||||
if hasCastVotes {
|
||||
err := fmt.Errorf("you already voted")
|
||||
app.unprocessableEntityErrorSingle(w, err)
|
||||
return "", err
|
||||
message := "you already voted"
|
||||
app.clientError(w, http.StatusUnprocessableEntity, message)
|
||||
return "", fmt.Errorf(message)
|
||||
}
|
||||
|
||||
voterExists, err := app.voters.Exists(voterIdentity, election.ID)
|
||||
@ -216,9 +221,9 @@ func (app *application) createVotesHandleKnownVotersElection(w http.ResponseWrit
|
||||
return "", err
|
||||
}
|
||||
if !voterExists {
|
||||
err := fmt.Errorf("invalid voter identity")
|
||||
app.unprocessableEntityErrorSingle(w, err)
|
||||
return "", err
|
||||
message := "invalid voter identity"
|
||||
app.clientError(w, http.StatusUnprocessableEntity, message)
|
||||
return "", fmt.Errorf(message)
|
||||
}
|
||||
|
||||
return voterIdentity, nil
|
||||
@ -233,9 +238,9 @@ func (app *application) createVotesHandleUnknownVotersElection(w http.ResponseWr
|
||||
return "", err
|
||||
}
|
||||
if voterExists {
|
||||
err := fmt.Errorf("you already voted")
|
||||
app.unprocessableEntityErrorSingle(w, err)
|
||||
return "", err
|
||||
message := "you already voted"
|
||||
app.clientError(w, http.StatusUnprocessableEntity, message)
|
||||
return "", fmt.Errorf(message)
|
||||
}
|
||||
|
||||
_, err = app.voters.Insert(voterIdentity, election.ID)
|
||||
@ -250,9 +255,9 @@ func (app *application) createVotesHandleUnknownVotersElection(w http.ResponseWr
|
||||
return "", err
|
||||
}
|
||||
if voterCount == election.MaxVoters {
|
||||
err := fmt.Errorf("maximum voters reached")
|
||||
app.unprocessableEntityErrorSingle(w, err)
|
||||
return "", err
|
||||
message := "maximum voters reached"
|
||||
app.clientError(w, http.StatusUnprocessableEntity, message)
|
||||
return "", fmt.Errorf(message)
|
||||
}
|
||||
|
||||
return voterIdentity, nil
|
||||
|
Reference in New Issue
Block a user