topfans/backend/services/assetService/repository/asset_like_repository.go
2026-05-29 19:47:09 +08:00

263 lines
6.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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, exhibitionID int64) error
// Exists 检查点赞记录是否存在(按 asset_id, user_id, exhibition_id
Exists(assetID, userID, starID, exhibitionID int64) (bool, error)
// ExistsByAsset 检查用户是否对资产有点赞记录(不管 exhibition_id
ExistsByAsset(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, like.ExhibitionID)
if err != nil {
return err
}
if exists {
return errors.New("本次展示已点赞")
}
if err := r.db.Create(like).Error; err != nil {
return err
}
return nil
}
// Delete 删除点赞记录
func (r *assetLikeRepository) Delete(assetID, userID, starID, exhibitionID 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")
}
if exhibitionID <= 0 {
return errors.New("exhibition_id must be greater than 0")
}
result := r.db.Where("asset_id = ? AND user_id = ? AND star_id = ? AND exhibition_id = ?", assetID, userID, starID, exhibitionID).
Delete(&models.AssetLike{})
if result.Error != nil {
return result.Error
}
if result.RowsAffected == 0 {
return errors.New("asset like not found")
}
return nil
}
// Exists 检查点赞记录是否存在(按 asset_id, user_id, exhibition_id
func (r *assetLikeRepository) Exists(assetID, userID, starID, exhibitionID 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")
}
if exhibitionID <= 0 {
return false, errors.New("exhibition_id must be greater than 0")
}
var count int64
err := r.db.Model(&models.AssetLike{}).
Where("asset_id = ? AND user_id = ? AND star_id = ? AND exhibition_id = ?", assetID, userID, starID, exhibitionID).
Count(&count).Error
if err != nil {
return false, err
}
return count > 0, nil
}
// ExistsByAsset 检查用户是否对资产有点赞记录(不管 exhibition_id
// 用于排行榜等场景,判断用户是否对某个藏品点赞过
func (r *assetLikeRepository) ExistsByAsset(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
}