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) poll, _ := core.PollGet(db, *user, params.PollId) 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 } votes, err := core.VotesList(db, int(params.PollId)) if err != nil { c.String(http.StatusInternalServerError, err.Error()) return } c.JSON(http.StatusOK, votes) } 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 } poll, err := core.PollGet(db, *user, path_params.PollId) if err != nil { c.String(http.StatusInternalServerError, err.Error()) return } var vote_params struct { Vote string `json:"vote"` } c.Bind(&vote_params) 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 { 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) }