初始化
This commit is contained in:
120
server/controller/folder_controller.go
Normal file
120
server/controller/folder_controller.go
Normal file
@@ -0,0 +1,120 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"seeyon-filesystem/dto"
|
||||
"seeyon-filesystem/middleware"
|
||||
"seeyon-filesystem/service"
|
||||
"seeyon-filesystem/utils"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// FolderController 文件夹控制器
|
||||
type FolderController struct {
|
||||
folderService *service.FolderService
|
||||
}
|
||||
|
||||
// NewFolderController 创建文件夹控制器实例
|
||||
func NewFolderController() *FolderController {
|
||||
return &FolderController{
|
||||
folderService: service.NewFolderService(),
|
||||
}
|
||||
}
|
||||
|
||||
// Create 创建文件夹
|
||||
// POST /api/folder
|
||||
func (c *FolderController) Create(ctx *gin.Context) {
|
||||
ownerID := middleware.GetCurrentUserID(ctx)
|
||||
|
||||
var req dto.CreateFolderRequest
|
||||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||
utils.BadRequest(ctx, "参数错误: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
result, err := c.folderService.Create(&req, ownerID)
|
||||
if err != nil {
|
||||
utils.ServerError(ctx, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
utils.Success(ctx, result)
|
||||
}
|
||||
|
||||
// ListChildren 获取文件夹子项
|
||||
// GET /api/folder/:id/children 或 GET /api/folder/root
|
||||
func (c *FolderController) ListChildren(ctx *gin.Context) {
|
||||
ownerID := middleware.GetCurrentUserID(ctx)
|
||||
|
||||
idStr := ctx.Param("id")
|
||||
var folderID uint64
|
||||
if idStr == "root" {
|
||||
folderID = 0
|
||||
} else {
|
||||
var err error
|
||||
folderID, err = strconv.ParseUint(idStr, 10, 64)
|
||||
if err != nil {
|
||||
utils.BadRequest(ctx, "无效的文件夹ID")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
result, err := c.folderService.ListChildren(folderID, ownerID)
|
||||
if err != nil {
|
||||
utils.ServerError(ctx, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
utils.Success(ctx, result)
|
||||
}
|
||||
|
||||
// Rename 重命名文件夹
|
||||
// PUT /api/folder/:id/rename
|
||||
func (c *FolderController) Rename(ctx *gin.Context) {
|
||||
ownerID := middleware.GetCurrentUserID(ctx)
|
||||
|
||||
id, err := strconv.ParseUint(ctx.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
utils.BadRequest(ctx, "无效的文件夹ID")
|
||||
return
|
||||
}
|
||||
|
||||
var req dto.RenameRequest
|
||||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||
utils.BadRequest(ctx, "参数错误: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if err := c.folderService.Rename(id, req.NewName, ownerID, req.Version); err != nil {
|
||||
if strings.Contains(err.Error(), "已被其他人修改") {
|
||||
utils.Error(ctx, 409, 409, err.Error())
|
||||
return
|
||||
}
|
||||
utils.ServerError(ctx, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
utils.SuccessWithMessage(ctx, "重命名成功", nil)
|
||||
}
|
||||
|
||||
// Delete 删除文件夹
|
||||
// DELETE /api/folder/:id
|
||||
func (c *FolderController) Delete(ctx *gin.Context) {
|
||||
ownerID := middleware.GetCurrentUserID(ctx)
|
||||
|
||||
id, err := strconv.ParseUint(ctx.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
utils.BadRequest(ctx, "无效的文件夹ID")
|
||||
return
|
||||
}
|
||||
|
||||
if err := c.folderService.Delete(id, ownerID); err != nil {
|
||||
utils.ServerError(ctx, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
utils.SuccessWithMessage(ctx, "已移入回收站", nil)
|
||||
}
|
||||
Reference in New Issue
Block a user