More stickers apis

This commit is contained in:
LittleSheep 2024-08-03 16:16:14 +08:00
parent ad1d82a2ff
commit 7af44da03b
3 changed files with 60 additions and 1 deletions

View File

@ -14,11 +14,13 @@ func MapAPIs(app *fiber.App, baseURL string) {
api.Put("/attachments/:id", updateAttachmentMeta)
api.Delete("/attachments/:id", deleteAttachment)
api.Get("/stickers/manifest", listStickerManifest)
api.Get("/stickers/packs", listStickerPacks)
api.Post("/stickers/packs", createStickerPack)
api.Put("/stickers/packs/:packId", updateStickerPack)
api.Delete("/stickers/packs/:packId", deleteStickerPack)
api.Get("/stickers", listStickers)
api.Get("/stickers/:stickerId", getSticker)
api.Post("/stickers", createSticker)
api.Put("/stickers/:stickerId", updateSticker)

View File

@ -1,6 +1,7 @@
package api
import (
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/database"
"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"
@ -8,7 +9,7 @@ import (
"github.com/gofiber/fiber/v2"
)
func listStickerPacks(c *fiber.Ctx) error {
func listStickerManifest(c *fiber.Ctx) error {
take := c.QueryInt("take", 0)
offset := c.QueryInt("offset", 0)
@ -23,6 +24,32 @@ func listStickerPacks(c *fiber.Ctx) error {
return c.JSON(stickers)
}
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 {
var author models.Account
if err := database.C.Where("name = ?", c.Query("author")).First(&author).Error; err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {
tx = tx.Where("account_id = ?", author.ID)
}
}
var packs []models.StickerPack
if err := tx.Limit(take).Offset(offset).Find(&packs).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
return c.JSON(packs)
}
func createStickerPack(c *fiber.Ctx) error {
if err := gap.H.EnsureAuthenticated(c); err != nil {
return err

View File

@ -12,6 +12,36 @@ import (
"github.com/gofiber/fiber/v2"
)
func listStickers(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 {
var author models.Account
if err := database.C.Where("name = ?", c.Query("author")).First(&author).Error; err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {
tx = tx.Where("account_id = ?", author.ID)
}
}
if val := c.QueryInt("pack", 0); val > 0 {
tx = tx.Where("pack_id = ?", val)
}
var stickers []models.Sticker
if err := tx.Limit(take).Offset(offset).Find(&stickers).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
return c.JSON(stickers)
}
func getSticker(c *fiber.Ctx) error {
id, _ := c.ParamsInt("stickerId", 0)
sticker, err := services.GetSticker(uint(id))