Write logic for createElection validation
This commit is contained in:
@ -1,7 +1,40 @@
|
||||
package main
|
||||
|
||||
import "net/http"
|
||||
import (
|
||||
api "code.dlmw.ch/dlmw/qv/internal"
|
||||
"code.dlmw.ch/dlmw/qv/internal/validator"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type createElectionRequestWithValidator struct {
|
||||
api.CreateElectionRequest
|
||||
validator.Validator
|
||||
}
|
||||
|
||||
func (app *application) createElection(w http.ResponseWriter, r *http.Request) {
|
||||
var request createElectionRequestWithValidator
|
||||
err := app.decodePostForm(r, &request)
|
||||
if err != nil {
|
||||
app.clientError(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
request.CheckField(validator.NotBlank(request.Name), "name", "must not be blank")
|
||||
request.CheckField(validator.GreaterThan(request.Tokens, 0), "tokens", "must be greater than 0")
|
||||
request.CheckField(validator.After(request.ExpiresAt, time.Now()), "expiresAt", "must expire in a future date")
|
||||
request.CheckField(validator.GreaterThan(len(request.Choices), 1), "choices", "there must be more than 1 choice")
|
||||
|
||||
request.CheckField(
|
||||
!request.IsAnonymous || (request.IsAnonymous && *request.MaxVoters > 0),
|
||||
"maxVoters",
|
||||
"must be greater than 0 for anonymous elections",
|
||||
)
|
||||
|
||||
if !request.Valid() {
|
||||
app.unprocessableEntityError(w, request.Validator)
|
||||
return
|
||||
}
|
||||
|
||||
w.Write([]byte("TODO"))
|
||||
}
|
||||
|
@ -1,12 +1,63 @@
|
||||
package main
|
||||
|
||||
import "net/http"
|
||||
import (
|
||||
api "code.dlmw.ch/dlmw/qv/internal"
|
||||
"code.dlmw.ch/dlmw/qv/internal/validator"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/go-playground/form/v4"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (app *application) serverError(w http.ResponseWriter, r *http.Request, err error) {
|
||||
var (
|
||||
method = r.Method
|
||||
uri = r.URL.RequestURI()
|
||||
)
|
||||
app.logger.Error(err.Error(), "method", method, "uri", uri)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
var response = api.ErrorResponse{
|
||||
Code: http.StatusInternalServerError,
|
||||
Details: nil,
|
||||
Message: "There was an error in the request",
|
||||
}
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
func (app *application) clientError(w http.ResponseWriter, status int, message string) {
|
||||
w.WriteHeader(status)
|
||||
var response = api.ErrorResponse{
|
||||
Code: http.StatusUnprocessableEntity,
|
||||
Details: &map[string]interface{}{
|
||||
"error": message,
|
||||
},
|
||||
Message: "There was an error in the request",
|
||||
}
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
func (app *application) unprocessableEntityError(w http.ResponseWriter, v validator.Validator) {
|
||||
w.WriteHeader(http.StatusUnprocessableEntity)
|
||||
var response = api.ErrorResponse{
|
||||
Code: http.StatusUnprocessableEntity,
|
||||
Details: &map[string]interface{}{
|
||||
"fields": v.FieldErrors,
|
||||
},
|
||||
Message: "Election data is invalid",
|
||||
}
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
func (app *application) decodePostForm(r *http.Request, dst any) error {
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = app.formDecoder.Decode(dst, r.PostForm)
|
||||
if err != nil {
|
||||
var invalidDecoderError *form.InvalidDecoderError
|
||||
if errors.As(err, &invalidDecoderError) {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
//go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config=oapi-codegen.yml openapi.yml
|
||||
//go:generate oapi-codegen --config=oapi-codegen.yml openapi.yml
|
||||
|
||||
package main
|
||||
|
||||
|
@ -34,8 +34,8 @@ paths:
|
||||
responses:
|
||||
201:
|
||||
description: Election created
|
||||
400:
|
||||
description: Bad request
|
||||
422:
|
||||
description: Unprocessable Content
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
@ -187,7 +187,7 @@ components:
|
||||
type: string
|
||||
description: Human-readable error message
|
||||
code:
|
||||
type: string
|
||||
type: integer
|
||||
description: Machine-readable error code
|
||||
details:
|
||||
type: object
|
||||
|
Reference in New Issue
Block a user