Use generated interface
This commit is contained in:
@ -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) {
|
||||
|
||||
}
|
||||
|
@ -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{
|
||||
|
@ -315,5 +315,4 @@ components:
|
||||
description: Machine-readable error code
|
||||
details:
|
||||
type: object
|
||||
description: Additional error details when available
|
||||
nullable: true
|
||||
description: Additional error details when available
|
@ -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)
|
||||
|
Reference in New Issue
Block a user