Be able to lookup sticker via thier pack prefix + alias

This commit is contained in:
LittleSheep 2024-09-15 18:39:09 +08:00
parent af1b45bd92
commit 525a103a76
3 changed files with 24 additions and 0 deletions

View File

@ -23,6 +23,7 @@ func MapAPIs(app *fiber.App, baseURL string) {
api.Post("/attachments/multipart", createAttachmentMultipartPlaceholder)
api.Post("/attachments/multipart/:file/:chunk", uploadAttachmentMultipart)
api.Get("/stickers/lookup/:alias", lookupSticker)
api.Get("/stickers/manifest", listStickerManifest)
api.Get("/stickers/packs", listStickerPacks)
api.Post("/stickers/packs", createStickerPack)

View File

@ -12,6 +12,15 @@ import (
"github.com/gofiber/fiber/v2"
)
func lookupSticker(c *fiber.Ctx) error {
alias := c.Params("alias")
if sticker, err := services.GetStickerWithAlias(alias); err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
} else {
return c.JSON(sticker)
}
}
func listStickers(c *fiber.Ctx) error {
take := c.QueryInt("take", 0)
offset := c.QueryInt("offset", 0)

View File

@ -1,10 +1,24 @@
package services
import (
"fmt"
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/database"
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/models"
"github.com/spf13/viper"
)
func GetStickerWithAlias(alias string) (models.Sticker, error) {
var sticker models.Sticker
prefix := viper.GetString("database.prefix")
if err := database.C.
Joins(fmt.Sprintf("LEFT JOIN %ssticker_packs pk ON pack_id = pk.id", prefix)).
Where("CONCAT(pk.prefix, alias) = ?", alias).First(&sticker).Error; err != nil {
return sticker, err
}
return sticker, nil
}
func GetSticker(id uint) (models.Sticker, error) {
var sticker models.Sticker
if err := database.C.Where("id = ?", id).Preload("Attachment").First(&sticker).Error; err != nil {