diff --git a/cmd/web/handlers.go b/cmd/web/handlers.go index d28717e..326c649 100644 --- a/cmd/web/handlers.go +++ b/cmd/web/handlers.go @@ -101,6 +101,11 @@ func randomVoterIdentity() string { return string(b) } +type createVoteRequestWithValidator struct { + api.CreateVotesRequest + validator.Validator +} + func (app *application) createVote(w http.ResponseWriter, r *http.Request) { } diff --git a/cmd/web/openapi.yml b/cmd/web/openapi.yml index 65bb22b..0225430 100644 --- a/cmd/web/openapi.yml +++ b/cmd/web/openapi.yml @@ -18,6 +18,8 @@ servers: tags: - name: election description: Retrieve data related to elections + - name: vote + description: Retrieve data related to votes paths: /election: @@ -50,6 +52,27 @@ paths: schema: $ref: "#/components/schemas/ErrorResponse" + /votes: + post: + tags: + - vote + summary: Cast your votes for an election + operationId: createVotes + requestBody: + content: + application/json: + schema: + $ref: "#/components/schemas/CreateVotesRequest" + responses: + 200: + description: Votes cast + 422: + description: Unprocessable content + content: + application/json: + schema: + $ref: "#/components/schemas/ErrorResponse" + components: schemas: Election: @@ -194,9 +217,27 @@ components: items: type: string minLength: 1 - maxItems: 1 uniqueItems: true + CreateVotesRequest: + type: object + properties: + voterIdentity: + type: string + minLength: 1 + electionId: + type: integer + choices: + type: array + items: + properties: + choiceText: + type: string + minLength: 1 + tokens: + type: integer + minimum: 1 + ErrorResponse: type: object required: diff --git a/internal/generated.go b/internal/generated.go index 5add100..c3f668f 100644 --- a/internal/generated.go +++ b/internal/generated.go @@ -28,6 +28,16 @@ type CreateElectionResponse struct { VoterIdentities *[]string `json:"voterIdentities,omitempty"` } +// CreateVotesRequest defines model for CreateVotesRequest. +type CreateVotesRequest struct { + Choices *[]struct { + ChoiceText *string `json:"choiceText,omitempty"` + Tokens *int `json:"tokens,omitempty"` + } `json:"choices,omitempty"` + ElectionId *int `json:"electionId,omitempty"` + VoterIdentity *string `json:"voterIdentity,omitempty"` +} + // ErrorResponse defines model for ErrorResponse. type ErrorResponse struct { // Code Machine-readable error code @@ -43,11 +53,17 @@ type ErrorResponse struct { // CreateElectionJSONRequestBody defines body for CreateElection for application/json ContentType. type CreateElectionJSONRequestBody = CreateElectionRequest +// CreateVotesJSONRequestBody defines body for CreateVotes for application/json ContentType. +type CreateVotesJSONRequestBody = CreateVotesRequest + // ServerInterface represents all server handlers. type ServerInterface interface { // Create a new election // (POST /election) CreateElection(w http.ResponseWriter, r *http.Request) + // Cast your votes for an election + // (POST /votes) + CreateVotes(w http.ResponseWriter, r *http.Request) } // ServerInterfaceWrapper converts contexts to parameters. @@ -74,6 +90,21 @@ func (siw *ServerInterfaceWrapper) CreateElection(w http.ResponseWriter, r *http handler.ServeHTTP(w, r.WithContext(ctx)) } +// CreateVotes operation middleware +func (siw *ServerInterfaceWrapper) CreateVotes(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + + handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + siw.Handler.CreateVotes(w, r) + })) + + for _, middleware := range siw.HandlerMiddlewares { + handler = middleware(handler) + } + + handler.ServeHTTP(w, r.WithContext(ctx)) +} + type UnescapedCookieParamError struct { ParamName string Err error @@ -189,6 +220,7 @@ func HandlerWithOptions(si ServerInterface, options StdHTTPServerOptions) http.H } m.HandleFunc("POST "+options.BaseURL+"/election", wrapper.CreateElection) + m.HandleFunc("POST "+options.BaseURL+"/votes", wrapper.CreateVotes) return m }