Paperclip/pkg/internal/services/stickers.go

58 lines
1.5 KiB
Go
Raw Normal View History

2024-08-03 07:43:15 +00:00
package services
import (
"fmt"
2024-08-03 07:43:15 +00:00
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/database"
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/models"
"github.com/spf13/viper"
2024-08-03 07:43:15 +00:00
)
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
}
2024-08-03 07:43:15 +00:00
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 {
2024-08-03 07:43:15 +00:00
return sticker, err
}
return sticker, nil
}
func GetStickerWithUser(id, userId uint) (models.Sticker, error) {
var sticker models.Sticker
if err := database.C.Where("id = ? AND account_id = ?", id, userId).First(&sticker).Error; err != nil {
return sticker, err
}
return sticker, nil
}
func NewSticker(sticker models.Sticker) (models.Sticker, error) {
if err := database.C.Save(&sticker).Error; err != nil {
return sticker, err
}
return sticker, nil
}
func UpdateSticker(sticker models.Sticker) (models.Sticker, error) {
if err := database.C.Save(&sticker).Error; err != nil {
return sticker, err
}
return sticker, nil
}
func DeleteSticker(sticker models.Sticker) (models.Sticker, error) {
if err := database.C.Delete(&sticker).Error; err != nil {
return sticker, err
}
return sticker, nil
}