2024-08-03 07:43:15 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2024-10-27 05:13:40 +00:00
|
|
|
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
2024-11-02 02:41:31 +00:00
|
|
|
"git.solsynth.dev/hypernet/paperclip/pkg/internal/database"
|
2024-11-02 17:53:57 +00:00
|
|
|
"git.solsynth.dev/hypernet/paperclip/pkg/internal/gap"
|
2024-11-02 02:41:31 +00:00
|
|
|
"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"
|
2024-11-02 17:53:57 +00:00
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/authkit"
|
2024-08-03 07:43:15 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
2024-08-03 08:16:14 +00:00
|
|
|
func listStickerPacks(c *fiber.Ctx) error {
|
|
|
|
take := c.QueryInt("take", 0)
|
|
|
|
offset := c.QueryInt("offset", 0)
|
|
|
|
|
|
|
|
if take > 100 {
|
|
|
|
take = 100
|
|
|
|
}
|
|
|
|
|
|
|
|
tx := database.C
|
|
|
|
|
|
|
|
if len(c.Query("author")) > 0 {
|
2024-11-02 17:53:57 +00:00
|
|
|
author, err := authkit.GetUserByName(gap.Nx, c.Query("author"))
|
|
|
|
if err == nil {
|
2024-08-03 08:16:14 +00:00
|
|
|
tx = tx.Where("account_id = ?", author.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-03 14:45:50 +00:00
|
|
|
var count int64
|
|
|
|
if err := database.C.Model(&models.StickerPack{}).Count(&count).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
}
|
|
|
|
|
2024-08-03 08:16:14 +00:00
|
|
|
var packs []models.StickerPack
|
|
|
|
if err := tx.Limit(take).Offset(offset).Find(&packs).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
}
|
2024-08-03 14:45:50 +00:00
|
|
|
|
|
|
|
return c.JSON(fiber.Map{
|
|
|
|
"count": count,
|
|
|
|
"data": packs,
|
|
|
|
})
|
2024-08-03 08:16:14 +00:00
|
|
|
}
|
|
|
|
|
2024-09-25 14:55:15 +00:00
|
|
|
func getStickerPack(c *fiber.Ctx) error {
|
|
|
|
id, _ := c.ParamsInt("packId", 0)
|
|
|
|
pack, err := services.GetStickerPack(uint(id))
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(pack)
|
|
|
|
}
|
|
|
|
|
2024-08-03 07:43:15 +00:00
|
|
|
func createStickerPack(c *fiber.Ctx) error {
|
2024-11-02 18:14:56 +00:00
|
|
|
user := c.Locals("nex_user").(*sec.UserInfo)
|
2024-08-03 07:43:15 +00:00
|
|
|
|
|
|
|
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 {
|
2024-11-02 18:14:56 +00:00
|
|
|
user := c.Locals("nex_user").(*sec.UserInfo)
|
2024-08-03 07:43:15 +00:00
|
|
|
|
|
|
|
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 {
|
2024-11-02 18:14:56 +00:00
|
|
|
user := c.Locals("nex_user").(*sec.UserInfo)
|
2024-08-03 07:43:15 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|