Make isValid a method and add skeleton for createVote

This commit is contained in:
2025-01-02 17:49:23 +01:00
parent 1554743a95
commit f884e29ccd
2 changed files with 7 additions and 2 deletions

View File

@ -23,7 +23,7 @@ func (app *application) createElection(w http.ResponseWriter, r *http.Request) {
return return
} }
if !isRequestValid(&request) { if !request.isValid() {
app.unprocessableEntityError(w, request.Validator) app.unprocessableEntityError(w, request.Validator)
return return
} }
@ -64,7 +64,7 @@ func (app *application) createElection(w http.ResponseWriter, r *http.Request) {
w.Write(res) w.Write(res)
} }
func isRequestValid(r *createElectionRequestWithValidator) bool { func (r *createElectionRequestWithValidator) isValid() bool {
r.CheckField(validator.NotBlank(r.Name), "name", "must not be blank") r.CheckField(validator.NotBlank(r.Name), "name", "must not be blank")
r.CheckField(validator.GreaterThan(r.Tokens, 0), "tokens", "must be greater than 0") r.CheckField(validator.GreaterThan(r.Tokens, 0), "tokens", "must be greater than 0")
r.CheckField(validator.After(r.ExpiresAt, time.Now()), "expiresAt", "must expire in a future date") r.CheckField(validator.After(r.ExpiresAt, time.Now()), "expiresAt", "must expire in a future date")
@ -100,3 +100,7 @@ func randomVoterIdentity() string {
} }
return string(b) return string(b)
} }
func (app *application) createVote(w http.ResponseWriter, r *http.Request) {
}

View File

@ -17,6 +17,7 @@ func (app *application) routes() http.Handler {
mux := http.NewServeMux() mux := http.NewServeMux()
mux.HandleFunc("POST /election", app.createElection) mux.HandleFunc("POST /election", app.createElection)
mux.HandleFunc("POST /vote", app.createVote)
standard := alice.New(app.recoverPanic, app.logRequest) standard := alice.New(app.recoverPanic, app.logRequest)
return standard.Then(mux) return standard.Then(mux)