2025-02-12 19:29:00 +01:00
|
|
|
package apis
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
"planner/core"
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func getPoll(c *gin.Context) {
|
|
|
|
|
user := extractUser(db, c)
|
|
|
|
|
if user == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var params struct {
|
|
|
|
|
PollId uint `uri:"poll_id"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bind_result := c.BindUri(¶ms)
|
|
|
|
|
if bind_result != nil {
|
|
|
|
|
fmt.Println(bind_result)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
fmt.Println(params)
|
|
|
|
|
|
2025-04-16 23:24:23 +02:00
|
|
|
poll, _ := core.PollGet(db, *user, params.PollId)
|
2025-02-12 19:29:00 +01:00
|
|
|
c.JSON(http.StatusOK, poll)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getPollVotes(c *gin.Context) {
|
|
|
|
|
user := extractUser(db, c)
|
|
|
|
|
if user == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var params struct {
|
|
|
|
|
PollId uint `uri:"poll_id"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bind_result := c.BindUri(¶ms)
|
|
|
|
|
if bind_result != nil {
|
|
|
|
|
fmt.Println(bind_result)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-16 23:24:23 +02:00
|
|
|
votes, err := core.VotesList(db, int(params.PollId))
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.String(http.StatusInternalServerError, err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
c.JSON(http.StatusOK, votes)
|
2025-02-12 19:29:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func pollVote(c *gin.Context) {
|
|
|
|
|
user := extractUser(db, c)
|
|
|
|
|
if user == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var path_params struct {
|
|
|
|
|
PollId uint `uri:"poll_id"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bind_result := c.BindUri(&path_params)
|
|
|
|
|
if bind_result != nil {
|
|
|
|
|
fmt.Println(bind_result)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-16 23:24:23 +02:00
|
|
|
poll, err := core.PollGet(db, *user, path_params.PollId)
|
2025-02-12 19:29:00 +01:00
|
|
|
if err != nil {
|
|
|
|
|
c.String(http.StatusInternalServerError, err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var vote_params struct {
|
|
|
|
|
Vote string `json:"vote"`
|
|
|
|
|
}
|
|
|
|
|
c.Bind(&vote_params)
|
|
|
|
|
|
2025-04-16 23:24:23 +02:00
|
|
|
plan, err := core.PlanGet(db, poll.PlanId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.String(http.StatusInternalServerError, "Unable to find plan: "+err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
member, err := user.GetMemberFromPlan(db, *plan)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.String(http.StatusInternalServerError, "Unable to find member: "+err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := poll.SetVote(db, *member, vote_params.Vote); err != nil {
|
2025-02-12 19:29:00 +01:00
|
|
|
c.String(http.StatusBadRequest, err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.Status(http.StatusOK)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func bindPollAPIs(r *gin.Engine) {
|
|
|
|
|
r.GET("/polls/:poll_id", getPoll)
|
|
|
|
|
r.GET("/polls/:poll_id/votes", getPollVotes)
|
|
|
|
|
r.POST("/polls/:poll_id/votes", pollVote)
|
|
|
|
|
}
|