Files
backend/internal/repository/sticker_repo.go

127 lines
3.5 KiB
Go
Raw Normal View History

package repository
import (
"with_you/internal/model"
"strings"
"gorm.io/gorm"
)
// StickerRepository 自定义表情仓库接口
type StickerRepository interface {
// 获取用户的所有表情
GetByUserID(userID string) ([]model.UserSticker, error)
// 根据ID获取表情
GetByID(id string) (*model.UserSticker, error)
// 创建表情
Create(sticker *model.UserSticker) error
// 删除表情
Delete(id string) error
// 删除用户的所有表情
DeleteByUserID(userID string) error
// 检查表情是否存在
Exists(userID string, url string) (bool, error)
// 更新排序
UpdateSortOrder(id string, sortOrder int) error
// 批量更新排序
BatchUpdateSortOrder(userID string, orders map[string]int) error
// 获取用户表情数量
CountByUserID(userID string) (int64, error)
}
// stickerRepository 自定义表情仓库实现
type stickerRepository struct {
db *gorm.DB
}
// NewStickerRepository 创建自定义表情仓库
func NewStickerRepository(db *gorm.DB) StickerRepository {
return &stickerRepository{db: db}
}
// GetByUserID 获取用户的所有表情
func (r *stickerRepository) GetByUserID(userID string) ([]model.UserSticker, error) {
var stickers []model.UserSticker
err := r.db.Where("user_id = ?", userID).
Order("sort_order ASC, created_at DESC").
Find(&stickers).Error
return stickers, err
}
// GetByID 根据ID获取表情
func (r *stickerRepository) GetByID(id string) (*model.UserSticker, error) {
var sticker model.UserSticker
err := r.db.Where("id = ?", id).First(&sticker).Error
if err != nil {
return nil, err
}
return &sticker, nil
}
// Create 创建表情
func (r *stickerRepository) Create(sticker *model.UserSticker) error {
return r.db.Create(sticker).Error
}
// Delete 删除表情
func (r *stickerRepository) Delete(id string) error {
return r.db.Where("id = ?", id).Delete(&model.UserSticker{}).Error
}
// DeleteByUserID 删除用户的所有表情
func (r *stickerRepository) DeleteByUserID(userID string) error {
return r.db.Where("user_id = ?", userID).Delete(&model.UserSticker{}).Error
}
// Exists 检查表情是否存在
func (r *stickerRepository) Exists(userID string, url string) (bool, error) {
var count int64
err := r.db.Model(&model.UserSticker{}).
Where("user_id = ? AND url = ?", userID, url).
Count(&count).Error
return count > 0, err
}
// UpdateSortOrder 更新排序
func (r *stickerRepository) UpdateSortOrder(id string, sortOrder int) error {
return r.db.Model(&model.UserSticker{}).
Where("id = ?", id).
Update("sort_order", sortOrder).Error
}
// BatchUpdateSortOrder 批量更新排序(使用 CASE WHEN 批量更新,避免 N+1 问题)
func (r *stickerRepository) BatchUpdateSortOrder(userID string, orders map[string]int) error {
if len(orders) == 0 {
return nil
}
// 构建 CASE WHEN 批量更新 SQL
var cases []string
var args []any
for id, sortOrder := range orders {
cases = append(cases, "WHEN id = ? THEN ?")
args = append(args, id, sortOrder)
}
ids := make([]string, 0, len(orders))
for id := range orders {
ids = append(ids, id)
}
sql := `UPDATE user_stickers SET sort_order = CASE ` +
strings.Join(cases, " ") + ` END WHERE id IN ? AND user_id = ?`
args = append(args, ids, userID)
return r.db.Exec(sql, args...).Error
}
// CountByUserID 获取用户表情数量
func (r *stickerRepository) CountByUserID(userID string) (int64, error) {
var count int64
err := r.db.Model(&model.UserSticker{}).
Where("user_id = ?", userID).
Count(&count).Error
return count, err
}