更新前后端代码
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"filestoragesystem/internal/middleware"
|
||||
"filestoragesystem/internal/utils"
|
||||
)
|
||||
|
||||
// ProjectController 项目控制器
|
||||
type ProjectController struct {
|
||||
*Base
|
||||
}
|
||||
|
||||
// NewProjectController 创建项目控制器
|
||||
func NewProjectController(b *Base) *ProjectController { return &ProjectController{Base: b} }
|
||||
|
||||
type createProjectReq struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
Description string `json:"description"`
|
||||
StorageLimit int64 `json:"storage_limit"` // 0使用默认
|
||||
}
|
||||
|
||||
// Create 创建项目
|
||||
func (ctl *ProjectController) Create(c *gin.Context) {
|
||||
var req createProjectReq
|
||||
if !bindJSON(c, &req) {
|
||||
return
|
||||
}
|
||||
userID := middleware.CurrentUserID(c)
|
||||
p, err := ctl.svc.Project.Create(userID, req.Name, req.Description, req.StorageLimit)
|
||||
if err != nil {
|
||||
ctl.svc.RecordOpLog(ctl.opCtx(c), "create", "project", 0, req.Name, false, err.Error())
|
||||
utils.FailMsg(c, err.Error())
|
||||
return
|
||||
}
|
||||
ctl.svc.RecordOpLog(ctl.opCtx(c), "create", "project", p.ID, p.Name, true, "")
|
||||
utils.OK(c, p)
|
||||
}
|
||||
|
||||
// List 项目列表
|
||||
func (ctl *ProjectController) List(c *gin.Context) {
|
||||
userID := middleware.CurrentUserID(c)
|
||||
page, pageSize := utils.ParsePage(c)
|
||||
all := c.Query("all") == "true" && middleware.IsAdmin(c)
|
||||
projects, total, err := ctl.svc.Project.List(userID, c.Query("keyword"), all, page, pageSize)
|
||||
if err != nil {
|
||||
utils.ServerError(c, err.Error())
|
||||
return
|
||||
}
|
||||
utils.OKPage(c, projects, total, page, pageSize)
|
||||
}
|
||||
|
||||
// Get 项目详情
|
||||
func (ctl *ProjectController) Get(c *gin.Context) {
|
||||
id, ok := paramID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
userID := middleware.CurrentUserID(c)
|
||||
p, err := ctl.svc.Project.Get(userID, id, middleware.IsAdmin(c))
|
||||
if err != nil {
|
||||
utils.FailMsg(c, err.Error())
|
||||
return
|
||||
}
|
||||
utils.OK(c, p)
|
||||
}
|
||||
|
||||
type updateProjectReq struct {
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
StorageLimit int64 `json:"storage_limit"`
|
||||
}
|
||||
|
||||
// Update 更新项目
|
||||
func (ctl *ProjectController) Update(c *gin.Context) {
|
||||
id, ok := paramID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var req updateProjectReq
|
||||
if !bindJSON(c, &req) {
|
||||
return
|
||||
}
|
||||
userID := middleware.CurrentUserID(c)
|
||||
p, err := ctl.svc.Project.Update(userID, id, middleware.IsAdmin(c), req.Name, req.Description, req.StorageLimit)
|
||||
if err != nil {
|
||||
utils.FailMsg(c, err.Error())
|
||||
return
|
||||
}
|
||||
ctl.svc.RecordOpLog(ctl.opCtx(c), "update", "project", id, p.Name, true, "")
|
||||
utils.OK(c, p)
|
||||
}
|
||||
|
||||
// Delete 删除项目
|
||||
func (ctl *ProjectController) Delete(c *gin.Context) {
|
||||
id, ok := paramID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
userID := middleware.CurrentUserID(c)
|
||||
p, _ := ctl.svc.Project.Get(userID, id, middleware.IsAdmin(c))
|
||||
if err := ctl.svc.Project.Delete(userID, id, middleware.IsAdmin(c)); err != nil {
|
||||
utils.FailMsg(c, err.Error())
|
||||
return
|
||||
}
|
||||
name := ""
|
||||
if p != nil {
|
||||
name = p.Name
|
||||
}
|
||||
ctl.svc.RecordOpLog(ctl.opCtx(c), "delete", "project", id, name, true, "")
|
||||
utils.OKMsg(c, "项目已删除")
|
||||
}
|
||||
Reference in New Issue
Block a user