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)
|
||||
}
|
||||
|
@ -43,6 +43,15 @@ func (app *application) unprocessableEntityError(w http.ResponseWriter, v valida
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
func (app *application) unprocessableEntityErrorSingle(w http.ResponseWriter, err error) {
|
||||
w.WriteHeader(http.StatusUnprocessableEntity)
|
||||
var response = api.ErrorResponse{
|
||||
Code: http.StatusUnprocessableEntity,
|
||||
Message: err.Error(),
|
||||
}
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
func (app *application) unmarshalRequest(r *http.Request, dst any) error {
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
|
@ -221,15 +221,21 @@ components:
|
||||
|
||||
CreateVotesRequest:
|
||||
type: object
|
||||
required:
|
||||
- electionId
|
||||
- choices
|
||||
properties:
|
||||
voterIdentity:
|
||||
type: string
|
||||
minLength: 1
|
||||
description: Must be filled if election has known voters
|
||||
electionId:
|
||||
type: integer
|
||||
choices:
|
||||
type: array
|
||||
items:
|
||||
required:
|
||||
- choiceText
|
||||
- tokens
|
||||
properties:
|
||||
choiceText:
|
||||
type: string
|
||||
|
@ -17,7 +17,7 @@ func (app *application) routes() http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.HandleFunc("POST /election", app.createElection)
|
||||
mux.HandleFunc("POST /vote", app.createVote)
|
||||
mux.HandleFunc("POST /votes", app.createVotes)
|
||||
|
||||
standard := alice.New(app.recoverPanic, app.logRequest)
|
||||
return standard.Then(mux)
|
||||
|
@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"code.dlmw.ch/dlmw/qv/internal/models"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
@ -48,6 +49,18 @@ func (e *mockElectionModel) Insert(name string, tokens int, areVotersKnown bool,
|
||||
return 1, nil
|
||||
}
|
||||
|
||||
func (e *mockElectionModel) GetById(id int) (*models.Election, error) {
|
||||
return &models.Election{
|
||||
ID: id,
|
||||
Name: "Guy of the year",
|
||||
Tokens: 100,
|
||||
AreVotersKnown: false,
|
||||
MaxVoters: 10,
|
||||
CreatedAt: time.Now().String(),
|
||||
ExpiresAt: time.Now().Add(100 * time.Hour).String(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
type mockVoterModel struct {
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user