aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGalen Guyer <galen@galenguyer.com>2022-08-11 13:46:31 -0400
committerGalen Guyer <galen@galenguyer.com>2022-08-11 13:46:31 -0400
commit43c72d2700ab90dc80caff49a3fd1d16d1352581 (patch)
tree7fc8dac05357c68a6adb6aaf9093a327b7272e7a
parent58cf6ea82f3e9e3923ead4d4a39579b92072b7f9 (diff)
Save ranked votes into database
-rw-r--r--database/ranked_vote.go2
-rw-r--r--main.go29
2 files changed, 30 insertions, 1 deletions
diff --git a/database/ranked_vote.go b/database/ranked_vote.go
index 5263ede..ec29c32 100644
--- a/database/ranked_vote.go
+++ b/database/ranked_vote.go
@@ -11,7 +11,7 @@ type RankedVote struct {
Id string `bson:"_id,omitempty"`
PollId primitive.ObjectID `bson:"pollId"`
UserId string `bson:"userId"`
- Options map[string]int `bson:"option"`
+ Options map[string]int `bson:"options"`
}
func CastRankedVote(vote *RankedVote) error {
diff --git a/main.go b/main.go
index fdba12c..d395a6d 100644
--- a/main.go
+++ b/main.go
@@ -6,6 +6,7 @@ import (
"net/http"
"os"
"sort"
+ "strconv"
"strings"
"time"
@@ -242,6 +243,34 @@ func main() {
return
}
database.CastSimpleVote(&vote)
+ } else if poll.VoteType == database.POLL_TYPE_RANKED {
+ vote := database.RankedVote{
+ Id: "",
+ PollId: pId,
+ UserId: claims.UserInfo.Username,
+ Options: make(map[string]int),
+ }
+ for _, opt := range poll.Options {
+ if c.PostForm(opt) != "" {
+ rank, err := strconv.Atoi(c.PostForm(opt))
+ if err != nil {
+ c.JSON(500, gin.H{"error": "error parsing votes"})
+ }
+ if rank > 0 {
+ vote.Options[opt] = rank
+ }
+ }
+ }
+ if c.PostForm("writeinOption") != "" && c.PostForm("writein") != "" {
+ rank, err := strconv.Atoi(c.PostForm("writein"))
+ if err != nil {
+ c.JSON(500, gin.H{"error": "error parsing votes"})
+ }
+ if rank > 0 {
+ vote.Options[c.PostForm("writeinOption")] = rank
+ }
+ }
+ database.CastRankedVote(&vote)
} else {
c.JSON(500, gin.H{"error": "Unknown Poll Type"})
return