From 27d166dad62e8b2fe3e5a9866a4a31612b844050 Mon Sep 17 00:00:00 2001 From: dylan Date: Fri, 17 Jan 2025 16:48:38 +0100 Subject: [PATCH] Implement page to vote and fix trigger --- cmd/web/handlers.go | 27 +++-- cmd/web/routes.go | 1 + internal/migrations/sql/000001_init.up.sql | 2 +- ui/election.html | 124 +++++++++++++++++++++ 4 files changed, 145 insertions(+), 9 deletions(-) create mode 100644 ui/election.html diff --git a/cmd/web/handlers.go b/cmd/web/handlers.go index e355fde..3eafc49 100644 --- a/cmd/web/handlers.go +++ b/cmd/web/handlers.go @@ -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 } diff --git a/cmd/web/routes.go b/cmd/web/routes.go index 62acedd..b303c93 100644 --- a/cmd/web/routes.go +++ b/cmd/web/routes.go @@ -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, diff --git a/internal/migrations/sql/000001_init.up.sql b/internal/migrations/sql/000001_init.up.sql index 0579444..b9373d0 100644 --- a/internal/migrations/sql/000001_init.up.sql +++ b/internal/migrations/sql/000001_init.up.sql @@ -35,7 +35,7 @@ CREATE TRIGGER enforce_max_voters SELECT 1 FROM elections e WHERE e.id = NEW.election_id - AND e.max_voters IS NOT NULL + AND e.max_voters != 0 ) BEGIN SELECT CASE diff --git a/ui/election.html b/ui/election.html new file mode 100644 index 0000000..a6b10f4 --- /dev/null +++ b/ui/election.html @@ -0,0 +1,124 @@ + + + + + + Vote in Election + + + + +
+
+

+ +
+ + + + + + + + + + +
+ +
+

+
+
+
+ + + +