Use generated interface

This commit is contained in:
2025-01-13 11:26:18 +01:00
parent c3563878b7
commit c7cccf2ec1
5 changed files with 23 additions and 15 deletions

View File

@ -12,7 +12,6 @@ import (
"math/rand" "math/rand"
"net/http" "net/http"
"slices" "slices"
"strconv"
"time" "time"
) )
@ -60,7 +59,7 @@ func (r *createElectionRequestWithValidator) isValid() bool {
return r.Valid() 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 var request createElectionRequestWithValidator
if err := app.unmarshalRequest(r, &request); err != nil { if err := app.unmarshalRequest(r, &request); err != nil {
@ -131,7 +130,7 @@ func (r *createVotesRequestWithValidator) isValid() bool {
return r.Valid() 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 var request createVotesRequestWithValidator
if err := app.unmarshalRequest(r, &request); err != nil { if err := app.unmarshalRequest(r, &request); err != nil {
@ -144,13 +143,7 @@ func (app *application) createVotes(w http.ResponseWriter, r *http.Request) {
return return
} }
electionID, err := strconv.Atoi(r.PathValue("id")) election, err := app.elections.GetById(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)
if err != nil { if err != nil {
if errors.Is(err, sql.ErrNoRows) { if errors.Is(err, sql.ErrNoRows) {
app.clientError(w, http.StatusNotFound, "Couldn't find an election with the ID you provided") 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 return voterIdentity, nil
} }
func (app *application) GetElectionResults(w http.ResponseWriter, r *http.Request, id int) {
}

View File

@ -19,6 +19,15 @@ func (app *application) serverError(w http.ResponseWriter, r *http.Request, err
json.NewEncoder(w).Encode(response) 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) { func (app *application) clientError(w http.ResponseWriter, status int, message string) {
w.WriteHeader(status) w.WriteHeader(status)
var response = api.ErrorResponse{ var response = api.ErrorResponse{

View File

@ -315,5 +315,4 @@ components:
description: Machine-readable error code description: Machine-readable error code
details: details:
type: object type: object
description: Additional error details when available description: Additional error details when available
nullable: true

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
api "code.dlmw.ch/dlmw/qv/internal"
"code.dlmw.ch/dlmw/qv/internal/models" "code.dlmw.ch/dlmw/qv/internal/models"
"code.dlmw.ch/dlmw/qv/ui" "code.dlmw.ch/dlmw/qv/ui"
"github.com/justinas/alice" "github.com/justinas/alice"
@ -21,8 +22,10 @@ func (app *application) routes() http.Handler {
mux.Handle("GET /static/", http.FileServerFS(ui.Files)) mux.Handle("GET /static/", http.FileServerFS(ui.Files))
mux.HandleFunc("GET /election/create", app.createElectionPage) mux.HandleFunc("GET /election/create", app.createElectionPage)
mux.HandleFunc("POST /election", app.createElection) api.HandlerWithOptions(app, api.StdHTTPServerOptions{
mux.HandleFunc("POST /election/{id}/votes", app.createVotes) BaseRouter: mux,
ErrorHandlerFunc: app.badRequestError,
})
standard := alice.New(app.recoverPanic, app.logRequest) standard := alice.New(app.recoverPanic, app.logRequest)
return standard.Then(mux) return standard.Then(mux)

View File

@ -50,7 +50,7 @@ type ErrorResponse struct {
Code int `json:"code"` Code int `json:"code"`
// Details Additional error details when available // 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 Human-readable error message
Message string `json:"message"` Message string `json:"message"`