aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGalen Guyer <galen@galenguyer.com>2022-08-11 12:09:05 -0400
committerGalen Guyer <galen@galenguyer.com>2022-08-11 12:09:05 -0400
commitf1a69c4f8de41a57d2a880ee6b70693c82041c4c (patch)
tree3c363f72281c0d751cc9117d541e99ae7ec173e3
parentf3d7edb8ec60b3cae9a548a1177c132b52a51873 (diff)
Add write-in votes if the poll creator allows it
-rw-r--r--Dockerfile3
-rw-r--r--database/poll.go1
-rw-r--r--docker-compose.yaml4
-rw-r--r--main.go30
-rw-r--r--templates/create.tmpl8
-rw-r--r--templates/poll.tmpl12
6 files changed, 46 insertions, 12 deletions
diff --git a/Dockerfile b/Dockerfile
index 5fbb814..0cbd170 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,9 +1,10 @@
+# syntax = docker/dockerfile:1.2
FROM docker.io/golang:1.18.4-alpine3.16 AS build
WORKDIR /src/
RUN apk add git
COPY . .
-RUN go build -v -o vote
+RUN --mount=type=cache,target=/go go build -v -o vote
FROM docker.io/alpine:3.16
COPY static /static
diff --git a/database/poll.go b/database/poll.go
index a0b8378..3763656 100644
--- a/database/poll.go
+++ b/database/poll.go
@@ -17,6 +17,7 @@ type Poll struct {
Options []string `bson:"options"`
Open bool `bson:"open"`
Hidden bool `bson:"hidden"`
+ AllowWriteIns bool `bson:"writeins"`
}
type Result struct {
diff --git a/docker-compose.yaml b/docker-compose.yaml
index 12a6041..42d2346 100644
--- a/docker-compose.yaml
+++ b/docker-compose.yaml
@@ -25,8 +25,8 @@ services:
- "MONGO_INITDB_ROOT_PASSWORD=c1f66aac6b4fafbef3c659371b8a50ed"
volumes:
- type: volume
- source: mongo
+ source: mongodb
target: /data/db/
volumes:
- mongo:
+ mongodb:
diff --git a/main.go b/main.go
index 78919e1..baa186b 100644
--- a/main.go
+++ b/main.go
@@ -103,6 +103,7 @@ func main() {
LongDescription: c.PostForm("longDescription"),
Open: true,
Hidden: false,
+ AllowWriteIns: c.PostForm("allowWriteIn") == "true",
}
switch c.PostForm("options") {
@@ -159,6 +160,7 @@ func main() {
"ShortDescription": poll.ShortDescription,
"LongDescription": poll.LongDescription,
"Options": poll.Options,
+ "AllowWriteIns": poll.AllowWriteIns,
"Username": claims.UserInfo.Username,
"FullName": claims.UserInfo.FullName,
})
@@ -189,17 +191,27 @@ func main() {
return
}
- if !hasOption(poll, c.PostForm("option")) {
- c.JSON(500, gin.H{"error": err.Error()})
- }
+ var vote database.Vote
- vote := &database.Vote{
- Id: "",
- PollId: pId,
- UserId: claims.UserInfo.Username,
- Option: c.PostForm("option"),
+ if hasOption(poll, c.PostForm("option")) {
+ vote = database.Vote{
+ Id: "",
+ PollId: pId,
+ UserId: claims.UserInfo.Username,
+ Option: c.PostForm("option"),
+ }
+ } else if poll.AllowWriteIns && c.PostForm("option") == "writein" {
+ vote = database.Vote{
+ Id: "",
+ PollId: pId,
+ UserId: claims.UserInfo.Username,
+ Option: c.PostForm("writeinOption"),
+ }
+ } else {
+ c.JSON(500, gin.H{"error": "Invalid Option"})
+ return
}
- database.CastVote(vote)
+ database.CastVote(&vote)
if poll, err := database.GetPoll(c.Param("id")); err == nil {
if results, err := poll.GetResult(); err == nil {
diff --git a/templates/create.tmpl b/templates/create.tmpl
index 79f07b8..e835a64 100644
--- a/templates/create.tmpl
+++ b/templates/create.tmpl
@@ -56,6 +56,14 @@
placeholder="Custom Options (Comma-separated)"
/>
</div>
+ <div class="form-group">
+ <input
+ type="checkbox"
+ name="allowWriteIn"
+ value="true"
+ />
+ <span>Allow Write-In Votes</span>
+ </div>
<input type="submit" class="btn btn-primary" value="Create" />
</form>
</div>
diff --git a/templates/poll.tmpl b/templates/poll.tmpl
index d46130e..b81ee05 100644
--- a/templates/poll.tmpl
+++ b/templates/poll.tmpl
@@ -37,6 +37,18 @@
</div>
<br />
{{ end }}
+ {{ if .AllowWriteIns }}
+ <div class="form-check" style="display: flex;">
+ <input class="form-check-input" type="radio" name="option" value="writein" />
+ <input
+ type="text"
+ name="writeinOption"
+ class="form-control"
+ style="height: 1.5em;"
+ placeholder="Write-In"
+ />
+ </div>
+ {{ end }}
<br />
<button type="submit" class="btn btn-primary">Submit</button>
</form>