Move voting to a new endpoint

/election/{id}/votes
This commit is contained in:
2025-01-11 18:29:01 +01:00
parent 86d7d0e881
commit cad5cfe636
8 changed files with 186 additions and 100 deletions

View File

@ -9,6 +9,8 @@ import (
"fmt"
"net/http"
"time"
"github.com/oapi-codegen/runtime"
)
// CreateElectionRequest defines model for CreateElectionRequest.
@ -34,12 +36,14 @@ type CreateVotesRequest struct {
ChoiceText string `json:"choiceText"`
Tokens int `json:"tokens"`
} `json:"choices"`
ElectionId int `json:"electionId"`
// VoterIdentity Must be filled if election has known voters
VoterIdentity *string `json:"voterIdentity,omitempty"`
}
// ElectionResultsResponse defines model for ElectionResultsResponse.
type ElectionResultsResponse = map[string]interface{}
// ErrorResponse defines model for ErrorResponse.
type ErrorResponse struct {
// Code Machine-readable error code
@ -63,9 +67,12 @@ type ServerInterface interface {
// Create a new election
// (POST /election)
CreateElection(w http.ResponseWriter, r *http.Request)
// Get the results of an election
// (GET /election/{id}/results)
GetElectionIdResults(w http.ResponseWriter, r *http.Request, id int)
// Cast your votes for an election
// (POST /votes)
CreateVotes(w http.ResponseWriter, r *http.Request)
// (POST /election/{id}/votes)
CreateVotes(w http.ResponseWriter, r *http.Request, id int)
}
// ServerInterfaceWrapper converts contexts to parameters.
@ -92,12 +99,49 @@ func (siw *ServerInterfaceWrapper) CreateElection(w http.ResponseWriter, r *http
handler.ServeHTTP(w, r.WithContext(ctx))
}
// GetElectionIdResults operation middleware
func (siw *ServerInterfaceWrapper) GetElectionIdResults(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
var err error
// ------------- Path parameter "id" -------------
var id int
err = runtime.BindStyledParameterWithOptions("simple", "id", r.PathValue("id"), &id, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationPath, Explode: false, Required: true})
if err != nil {
siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "id", Err: err})
return
}
handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
siw.Handler.GetElectionIdResults(w, r, id)
}))
for _, middleware := range siw.HandlerMiddlewares {
handler = middleware(handler)
}
handler.ServeHTTP(w, r.WithContext(ctx))
}
// CreateVotes operation middleware
func (siw *ServerInterfaceWrapper) CreateVotes(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
var err error
// ------------- Path parameter "id" -------------
var id int
err = runtime.BindStyledParameterWithOptions("simple", "id", r.PathValue("id"), &id, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationPath, Explode: false, Required: true})
if err != nil {
siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "id", Err: err})
return
}
handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
siw.Handler.CreateVotes(w, r)
siw.Handler.CreateVotes(w, r, id)
}))
for _, middleware := range siw.HandlerMiddlewares {
@ -222,7 +266,8 @@ func HandlerWithOptions(si ServerInterface, options StdHTTPServerOptions) http.H
}
m.HandleFunc("POST "+options.BaseURL+"/election", wrapper.CreateElection)
m.HandleFunc("POST "+options.BaseURL+"/votes", wrapper.CreateVotes)
m.HandleFunc("GET "+options.BaseURL+"/election/{id}/results", wrapper.GetElectionIdResults)
m.HandleFunc("POST "+options.BaseURL+"/election/{id}/votes", wrapper.CreateVotes)
return m
}