225 lines
4.9 KiB
Go
225 lines
4.9 KiB
Go
package repository
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"github.com/topfans/backend/pkg/models"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// AssetLikeRepository 资产点赞记录Repository接口
|
|
type AssetLikeRepository interface {
|
|
// Create 创建点赞记录
|
|
Create(like *models.AssetLike) error
|
|
|
|
// Delete 删除点赞记录
|
|
Delete(assetID, userID, starID int64) error
|
|
|
|
// Exists 检查点赞记录是否存在
|
|
Exists(assetID, userID, starID int64) (bool, error)
|
|
|
|
// GetByAsset 获取资产的点赞记录列表(分页)
|
|
GetByAsset(assetID int64, limit, offset int) ([]*models.AssetLike, error)
|
|
|
|
// GetByUser 获取用户的点赞记录列表(分页)
|
|
GetByUser(userID, starID int64, limit, offset int) ([]*models.AssetLike, error)
|
|
|
|
// CountByAsset 统计资产的点赞数
|
|
CountByAsset(assetID int64) (int64, error)
|
|
|
|
// CountByUser 统计用户的点赞数
|
|
CountByUser(userID, starID int64) (int64, error)
|
|
}
|
|
|
|
// assetLikeRepository 资产点赞记录Repository实现
|
|
type assetLikeRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewAssetLikeRepository 创建资产点赞记录Repository实例
|
|
func NewAssetLikeRepository(db *gorm.DB) AssetLikeRepository {
|
|
return &assetLikeRepository{
|
|
db: db,
|
|
}
|
|
}
|
|
|
|
// Create 创建点赞记录
|
|
func (r *assetLikeRepository) Create(like *models.AssetLike) error {
|
|
if like == nil {
|
|
return errors.New("asset like cannot be nil")
|
|
}
|
|
|
|
if like.AssetID <= 0 {
|
|
return errors.New("asset_id must be greater than 0")
|
|
}
|
|
|
|
if like.UserID <= 0 {
|
|
return errors.New("user_id must be greater than 0")
|
|
}
|
|
|
|
if like.StarID <= 0 {
|
|
return errors.New("star_id must be greater than 0")
|
|
}
|
|
|
|
// 检查是否已存在
|
|
exists, err := r.Exists(like.AssetID, like.UserID, like.StarID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if exists {
|
|
return errors.New("already liked")
|
|
}
|
|
|
|
if err := r.db.Create(like).Error; err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Delete 删除点赞记录
|
|
func (r *assetLikeRepository) Delete(assetID, userID, starID int64) error {
|
|
if assetID <= 0 {
|
|
return errors.New("asset_id must be greater than 0")
|
|
}
|
|
|
|
if userID <= 0 {
|
|
return errors.New("user_id must be greater than 0")
|
|
}
|
|
|
|
if starID <= 0 {
|
|
return errors.New("star_id must be greater than 0")
|
|
}
|
|
|
|
result := r.db.Where("asset_id = ? AND user_id = ? AND star_id = ?", assetID, userID, starID).
|
|
Delete(&models.AssetLike{})
|
|
|
|
if result.Error != nil {
|
|
return result.Error
|
|
}
|
|
|
|
if result.RowsAffected == 0 {
|
|
return errors.New("asset like not found")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Exists 检查点赞记录是否存在
|
|
func (r *assetLikeRepository) Exists(assetID, userID, starID int64) (bool, error) {
|
|
if assetID <= 0 {
|
|
return false, errors.New("asset_id must be greater than 0")
|
|
}
|
|
|
|
if userID <= 0 {
|
|
return false, errors.New("user_id must be greater than 0")
|
|
}
|
|
|
|
if starID <= 0 {
|
|
return false, errors.New("star_id must be greater than 0")
|
|
}
|
|
|
|
var count int64
|
|
err := r.db.Model(&models.AssetLike{}).
|
|
Where("asset_id = ? AND user_id = ? AND star_id = ?", assetID, userID, starID).
|
|
Count(&count).Error
|
|
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
return count > 0, nil
|
|
}
|
|
|
|
// GetByAsset 获取资产的点赞记录列表(分页)
|
|
func (r *assetLikeRepository) GetByAsset(assetID int64, limit, offset int) ([]*models.AssetLike, error) {
|
|
if assetID <= 0 {
|
|
return nil, errors.New("asset_id must be greater than 0")
|
|
}
|
|
|
|
if limit <= 0 {
|
|
limit = 20
|
|
}
|
|
|
|
var likes []*models.AssetLike
|
|
err := r.db.Where("asset_id = ?", assetID).
|
|
Order("created_at DESC").
|
|
Limit(limit).
|
|
Offset(offset).
|
|
Find(&likes).Error
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return likes, nil
|
|
}
|
|
|
|
// GetByUser 获取用户的点赞记录列表(分页)
|
|
func (r *assetLikeRepository) GetByUser(userID, starID int64, limit, offset int) ([]*models.AssetLike, error) {
|
|
if userID <= 0 {
|
|
return nil, errors.New("user_id must be greater than 0")
|
|
}
|
|
|
|
if starID <= 0 {
|
|
return nil, errors.New("star_id must be greater than 0")
|
|
}
|
|
|
|
if limit <= 0 {
|
|
limit = 20
|
|
}
|
|
|
|
var likes []*models.AssetLike
|
|
err := r.db.Where("user_id = ? AND star_id = ?", userID, starID).
|
|
Order("created_at DESC").
|
|
Limit(limit).
|
|
Offset(offset).
|
|
Find(&likes).Error
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return likes, nil
|
|
}
|
|
|
|
// CountByAsset 统计资产的点赞数
|
|
func (r *assetLikeRepository) CountByAsset(assetID int64) (int64, error) {
|
|
if assetID <= 0 {
|
|
return 0, errors.New("asset_id must be greater than 0")
|
|
}
|
|
|
|
var count int64
|
|
err := r.db.Model(&models.AssetLike{}).
|
|
Where("asset_id = ?", assetID).
|
|
Count(&count).Error
|
|
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
return count, nil
|
|
}
|
|
|
|
// CountByUser 统计用户的点赞数
|
|
func (r *assetLikeRepository) CountByUser(userID, starID int64) (int64, error) {
|
|
if userID <= 0 {
|
|
return 0, errors.New("user_id must be greater than 0")
|
|
}
|
|
|
|
if starID <= 0 {
|
|
return 0, errors.New("star_id must be greater than 0")
|
|
}
|
|
|
|
var count int64
|
|
err := r.db.Model(&models.AssetLike{}).
|
|
Where("user_id = ? AND star_id = ?", userID, starID).
|
|
Count(&count).Error
|
|
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
return count, nil
|
|
}
|