101 lines
2.5 KiB
Go
101 lines
2.5 KiB
Go
|
package api
|
||
|
|
||
|
import (
|
||
|
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/gap"
|
||
|
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/models"
|
||
|
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/server/exts"
|
||
|
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/services"
|
||
|
"github.com/gofiber/fiber/v2"
|
||
|
)
|
||
|
|
||
|
func listStickerPacks(c *fiber.Ctx) error {
|
||
|
take := c.QueryInt("take", 0)
|
||
|
offset := c.QueryInt("offset", 0)
|
||
|
|
||
|
if take > 100 {
|
||
|
take = 100
|
||
|
}
|
||
|
|
||
|
stickers, err := services.ListStickerPackWithStickers(take, offset)
|
||
|
if err != nil {
|
||
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||
|
}
|
||
|
return c.JSON(stickers)
|
||
|
}
|
||
|
|
||
|
func createStickerPack(c *fiber.Ctx) error {
|
||
|
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
user := c.Locals("user").(models.Account)
|
||
|
|
||
|
var data struct {
|
||
|
Prefix string `json:"prefix" validate:"required,alphanum,min=2,max=12"`
|
||
|
Name string `json:"name" validate:"required"`
|
||
|
Description string `json:"description"`
|
||
|
}
|
||
|
|
||
|
if err := exts.BindAndValidate(c, &data); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
pack, err := services.NewStickerPack(user, data.Prefix, data.Name, data.Description)
|
||
|
if err != nil {
|
||
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||
|
}
|
||
|
|
||
|
return c.JSON(pack)
|
||
|
}
|
||
|
|
||
|
func updateStickerPack(c *fiber.Ctx) error {
|
||
|
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
user := c.Locals("user").(models.Account)
|
||
|
|
||
|
var data struct {
|
||
|
Prefix string `json:"prefix" validate:"required,alphanum,min=2,max=12"`
|
||
|
Name string `json:"name" validate:"required"`
|
||
|
Description string `json:"description"`
|
||
|
}
|
||
|
|
||
|
if err := exts.BindAndValidate(c, &data); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
id, _ := c.ParamsInt("packId", 0)
|
||
|
pack, err := services.GetStickerPackWithUser(uint(id), user.ID)
|
||
|
if err != nil {
|
||
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||
|
}
|
||
|
|
||
|
pack.Prefix = data.Prefix
|
||
|
pack.Name = data.Name
|
||
|
pack.Description = data.Description
|
||
|
|
||
|
if pack, err = services.UpdateStickerPack(pack); err != nil {
|
||
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||
|
}
|
||
|
|
||
|
return c.JSON(pack)
|
||
|
}
|
||
|
|
||
|
func deleteStickerPack(c *fiber.Ctx) error {
|
||
|
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
user := c.Locals("user").(models.Account)
|
||
|
|
||
|
id, _ := c.ParamsInt("packId", 0)
|
||
|
pack, err := services.GetStickerPackWithUser(uint(id), user.ID)
|
||
|
if err != nil {
|
||
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||
|
}
|
||
|
|
||
|
if pack, err = services.DeleteStickerPack(pack); err != nil {
|
||
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||
|
}
|
||
|
|
||
|
return c.JSON(pack)
|
||
|
}
|