95 lines
2.9 KiB
Go
95 lines
2.9 KiB
Go
|
|
package repository
|
||
|
|
|
||
|
|
import (
|
||
|
|
"seeyon-filesystem/config"
|
||
|
|
"seeyon-filesystem/model"
|
||
|
|
)
|
||
|
|
|
||
|
|
// FolderRepository 文件夹数据访问
|
||
|
|
type FolderRepository struct{}
|
||
|
|
|
||
|
|
// NewFolderRepository 创建文件夹Repository实例
|
||
|
|
func NewFolderRepository() *FolderRepository {
|
||
|
|
return &FolderRepository{}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Create 创建文件夹
|
||
|
|
func (r *FolderRepository) Create(folder *model.Folder) error {
|
||
|
|
return config.DB.Create(folder).Error
|
||
|
|
}
|
||
|
|
|
||
|
|
// FindByID 根据ID查找文件夹
|
||
|
|
func (r *FolderRepository) FindByID(id uint64) (*model.Folder, error) {
|
||
|
|
var folder model.Folder
|
||
|
|
err := config.DB.First(&folder, id).Error
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return &folder, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// ListByParent 查询子文件夹列表
|
||
|
|
func (r *FolderRepository) ListByParent(parentID uint64, ownerID uint64) ([]model.Folder, error) {
|
||
|
|
var folders []model.Folder
|
||
|
|
err := config.DB.Where("parent_id = ? AND owner_id = ? AND status = 1", parentID, ownerID).
|
||
|
|
Order("name ASC").Find(&folders).Error
|
||
|
|
return folders, err
|
||
|
|
}
|
||
|
|
|
||
|
|
// FindByPath 根据路径查找文件夹
|
||
|
|
func (r *FolderRepository) FindByPath(path string, ownerID uint64) (*model.Folder, error) {
|
||
|
|
var folder model.Folder
|
||
|
|
err := config.DB.Where("path = ? AND owner_id = ? AND status = 1", path, ownerID).First(&folder).Error
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return &folder, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// FindByNameAndParent 根据名称和父文件夹查找(用于判断重名)
|
||
|
|
func (r *FolderRepository) FindByNameAndParent(name string, parentID uint64, ownerID uint64) (*model.Folder, error) {
|
||
|
|
var folder model.Folder
|
||
|
|
err := config.DB.Where("name = ? AND parent_id = ? AND owner_id = ? AND status = 1",
|
||
|
|
name, parentID, ownerID).First(&folder).Error
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return &folder, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// ListTrashed 查询回收站文件夹
|
||
|
|
func (r *FolderRepository) ListTrashed(ownerID uint64) ([]model.Folder, error) {
|
||
|
|
var folders []model.Folder
|
||
|
|
err := config.DB.Where("owner_id = ? AND status = 0", ownerID).
|
||
|
|
Order("updated_at DESC").Find(&folders).Error
|
||
|
|
return folders, err
|
||
|
|
}
|
||
|
|
|
||
|
|
// Update 更新文件夹
|
||
|
|
func (r *FolderRepository) Update(folder *model.Folder) error {
|
||
|
|
return config.DB.Save(folder).Error
|
||
|
|
}
|
||
|
|
|
||
|
|
// SoftDelete 软删除
|
||
|
|
func (r *FolderRepository) SoftDelete(id uint64) error {
|
||
|
|
return config.DB.Model(&model.Folder{}).Where("id = ?", id).Update("status", 0).Error
|
||
|
|
}
|
||
|
|
|
||
|
|
// Restore 从回收站恢复
|
||
|
|
func (r *FolderRepository) Restore(id uint64) error {
|
||
|
|
return config.DB.Model(&model.Folder{}).Where("id = ?", id).Update("status", 1).Error
|
||
|
|
}
|
||
|
|
|
||
|
|
// HardDelete 永久删除
|
||
|
|
func (r *FolderRepository) HardDelete(id uint64) error {
|
||
|
|
return config.DB.Unscoped().Delete(&model.Folder{}, id).Error
|
||
|
|
}
|
||
|
|
|
||
|
|
// GetChildrenPath 获取所有子孙文件夹的路径前缀(用于级联操作)
|
||
|
|
func (r *FolderRepository) GetChildrenByPath(pathPrefix string, ownerID uint64) ([]model.Folder, error) {
|
||
|
|
var folders []model.Folder
|
||
|
|
err := config.DB.Where("path LIKE ? AND owner_id = ?",
|
||
|
|
pathPrefix+"%", ownerID).Find(&folders).Error
|
||
|
|
return folders, err
|
||
|
|
}
|