✨ Able to get own poll answer & poll answer percentage
This commit is contained in:
parent
429ca64f9d
commit
e16201a3ad
@ -71,6 +71,7 @@ func MapAPIs(app *fiber.App, baseURL string) {
|
|||||||
polls.Put("/:pollId", updatePoll)
|
polls.Put("/:pollId", updatePoll)
|
||||||
polls.Delete("/:pollId", deletePoll)
|
polls.Delete("/:pollId", deletePoll)
|
||||||
polls.Post("/:pollId/answer", answerPoll)
|
polls.Post("/:pollId/answer", answerPoll)
|
||||||
|
polls.Get("/:pollId/answer", getMyPollAnswer)
|
||||||
}
|
}
|
||||||
|
|
||||||
subscriptions := api.Group("/subscriptions").Name("Subscriptions API")
|
subscriptions := api.Group("/subscriptions").Name("Subscriptions API")
|
||||||
|
@ -12,6 +12,22 @@ import (
|
|||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func getMyPollAnswer(c *fiber.Ctx) error {
|
||||||
|
if err := sec.EnsureAuthenticated(c); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
user := c.Locals("user").(authm.Account)
|
||||||
|
|
||||||
|
pollId, _ := c.ParamsInt("pollId")
|
||||||
|
|
||||||
|
var answer models.PollAnswer
|
||||||
|
if err := database.C.Where("poll_id = ? AND account_id = ?", pollId, user.ID).First(&answer).Error; err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.JSON(answer)
|
||||||
|
}
|
||||||
|
|
||||||
func getPoll(c *fiber.Ctx) error {
|
func getPoll(c *fiber.Ctx) error {
|
||||||
pollId, _ := c.ParamsInt("pollId")
|
pollId, _ := c.ParamsInt("pollId")
|
||||||
|
|
||||||
|
@ -20,6 +20,7 @@ type Poll struct {
|
|||||||
type PollMetric struct {
|
type PollMetric struct {
|
||||||
TotalAnswer int64 `json:"total_answer"`
|
TotalAnswer int64 `json:"total_answer"`
|
||||||
ByOptions map[string]int64 `json:"by_options"`
|
ByOptions map[string]int64 `json:"by_options"`
|
||||||
|
ByOptionsPercentage map[string]float64 `json:"by_options_percentage"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type PollOption struct {
|
type PollOption struct {
|
||||||
|
@ -49,8 +49,14 @@ func GetPollMetric(poll models.Poll) models.PollMetric {
|
|||||||
byOptions[answer.Answer]++
|
byOptions[answer.Answer]++
|
||||||
}
|
}
|
||||||
|
|
||||||
|
byOptionsPercentage := make(map[string]float64)
|
||||||
|
for _, option := range poll.Options {
|
||||||
|
byOptionsPercentage[option.ID] = float64(byOptions[option.ID]) / float64(len(answers))
|
||||||
|
}
|
||||||
|
|
||||||
return models.PollMetric{
|
return models.PollMetric{
|
||||||
TotalAnswer: int64(len(answers)),
|
TotalAnswer: int64(len(answers)),
|
||||||
ByOptions: byOptions,
|
ByOptions: byOptions,
|
||||||
|
ByOptionsPercentage: byOptionsPercentage,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user