Add some more tests and implement getElection

This commit is contained in:
2025-01-17 14:49:51 +01:00
parent 60d1bb382c
commit 410e8f39d3
6 changed files with 236 additions and 1 deletions

View File

@ -41,6 +41,18 @@ type CreateVotesRequest struct {
VoterIdentity *string `json:"voterIdentity,omitempty"`
}
// Election defines model for Election.
type Election struct {
AreVotersKnown bool `json:"areVotersKnown"`
Choices []string `json:"choices"`
CreatedAt string `json:"createdAt"`
ExpiresAt string `json:"expiresAt"`
Id int `json:"id"`
MaxVoters int `json:"maxVoters"`
Name string `json:"name"`
Tokens int `json:"tokens"`
}
// ElectionResultsResponse defines model for ElectionResultsResponse.
type ElectionResultsResponse struct {
Results *[]VotesForChoice `json:"results,omitempty"`
@ -75,6 +87,9 @@ type ServerInterface interface {
// Create a new election
// (POST /election)
CreateElection(w http.ResponseWriter, r *http.Request)
// Get an election
// (GET /election/{id})
GetElection(w http.ResponseWriter, r *http.Request, id int)
// Get the results of an election
// (GET /election/{id}/results)
GetElectionResults(w http.ResponseWriter, r *http.Request, id int)
@ -107,6 +122,32 @@ func (siw *ServerInterfaceWrapper) CreateElection(w http.ResponseWriter, r *http
handler.ServeHTTP(w, r.WithContext(ctx))
}
// GetElection operation middleware
func (siw *ServerInterfaceWrapper) GetElection(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.GetElection(w, r, id)
}))
for _, middleware := range siw.HandlerMiddlewares {
handler = middleware(handler)
}
handler.ServeHTTP(w, r.WithContext(ctx))
}
// GetElectionResults operation middleware
func (siw *ServerInterfaceWrapper) GetElectionResults(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
@ -274,6 +315,7 @@ func HandlerWithOptions(si ServerInterface, options StdHTTPServerOptions) http.H
}
m.HandleFunc("POST "+options.BaseURL+"/election", wrapper.CreateElection)
m.HandleFunc("GET "+options.BaseURL+"/election/{id}", wrapper.GetElection)
m.HandleFunc("GET "+options.BaseURL+"/election/{id}/results", wrapper.GetElectionResults)
m.HandleFunc("POST "+options.BaseURL+"/election/{id}/votes", wrapper.CreateVotes)