✨ Polls
This commit is contained in:
@ -12,6 +12,8 @@ var AutoMaintainRange = []any{
|
||||
&models.Post{},
|
||||
&models.PostInsight{},
|
||||
&models.Subscription{},
|
||||
&models.Poll{},
|
||||
&models.PollOption{},
|
||||
}
|
||||
|
||||
func RunMigration(source *gorm.DB) error {
|
||||
|
@ -64,6 +64,15 @@ func MapAPIs(app *fiber.App, baseURL string) {
|
||||
posts.Get("/:postId/replies/featured", listPostFeaturedReply)
|
||||
}
|
||||
|
||||
polls := api.Group("/polls").Name("Polls API")
|
||||
{
|
||||
polls.Get("/:pollId", getPoll)
|
||||
polls.Post("/", createPoll)
|
||||
polls.Put("/:pollId", updatePoll)
|
||||
polls.Delete("/:pollId", deletePoll)
|
||||
polls.Post("/:pollId/answer", answerPoll)
|
||||
}
|
||||
|
||||
subscriptions := api.Group("/subscriptions").Name("Subscriptions API")
|
||||
{
|
||||
subscriptions.Get("/users/:userId", getSubscriptionOnUser)
|
||||
|
62
pkg/internal/http/api/poll_answers_api.go
Normal file
62
pkg/internal/http/api/poll_answers_api.go
Normal file
@ -0,0 +1,62 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"git.solsynth.dev/hypernet/interactive/pkg/internal/database"
|
||||
"git.solsynth.dev/hypernet/interactive/pkg/internal/http/exts"
|
||||
"git.solsynth.dev/hypernet/interactive/pkg/internal/models"
|
||||
"git.solsynth.dev/hypernet/interactive/pkg/internal/services"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
||||
authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func answerPoll(c *fiber.Ctx) error {
|
||||
pollId, _ := c.ParamsInt("pollId")
|
||||
|
||||
if err := sec.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(authm.Account)
|
||||
|
||||
var data struct {
|
||||
Answer string `json:"answer" validate:"required"`
|
||||
}
|
||||
|
||||
if err := exts.BindAndValidate(c, &data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var poll models.Poll
|
||||
if err := database.C.Where("id = ?", pollId).First(&poll).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
if poll.ExpiredAt != nil && time.Now().Unix() >= poll.ExpiredAt.Unix() {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "poll has been ended")
|
||||
}
|
||||
|
||||
doesContains := false
|
||||
for _, option := range poll.Options {
|
||||
if option.ID == data.Answer {
|
||||
doesContains = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !doesContains {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "poll does not have a option like that")
|
||||
}
|
||||
|
||||
answer := models.PollAnswer{
|
||||
Answer: data.Answer,
|
||||
PollID: poll.ID,
|
||||
AccountID: user.ID,
|
||||
}
|
||||
|
||||
if answer, err := services.AddPollAnswer(poll, answer); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
} else {
|
||||
return c.JSON(answer)
|
||||
}
|
||||
}
|
108
pkg/internal/http/api/polls_api.go
Normal file
108
pkg/internal/http/api/polls_api.go
Normal file
@ -0,0 +1,108 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"git.solsynth.dev/hypernet/interactive/pkg/internal/database"
|
||||
"git.solsynth.dev/hypernet/interactive/pkg/internal/http/exts"
|
||||
"git.solsynth.dev/hypernet/interactive/pkg/internal/models"
|
||||
"git.solsynth.dev/hypernet/interactive/pkg/internal/services"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
||||
authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func getPoll(c *fiber.Ctx) error {
|
||||
pollId, _ := c.ParamsInt("pollId")
|
||||
|
||||
var poll models.Poll
|
||||
if err := database.C.Where("id = ?", pollId).First(&poll).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
}
|
||||
|
||||
poll.Metric = services.GetPollMetric(poll)
|
||||
|
||||
return c.JSON(poll)
|
||||
}
|
||||
|
||||
func createPoll(c *fiber.Ctx) error {
|
||||
if err := sec.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(authm.Account)
|
||||
|
||||
var data struct {
|
||||
Options []models.PollOption `json:"options" validate:"required"`
|
||||
ExpiredAt *time.Time `json:"expired_at"`
|
||||
}
|
||||
|
||||
if err := exts.BindAndValidate(c, &data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
poll := models.Poll{
|
||||
ExpiredAt: data.ExpiredAt,
|
||||
Options: data.Options,
|
||||
AccountID: user.ID,
|
||||
}
|
||||
|
||||
var err error
|
||||
if poll, err = services.NewPoll(poll); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(poll)
|
||||
}
|
||||
|
||||
func updatePoll(c *fiber.Ctx) error {
|
||||
pollId, _ := c.ParamsInt("pollId")
|
||||
|
||||
if err := sec.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(authm.Account)
|
||||
|
||||
var data struct {
|
||||
Options []models.PollOption `json:"options" validate:"required"`
|
||||
ExpiredAt *time.Time `json:"expired_at"`
|
||||
}
|
||||
|
||||
if err := exts.BindAndValidate(c, &data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var poll models.Poll
|
||||
if err := database.C.Where("id = ? AND account_id = ?", pollId, user.ID).First(&poll).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
}
|
||||
|
||||
poll.Options = data.Options
|
||||
poll.ExpiredAt = data.ExpiredAt
|
||||
|
||||
var err error
|
||||
if poll, err = services.UpdatePoll(poll); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(poll)
|
||||
}
|
||||
|
||||
func deletePoll(c *fiber.Ctx) error {
|
||||
pollId, _ := c.ParamsInt("pollId")
|
||||
|
||||
if err := sec.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(authm.Account)
|
||||
|
||||
var poll models.Poll
|
||||
if err := database.C.Where("id = ? AND account_id = ?", pollId, user.ID).First(&poll).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
}
|
||||
|
||||
if err := database.C.Delete(&poll).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(poll)
|
||||
}
|
38
pkg/internal/models/polls.go
Normal file
38
pkg/internal/models/polls.go
Normal file
@ -0,0 +1,38 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/cruda"
|
||||
"gorm.io/datatypes"
|
||||
)
|
||||
|
||||
type Poll struct {
|
||||
cruda.BaseModel
|
||||
|
||||
ExpiredAt *time.Time `json:"expired_at"`
|
||||
Options datatypes.JSONSlice[PollOption] `json:"options"`
|
||||
AccountID uint `json:"account_id"`
|
||||
|
||||
Metric PollMetric `json:"metric" gorm:"-"`
|
||||
}
|
||||
|
||||
type PollMetric struct {
|
||||
TotalAnswer int64 `json:"total_answer"`
|
||||
ByOptions map[string]int64 `json:"by_options"`
|
||||
}
|
||||
|
||||
type PollOption struct {
|
||||
ID string `json:"id"`
|
||||
Icon string `json:"icon"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
type PollAnswer struct {
|
||||
cruda.BaseModel
|
||||
|
||||
Answer string `json:"answer"`
|
||||
PollID uint `json:"poll_id"`
|
||||
AccountID uint `json:"account_id"`
|
||||
}
|
56
pkg/internal/services/polls.go
Normal file
56
pkg/internal/services/polls.go
Normal file
@ -0,0 +1,56 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.solsynth.dev/hypernet/interactive/pkg/internal/database"
|
||||
"git.solsynth.dev/hypernet/interactive/pkg/internal/models"
|
||||
)
|
||||
|
||||
func NewPoll(poll models.Poll) (models.Poll, error) {
|
||||
if err := database.C.Create(&poll).Error; err != nil {
|
||||
return poll, err
|
||||
}
|
||||
return poll, nil
|
||||
}
|
||||
|
||||
func UpdatePoll(poll models.Poll) (models.Poll, error) {
|
||||
if err := database.C.Save(&poll).Error; err != nil {
|
||||
return poll, err
|
||||
}
|
||||
return poll, nil
|
||||
}
|
||||
|
||||
func AddPollAnswer(poll models.Poll, answer models.PollAnswer) (models.PollAnswer, error) {
|
||||
answer.PollID = poll.ID
|
||||
|
||||
var count int64
|
||||
if err := database.C.Model(&models.PollAnswer{}).Where("poll_id = ? AND account_id = ?", poll.ID, answer.AccountID).Count(&count).Error; err != nil {
|
||||
return answer, fmt.Errorf("you already answered the poll")
|
||||
}
|
||||
if err := database.C.Create(&answer).Error; err != nil {
|
||||
return answer, err
|
||||
}
|
||||
|
||||
return answer, nil
|
||||
}
|
||||
|
||||
func GetPollMetric(poll models.Poll) models.PollMetric {
|
||||
var answers []models.PollAnswer
|
||||
if err := database.C.Where("poll_id = ?", poll.ID).Find(&answers); err != nil {
|
||||
return models.PollMetric{}
|
||||
}
|
||||
|
||||
byOptions := make(map[string]int64)
|
||||
for _, answer := range answers {
|
||||
if _, ok := byOptions[answer.Answer]; !ok {
|
||||
byOptions[answer.Answer] = 0
|
||||
}
|
||||
byOptions[answer.Answer]++
|
||||
}
|
||||
|
||||
return models.PollMetric{
|
||||
TotalAnswer: int64(len(answers)),
|
||||
ByOptions: byOptions,
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user