2024-08-03 07:43:15 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2024-09-15 10:39:09 +00:00
|
|
|
"fmt"
|
|
|
|
|
2024-11-02 02:41:31 +00:00
|
|
|
"git.solsynth.dev/hypernet/paperclip/pkg/internal/database"
|
|
|
|
"git.solsynth.dev/hypernet/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-16 12:44:40 +00:00
|
|
|
func GetStickerLikeAlias(alias string) ([]models.Sticker, error) {
|
|
|
|
var stickers []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("UPPER(CONCAT(pk.prefix, alias)) LIKE UPPER(?)", "%"+alias+"%").
|
|
|
|
Preload("Attachment").Preload("Pack").
|
|
|
|
Limit(10).
|
|
|
|
Find(&stickers).Error; err != nil {
|
|
|
|
return stickers, err
|
|
|
|
}
|
|
|
|
return stickers, nil
|
|
|
|
}
|
|
|
|
|
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)).
|
2024-09-16 12:40:57 +00:00
|
|
|
Where("UPPER(CONCAT(pk.prefix, alias)) = UPPER(?)", alias).
|
2024-09-16 12:35:59 +00:00
|
|
|
Preload("Attachment").Preload("Pack").
|
|
|
|
First(&sticker).Error; err != nil {
|
2024-09-15 10:39:09 +00:00
|
|
|
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
|
|
|
|
}
|