From c7cccf2ec124a24f7b44dd8bfe070e3842550375 Mon Sep 17 00:00:00 2001 From: dylan Date: Mon, 13 Jan 2025 11:26:18 +0100 Subject: [PATCH] Use generated interface --- cmd/web/handlers.go | 17 +++++++---------- cmd/web/helpers.go | 9 +++++++++ cmd/web/openapi.yml | 3 +-- cmd/web/routes.go | 7 +++++-- internal/generated.go | 2 +- 5 files changed, 23 insertions(+), 15 deletions(-) diff --git a/cmd/web/handlers.go b/cmd/web/handlers.go index 1d4d498..bbd7ec4 100644 --- a/cmd/web/handlers.go +++ b/cmd/web/handlers.go @@ -12,7 +12,6 @@ import ( "math/rand" "net/http" "slices" - "strconv" "time" ) @@ -60,7 +59,7 @@ func (r *createElectionRequestWithValidator) isValid() bool { return r.Valid() } -func (app *application) createElection(w http.ResponseWriter, r *http.Request) { +func (app *application) CreateElection(w http.ResponseWriter, r *http.Request) { var request createElectionRequestWithValidator if err := app.unmarshalRequest(r, &request); err != nil { @@ -131,7 +130,7 @@ func (r *createVotesRequestWithValidator) isValid() bool { return r.Valid() } -func (app *application) createVotes(w http.ResponseWriter, r *http.Request) { +func (app *application) CreateVotes(w http.ResponseWriter, r *http.Request, id int) { var request createVotesRequestWithValidator if err := app.unmarshalRequest(r, &request); err != nil { @@ -144,13 +143,7 @@ func (app *application) createVotes(w http.ResponseWriter, r *http.Request) { return } - 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) + election, err := app.elections.GetById(id) if err != nil { if errors.Is(err, sql.ErrNoRows) { app.clientError(w, http.StatusNotFound, "Couldn't find an election with the ID you provided") @@ -274,3 +267,7 @@ func (app *application) createVotesHandleUnknownVotersElection(w http.ResponseWr return voterIdentity, nil } + +func (app *application) GetElectionResults(w http.ResponseWriter, r *http.Request, id int) { + +} diff --git a/cmd/web/helpers.go b/cmd/web/helpers.go index ae1aedd..77970e5 100644 --- a/cmd/web/helpers.go +++ b/cmd/web/helpers.go @@ -19,6 +19,15 @@ func (app *application) serverError(w http.ResponseWriter, r *http.Request, err json.NewEncoder(w).Encode(response) } +func (app *application) badRequestError(w http.ResponseWriter, r *http.Request, err error) { + w.WriteHeader(http.StatusBadRequest) + var response = api.ErrorResponse{ + Code: http.StatusBadRequest, + Message: err.Error(), + } + json.NewEncoder(w).Encode(response) +} + func (app *application) clientError(w http.ResponseWriter, status int, message string) { w.WriteHeader(status) var response = api.ErrorResponse{ diff --git a/cmd/web/openapi.yml b/cmd/web/openapi.yml index 4432175..2634f71 100644 --- a/cmd/web/openapi.yml +++ b/cmd/web/openapi.yml @@ -315,5 +315,4 @@ components: description: Machine-readable error code details: type: object - description: Additional error details when available - nullable: true \ No newline at end of file + description: Additional error details when available \ No newline at end of file diff --git a/cmd/web/routes.go b/cmd/web/routes.go index 924b4ed..150704e 100644 --- a/cmd/web/routes.go +++ b/cmd/web/routes.go @@ -1,6 +1,7 @@ package main import ( + api "code.dlmw.ch/dlmw/qv/internal" "code.dlmw.ch/dlmw/qv/internal/models" "code.dlmw.ch/dlmw/qv/ui" "github.com/justinas/alice" @@ -21,8 +22,10 @@ func (app *application) routes() http.Handler { mux.Handle("GET /static/", http.FileServerFS(ui.Files)) mux.HandleFunc("GET /election/create", app.createElectionPage) - mux.HandleFunc("POST /election", app.createElection) - mux.HandleFunc("POST /election/{id}/votes", app.createVotes) + api.HandlerWithOptions(app, api.StdHTTPServerOptions{ + BaseRouter: mux, + ErrorHandlerFunc: app.badRequestError, + }) standard := alice.New(app.recoverPanic, app.logRequest) return standard.Then(mux) diff --git a/internal/generated.go b/internal/generated.go index 659d93f..8da6460 100644 --- a/internal/generated.go +++ b/internal/generated.go @@ -50,7 +50,7 @@ type ErrorResponse struct { Code int `json:"code"` // Details Additional error details when available - Details *map[string]interface{} `json:"details"` + Details *map[string]interface{} `json:"details,omitempty"` // Message Human-readable error message Message string `json:"message"`