2024-08-03 07:43:15 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2024-09-15 10:39:09 +00:00
|
|
|
"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"
|
2024-09-15 10:39:09 +00:00
|
|
|
"github.com/spf13/viper"
|
2024-08-03 07:43:15 +00:00
|
|
|
)
|
|
|
|
|
2024-09-15 10:39:09 +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
|
2024-08-03 14:04:44 +00:00
|
|
|
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
|
|
|
|
}
|