50 lines
1.3 KiB
Go
50 lines
1.3 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/topfans/backend/services/assetService/model"
|
|
)
|
|
|
|
// ShareRepo 分享事件数据访问层
|
|
type ShareRepo struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewShareRepo(db *gorm.DB) *ShareRepo {
|
|
return &ShareRepo{db: db}
|
|
}
|
|
|
|
// Create 插入一条分享事件,返回新 ID
|
|
func (r *ShareRepo) Create(ctx context.Context, e *model.ShareEvent) (int64, error) {
|
|
if err := r.db.WithContext(ctx).Create(e).Error; err != nil {
|
|
return 0, err
|
|
}
|
|
return e.ID, nil
|
|
}
|
|
|
|
// AssetExists 校验资产是否存在
|
|
func (r *ShareRepo) AssetExists(ctx context.Context, assetID int64) (bool, error) {
|
|
var count int64
|
|
if err := r.db.WithContext(ctx).Table("public.assets").Where("id = ?", assetID).Count(&count).Error; err != nil {
|
|
return false, err
|
|
}
|
|
return count > 0, nil
|
|
}
|
|
|
|
// UserExists 校验用户存在且状态正常
|
|
//
|
|
// 注意:项目 User 模型使用 is_active bool + deleted_at IS NULL 软删除,
|
|
// 没有 status 字段(与本任务模板的 status = 1 不一致)。
|
|
func (r *ShareRepo) UserExists(ctx context.Context, userID int64) (bool, error) {
|
|
var count int64
|
|
if err := r.db.WithContext(ctx).Table("public.users").
|
|
Where("id = ? AND is_active = ? AND deleted_at IS NULL", userID, true).
|
|
Count(&count).Error; err != nil {
|
|
return false, err
|
|
}
|
|
return count > 0, nil
|
|
}
|