✨ Polls
This commit is contained in:
@ -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)
|
||||
}
|
Reference in New Issue
Block a user