Make isValid a method and add skeleton for createVote
This commit is contained in:
@ -23,7 +23,7 @@ func (app *application) createElection(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if !isRequestValid(&request) {
|
||||
if !request.isValid() {
|
||||
app.unprocessableEntityError(w, request.Validator)
|
||||
return
|
||||
}
|
||||
@ -64,7 +64,7 @@ func (app *application) createElection(w http.ResponseWriter, r *http.Request) {
|
||||
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.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")
|
||||
@ -100,3 +100,7 @@ func randomVoterIdentity() string {
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func (app *application) createVote(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ func (app *application) routes() http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.HandleFunc("POST /election", app.createElection)
|
||||
mux.HandleFunc("POST /vote", app.createVote)
|
||||
|
||||
standard := alice.New(app.recoverPanic, app.logRequest)
|
||||
return standard.Then(mux)
|
||||
|
Reference in New Issue
Block a user