222 lines
6.6 KiB
Go
222 lines
6.6 KiB
Go
package repository
|
|
|
|
import (
|
|
"errors"
|
|
|
|
appErrors "github.com/topfans/backend/pkg/errors"
|
|
"github.com/topfans/backend/pkg/models"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// CollectionRepository 典藏藏品Repository接口
|
|
type CollectionRepository interface {
|
|
// Create 创建典藏藏品
|
|
Create(asset *models.CollectionAsset) error
|
|
|
|
// GetByID 根据ID查询
|
|
GetByID(id int64) (*models.CollectionAsset, error)
|
|
|
|
// GetByAssetID 根据asset_id查询
|
|
GetByAssetID(assetID int64) (*models.CollectionAsset, error)
|
|
|
|
// GetByAssetIDs 批量查询
|
|
GetByAssetIDs(assetIDs []int64) ([]*models.CollectionAsset, error)
|
|
|
|
// GetByOwner 查询用户的典藏藏品列表
|
|
GetByOwner(ownerUID, starID int64, limit, offset int) ([]*models.CollectionAsset, error)
|
|
|
|
// GetByOwnerAndCategory 查询用户指定分类的典藏藏品
|
|
GetByOwnerAndCategory(ownerUID, starID int64, category string, limit, offset int) ([]*models.CollectionAsset, error)
|
|
|
|
// CountByOwner 统计用户的典藏藏品数量
|
|
CountByOwner(ownerUID, starID int64) (int64, error)
|
|
|
|
// CountByOwnerAndCategory 统计用户指定分类的典藏藏品数量
|
|
CountByOwnerAndCategory(ownerUID, starID int64, category string) (int64, error)
|
|
|
|
// UpdateLikeCount 更新点赞数
|
|
UpdateLikeCount(id int64, likeCount int32) error
|
|
|
|
// IncrementLikeCount 增加点赞数
|
|
IncrementLikeCount(id int64) error
|
|
|
|
// DecrementLikeCount 减少点赞数
|
|
DecrementLikeCount(id int64) error
|
|
}
|
|
|
|
// collectionRepository 典藏藏品Repository实现
|
|
type collectionRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewCollectionRepository 创建典藏藏品Repository实例
|
|
func NewCollectionRepository(db *gorm.DB) CollectionRepository {
|
|
return &collectionRepository{db: db}
|
|
}
|
|
|
|
// Create 创建典藏藏品
|
|
func (r *collectionRepository) Create(asset *models.CollectionAsset) error {
|
|
if asset == nil {
|
|
return errors.New("collection asset cannot be nil")
|
|
}
|
|
if asset.OwnerUID <= 0 {
|
|
return errors.New("owner_uid must be greater than 0")
|
|
}
|
|
if asset.StarID <= 0 {
|
|
return errors.New("star_id must be greater than 0")
|
|
}
|
|
return r.db.Create(asset).Error
|
|
}
|
|
|
|
// GetByID 根据ID查询
|
|
func (r *collectionRepository) GetByID(id int64) (*models.CollectionAsset, error) {
|
|
if id <= 0 {
|
|
return nil, errors.New("id must be greater than 0")
|
|
}
|
|
var asset models.CollectionAsset
|
|
if err := r.db.Where("id = ?", id).First(&asset).Error; err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, appErrors.ErrCollectionAssetNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
return &asset, nil
|
|
}
|
|
|
|
// GetByAssetID 根据asset_id查询
|
|
func (r *collectionRepository) GetByAssetID(assetID int64) (*models.CollectionAsset, error) {
|
|
if assetID <= 0 {
|
|
return nil, errors.New("asset_id must be greater than 0")
|
|
}
|
|
var asset models.CollectionAsset
|
|
if err := r.db.Where("asset_id = ?", assetID).First(&asset).Error; err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, appErrors.ErrCollectionAssetNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
return &asset, nil
|
|
}
|
|
|
|
// GetByAssetIDs 批量查询
|
|
func (r *collectionRepository) GetByAssetIDs(assetIDs []int64) ([]*models.CollectionAsset, error) {
|
|
if len(assetIDs) == 0 {
|
|
return []*models.CollectionAsset{}, nil
|
|
}
|
|
var assets []*models.CollectionAsset
|
|
if err := r.db.Where("asset_id IN ?", assetIDs).Find(&assets).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return assets, nil
|
|
}
|
|
|
|
// GetByOwner 查询用户的典藏藏品列表
|
|
func (r *collectionRepository) GetByOwner(ownerUID, starID int64, limit, offset int) ([]*models.CollectionAsset, error) {
|
|
if ownerUID <= 0 {
|
|
return nil, errors.New("owner_uid must be greater than 0")
|
|
}
|
|
if starID <= 0 {
|
|
return nil, errors.New("star_id must be greater than 0")
|
|
}
|
|
var assets []*models.CollectionAsset
|
|
query := r.db.Where("owner_uid = ? AND star_id = ?", ownerUID, starID).
|
|
Order("created_at DESC")
|
|
if limit > 0 {
|
|
query = query.Limit(limit)
|
|
}
|
|
if offset > 0 {
|
|
query = query.Offset(offset)
|
|
}
|
|
if err := query.Find(&assets).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return assets, nil
|
|
}
|
|
|
|
// GetByOwnerAndCategory 查询用户指定分类的典藏藏品
|
|
func (r *collectionRepository) GetByOwnerAndCategory(ownerUID, starID int64, category string, limit, offset int) ([]*models.CollectionAsset, error) {
|
|
if ownerUID <= 0 {
|
|
return nil, errors.New("owner_uid must be greater than 0")
|
|
}
|
|
if starID <= 0 {
|
|
return nil, errors.New("star_id must be greater than 0")
|
|
}
|
|
var assets []*models.CollectionAsset
|
|
query := r.db.Where("owner_uid = ? AND star_id = ? AND category = ?", ownerUID, starID, category).
|
|
Order("created_at DESC")
|
|
if limit > 0 {
|
|
query = query.Limit(limit)
|
|
}
|
|
if offset > 0 {
|
|
query = query.Offset(offset)
|
|
}
|
|
if err := query.Find(&assets).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return assets, nil
|
|
}
|
|
|
|
// CountByOwner 统计用户的典藏藏品数量
|
|
func (r *collectionRepository) CountByOwner(ownerUID, starID int64) (int64, error) {
|
|
if ownerUID <= 0 {
|
|
return 0, errors.New("owner_uid must be greater than 0")
|
|
}
|
|
if starID <= 0 {
|
|
return 0, errors.New("star_id must be greater than 0")
|
|
}
|
|
var count int64
|
|
if err := r.db.Model(&models.CollectionAsset{}).
|
|
Where("owner_uid = ? AND star_id = ?", ownerUID, starID).
|
|
Count(&count).Error; err != nil {
|
|
return 0, err
|
|
}
|
|
return count, nil
|
|
}
|
|
|
|
// CountByOwnerAndCategory 统计用户指定分类的典藏藏品数量
|
|
func (r *collectionRepository) CountByOwnerAndCategory(ownerUID, starID int64, category string) (int64, error) {
|
|
if ownerUID <= 0 {
|
|
return 0, errors.New("owner_uid must be greater than 0")
|
|
}
|
|
if starID <= 0 {
|
|
return 0, errors.New("star_id must be greater than 0")
|
|
}
|
|
var count int64
|
|
if err := r.db.Model(&models.CollectionAsset{}).
|
|
Where("owner_uid = ? AND star_id = ? AND category = ?", ownerUID, starID, category).
|
|
Count(&count).Error; err != nil {
|
|
return 0, err
|
|
}
|
|
return count, nil
|
|
}
|
|
|
|
// UpdateLikeCount 更新点赞数
|
|
func (r *collectionRepository) UpdateLikeCount(id int64, likeCount int32) error {
|
|
if id <= 0 {
|
|
return errors.New("id must be greater than 0")
|
|
}
|
|
return r.db.Model(&models.CollectionAsset{}).
|
|
Where("id = ?", id).
|
|
Update("like_count", likeCount).Error
|
|
}
|
|
|
|
// IncrementLikeCount 增加点赞数
|
|
func (r *collectionRepository) IncrementLikeCount(id int64) error {
|
|
if id <= 0 {
|
|
return errors.New("id must be greater than 0")
|
|
}
|
|
return r.db.Model(&models.CollectionAsset{}).
|
|
Where("id = ?", id).
|
|
UpdateColumn("like_count", gorm.Expr("like_count + ?", 1)).Error
|
|
}
|
|
|
|
// DecrementLikeCount 减少点赞数
|
|
func (r *collectionRepository) DecrementLikeCount(id int64) error {
|
|
if id <= 0 {
|
|
return errors.New("id must be greater than 0")
|
|
}
|
|
return r.db.Model(&models.CollectionAsset{}).
|
|
Where("id = ? AND like_count > ?", id, 0).
|
|
UpdateColumn("like_count", gorm.Expr("like_count - ?", 1)).Error
|
|
}
|