Implement page to vote and fix trigger
This commit is contained in:
@ -20,7 +20,18 @@ import (
|
||||
func (app *application) createElectionPage(w http.ResponseWriter, r *http.Request) {
|
||||
content, err := ui.Files.ReadFile("create-election.html")
|
||||
if err != nil {
|
||||
app.serverError(w, r, fmt.Errorf("couldn't read create-election.html"))
|
||||
app.serverError(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
w.Write(content)
|
||||
}
|
||||
|
||||
func (app *application) getElectionPage(w http.ResponseWriter, r *http.Request) {
|
||||
content, err := ui.Files.ReadFile("election.html")
|
||||
if err != nil {
|
||||
app.serverError(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
@ -253,23 +264,23 @@ func (app *application) createVotesHandleUnknownVotersElection(w http.ResponseWr
|
||||
return "", fmt.Errorf(message)
|
||||
}
|
||||
|
||||
_, err = app.voters.InsertMultiple([]string{voterIdentity}, election.ID)
|
||||
if err != nil {
|
||||
app.serverError(w, r, err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
voterCount, err := app.voters.CountByElection(election.ID)
|
||||
if err != nil && !errors.Is(sql.ErrNoRows, err) {
|
||||
app.serverError(w, r, err)
|
||||
return "", err
|
||||
}
|
||||
if voterCount == election.MaxVoters {
|
||||
if voterCount != 0 && voterCount == election.MaxVoters {
|
||||
message := "maximum voters reached"
|
||||
app.clientError(w, http.StatusUnprocessableEntity, message)
|
||||
return "", fmt.Errorf(message)
|
||||
}
|
||||
|
||||
_, err = app.voters.InsertMultiple([]string{voterIdentity}, election.ID)
|
||||
if err != nil {
|
||||
app.serverError(w, r, err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
return voterIdentity, nil
|
||||
}
|
||||
|
||||
|
@ -23,6 +23,7 @@ func (app *application) routes() http.Handler {
|
||||
mux.Handle("GET /static/", cached.Then(http.FileServerFS(ui.Files)))
|
||||
|
||||
mux.HandleFunc("GET /election/create", app.createElectionPage)
|
||||
mux.HandleFunc("GET /election/{id}", app.getElectionPage)
|
||||
|
||||
api.HandlerWithOptions(app, api.StdHTTPServerOptions{
|
||||
BaseRouter: mux,
|
||||
|
Reference in New Issue
Block a user