64 lines
1.7 KiB
Go
64 lines
1.7 KiB
Go
|
|
package repository
|
||
|
|
|
||
|
|
import (
|
||
|
|
"seeyon-filesystem/config"
|
||
|
|
"seeyon-filesystem/model"
|
||
|
|
)
|
||
|
|
|
||
|
|
// ShareRepository 分享数据访问
|
||
|
|
type ShareRepository struct{}
|
||
|
|
|
||
|
|
// NewShareRepository 创建分享Repository实例
|
||
|
|
func NewShareRepository() *ShareRepository {
|
||
|
|
return &ShareRepository{}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Create 创建分享记录
|
||
|
|
func (r *ShareRepository) Create(share *model.Share) error {
|
||
|
|
return config.DB.Create(share).Error
|
||
|
|
}
|
||
|
|
|
||
|
|
// FindByShareCode 根据分享码查找
|
||
|
|
func (r *ShareRepository) FindByShareCode(code string) (*model.Share, error) {
|
||
|
|
var share model.Share
|
||
|
|
err := config.DB.Where("share_code = ? AND status = 1", code).First(&share).Error
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return &share, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// FindByID 根据ID查找
|
||
|
|
func (r *ShareRepository) FindByID(id uint64) (*model.Share, error) {
|
||
|
|
var share model.Share
|
||
|
|
err := config.DB.First(&share, id).Error
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return &share, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// ListByOwner 查询用户的分享列表
|
||
|
|
func (r *ShareRepository) ListByOwner(ownerID uint64) ([]model.Share, error) {
|
||
|
|
var shares []model.Share
|
||
|
|
err := config.DB.Where("owner_id = ? AND status = 1", ownerID).
|
||
|
|
Order("created_at DESC").Find(&shares).Error
|
||
|
|
return shares, err
|
||
|
|
}
|
||
|
|
|
||
|
|
// IncrementDownloadCount 增加下载次数
|
||
|
|
func (r *ShareRepository) IncrementDownloadCount(id uint64) error {
|
||
|
|
return config.DB.Model(&model.Share{}).Where("id = ?", id).
|
||
|
|
Update("download_count", config.DB.Raw("download_count + 1")).Error
|
||
|
|
}
|
||
|
|
|
||
|
|
// Cancel 取消分享
|
||
|
|
func (r *ShareRepository) Cancel(id uint64) error {
|
||
|
|
return config.DB.Model(&model.Share{}).Where("id = ?", id).Update("status", 0).Error
|
||
|
|
}
|
||
|
|
|
||
|
|
// Delete 永久删除
|
||
|
|
func (r *ShareRepository) Delete(id uint64) error {
|
||
|
|
return config.DB.Unscoped().Delete(&model.Share{}, id).Error
|
||
|
|
}
|