Feat: list/search/update/delete shares

This commit is contained in:
HFO4
2020-02-13 11:53:24 +08:00
parent bb63ea7142
commit cfbfbc3c55
9 changed files with 290 additions and 5 deletions

View File

@ -25,6 +25,7 @@ type Share struct {
Expires *time.Time // 过期时间,空值表示无过期时间
Score int // 每人次下载扣除积分
PreviewEnabled bool // 是否允许直接预览
SourceName string `gorm:"index:source"` // 用于搜索的字段
// 数据库忽略字段
User User `gorm:"PRELOAD:false,association_autoupdate:false"`
@ -79,6 +80,11 @@ func (share *Share) IsAvailable() bool {
return false
}
// 检查创建者状态
if share.Creator().Status != Active {
return false
}
return true
}
@ -202,3 +208,50 @@ func (share *Share) Downloaded() {
"remain_downloads": share.RemainDownloads,
})
}
// Update 更新分享属性
func (share *Share) Update(props map[string]interface{}) error {
return DB.Model(share).Updates(props).Error
}
// Delete 删除分享
func (share *Share) Delete() error {
return DB.Model(share).Delete(share).Error
}
// ListShares 列出UID下的分享
func ListShares(uid uint, page, pageSize int, order string, publicOnly bool) ([]Share, int) {
var (
shares []Share
total int
)
dbChain := DB
dbChain = dbChain.Where("user_id = ?", uid)
if publicOnly {
dbChain.Where("password = ?", "")
}
// 计算总数用于分页
dbChain.Model(&Share{}).Count(&total)
// 查询记录
dbChain.Limit(pageSize).Offset((page - 1) * pageSize).Order(order).Find(&shares)
return shares, total
}
// SearchShares 根据关键字搜索分享
func SearchShares(page, pageSize int, order, keywords string) ([]Share, int) {
var (
shares []Share
total int
)
dbChain := DB
dbChain = dbChain.Where("password = ? and remain_downloads <> 0 and (expires is NULL or expires > ?) and source_name like ?", "", time.Now(), "%"+keywords+"%")
// 计算总数用于分页
dbChain.Model(&Share{}).Count(&total)
// 查询记录
dbChain.Limit(pageSize).Offset((page - 1) * pageSize).Order(order).Find(&shares)
return shares, total
}