Compare commits

...

2 Commits

Author SHA1 Message Date
f884e29ccd Make isValid a method and add skeleton for createVote 2025-01-02 17:49:23 +01:00
1554743a95 Add "web" to .gitignore 2025-01-02 17:45:41 +01:00
3 changed files with 11 additions and 3 deletions

5
.gitignore vendored
View File

@ -26,4 +26,7 @@ go.work.sum
.idea .idea
*.iml *.iml
*.sqlite *.sqlite
# exclude built binary
web

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)