Small refactor
This commit is contained in:
@ -16,29 +16,25 @@ type createElectionRequestWithValidator struct {
|
|||||||
|
|
||||||
func (app *application) createElection(w http.ResponseWriter, r *http.Request) {
|
func (app *application) createElection(w http.ResponseWriter, r *http.Request) {
|
||||||
var request createElectionRequestWithValidator
|
var request createElectionRequestWithValidator
|
||||||
err := app.unmarshalRequest(r, &request)
|
|
||||||
if err != nil {
|
if err := app.unmarshalRequest(r, &request); err != nil {
|
||||||
app.clientError(w, http.StatusBadRequest, err.Error())
|
app.clientError(w, http.StatusBadRequest, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
request.CheckField(validator.NotBlank(request.Name), "name", "must not be blank")
|
if !isRequestValid(&request) {
|
||||||
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.AreVotersKnown && request.MaxVoters < 1),
|
|
||||||
"maxVoters",
|
|
||||||
"must be greater than 0 when voters are known",
|
|
||||||
)
|
|
||||||
|
|
||||||
if !request.Valid() {
|
|
||||||
app.unprocessableEntityError(w, request.Validator)
|
app.unprocessableEntityError(w, request.Validator)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
electionId, err := app.elections.Insert(request.Name, request.Tokens, request.AreVotersKnown, request.MaxVoters, request.Choices, request.ExpiresAt)
|
electionId, err := app.elections.Insert(
|
||||||
|
request.Name,
|
||||||
|
request.Tokens,
|
||||||
|
request.AreVotersKnown,
|
||||||
|
request.MaxVoters,
|
||||||
|
request.Choices,
|
||||||
|
request.ExpiresAt,
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.serverError(w, r, err)
|
app.serverError(w, r, err)
|
||||||
return
|
return
|
||||||
@ -66,6 +62,21 @@ func (app *application) createElection(w http.ResponseWriter, r *http.Request) {
|
|||||||
w.Write(res)
|
w.Write(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isRequestValid(r *createElectionRequestWithValidator) 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")
|
||||||
|
r.CheckField(validator.GreaterThan(len(r.Choices), 1), "choices", "there must be more than 1 choice")
|
||||||
|
|
||||||
|
r.CheckField(
|
||||||
|
!(r.AreVotersKnown && r.MaxVoters < 1),
|
||||||
|
"maxVoters",
|
||||||
|
"must be greater than 0 when voters are known",
|
||||||
|
)
|
||||||
|
|
||||||
|
return r.Valid()
|
||||||
|
}
|
||||||
|
|
||||||
func generateRandomVoterIdentity() string {
|
func generateRandomVoterIdentity() string {
|
||||||
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
||||||
b := make([]byte, 16)
|
b := make([]byte, 16)
|
||||||
|
Reference in New Issue
Block a user