Paperclip/pkg/internal/server/api/boost_api.go

142 lines
3.9 KiB
Go
Raw Normal View History

2024-12-28 13:41:13 +00:00
package api
import (
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
"git.solsynth.dev/hypernet/paperclip/pkg/internal/database"
"git.solsynth.dev/hypernet/paperclip/pkg/internal/models"
"git.solsynth.dev/hypernet/paperclip/pkg/internal/server/exts"
"git.solsynth.dev/hypernet/paperclip/pkg/internal/services"
"github.com/gofiber/fiber/v2"
)
2024-12-28 14:14:30 +00:00
func listBoostByUser(c *fiber.Ctx) error {
user := c.Locals("nex_user").(*sec.UserInfo)
take := c.QueryInt("take", 0)
offset := c.QueryInt("offset", 0)
if take > 100 {
take = 100
}
count, err := services.CountBoostByUser(user.ID)
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
boosts, err := services.ListBoostByUser(user.ID, take, offset)
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
return c.JSON(fiber.Map{
"count": count,
"data": boosts,
})
}
func listBoostByAttachment(c *fiber.Ctx) error {
2024-12-28 14:08:45 +00:00
attachmentId, _ := c.ParamsInt("attachmentId", 0)
if boost, err := services.ListBoostByAttachment(uint(attachmentId)); err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else {
return c.JSON(boost)
}
}
2024-12-28 13:41:13 +00:00
func getBoost(c *fiber.Ctx) error {
2024-12-28 14:08:45 +00:00
boostId, _ := c.ParamsInt("boostId", 0)
2024-12-28 13:41:13 +00:00
2024-12-28 14:08:45 +00:00
if boost, err := services.GetBoostByID(uint(boostId)); err != nil {
2024-12-28 13:41:13 +00:00
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else {
return c.JSON(boost)
}
}
func createBoost(c *fiber.Ctx) error {
user := c.Locals("nex_user").(*sec.UserInfo)
var data struct {
Attachment uint `json:"attachment" validate:"required"`
Destination int `json:"destination" validate:"required"`
}
if err := exts.BindAndValidate(c, &data); err != nil {
return err
}
var attachment models.Attachment
if err := database.C.Where("id = ?", data.Attachment).First(&attachment).Error; err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
if boost, err := services.CreateBoost(user, attachment, data.Destination); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {
return c.JSON(boost)
}
}
2024-12-28 17:38:48 +00:00
func activateBoost(c *fiber.Ctx) error {
user := c.Locals("nex_user").(*sec.UserInfo)
boostId, _ := c.ParamsInt("boostId", 0)
boost, err := services.GetBoostByID(uint(boostId))
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else if boost.AccountID != user.ID {
return fiber.NewError(fiber.StatusNotFound, "record not created by you")
}
if err := services.ActivateBoost(boost); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {
return c.JSON(boost)
}
}
2024-12-28 13:41:13 +00:00
func updateBoost(c *fiber.Ctx) error {
user := c.Locals("nex_user").(*sec.UserInfo)
2024-12-28 14:08:45 +00:00
boostId, _ := c.ParamsInt("boostId", 0)
2024-12-28 13:41:13 +00:00
var data struct {
Status int `json:"status" validate:"required"`
}
if err := exts.BindAndValidate(c, &data); err != nil {
return err
}
2024-12-28 14:08:45 +00:00
boost, err := services.GetBoostByID(uint(boostId))
2024-12-28 13:41:13 +00:00
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else if boost.AccountID != user.ID {
return fiber.NewError(fiber.StatusNotFound, "record not created by you")
}
if boost, err := services.UpdateBoostStatus(boost, data.Status); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {
return c.JSON(boost)
}
}
func deleteBoost(c *fiber.Ctx) error {
user := c.Locals("nex_user").(*sec.UserInfo)
2024-12-28 14:08:45 +00:00
boostId, _ := c.ParamsInt("boostId", 0)
2024-12-28 13:41:13 +00:00
2024-12-28 14:08:45 +00:00
boost, err := services.GetBoostByID(uint(boostId))
2024-12-28 13:41:13 +00:00
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else if boost.AccountID != user.ID {
return fiber.NewError(fiber.StatusNotFound, "record not created by you")
}
if err := services.DeleteBoost(boost); err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else {
return c.SendStatus(fiber.StatusOK)
}
}