octopus/internal/router/doc_folder.go

97 lines
2.4 KiB
Go
Raw Normal View History

package router
import (
"octopus/internal/schema"
"octopus/internal/service"
"github.com/gofiber/fiber/v2"
"github.com/neo-f/soda"
)
func RegisterDocFolderRouters(app *soda.Soda) {
app.Get("/doc-folders", GetDocFolderTree).
AddTags("文件夹管理").
SetSummary("获取文件夹树").
AddJWTSecurity(JWTRequired).
SetParameters(schema.GetDocFolderTree{}).
AddJSONResponse(200, schema.DocFolderWithChildren{}).
OK()
app.Post("/doc-folders", CreateDocFolder).
AddTags("文件夹管理").
SetSummary("新建文件夹").
AddJWTSecurity(JWTRequired).
SetJSONRequestBody(schema.CreateDocFolder{}).
AddJSONResponse(200, schema.DocFolder{}).
OK()
app.Put("/doc-folders/:id", UpdateDocFolder).
AddTags("文件夹管理").
SetSummary("更新文件夹").
AddJWTSecurity(JWTRequired).
SetParameters(schema.DocFolderID{}).
SetJSONRequestBody(schema.UpdateDocFolder{}).
AddJSONResponse(200, schema.DocFolder{}).
OK()
app.Delete("/doc-folders/:id", DeleteDocFolder).
AddTags("文件夹管理").
SetSummary("删除文件夹").
AddJWTSecurity(JWTRequired).
SetParameters(schema.DocFolderID{}).
AddJSONResponse(200, nil).
OK()
}
func GetDocFolderTree(c *fiber.Ctx) error {
auth := getAuth(c)
param := c.Locals(soda.KeyParameter).(*schema.GetDocFolderTree)
ctx := c.UserContext()
tree, err := service.GetDocFolderTree(ctx, auth, param)
if err != nil {
return err
}
schemas := make([]*schema.DocFolderWithChildren, len(tree))
for i, t := range tree {
schemas[i] = t.ToTreeSchema()
}
return c.JSON(schemas)
}
func CreateDocFolder(c *fiber.Ctx) error {
auth := getAuth(c)
body := c.Locals(soda.KeyRequestBody).(*schema.CreateDocFolder)
ctx := c.UserContext()
doc, err := service.CreateDocFolder(ctx, auth, body)
if err != nil {
return err
}
return c.JSON(doc.ToSchema())
}
func UpdateDocFolder(c *fiber.Ctx) error {
auth := getAuth(c)
param := c.Locals(soda.KeyParameter).(*schema.DocFolderID)
body := c.Locals(soda.KeyRequestBody).(*schema.UpdateDocFolder)
ctx := c.UserContext()
doc, err := service.UpdateDocFolder(ctx, auth, param.ID, body)
if err != nil {
return err
}
return c.JSON(doc.ToSchema())
}
func DeleteDocFolder(c *fiber.Ctx) error {
auth := getAuth(c)
param := c.Locals(soda.KeyParameter).(*schema.DocFolderID)
ctx := c.UserContext()
if err := service.DeleteDocFolder(ctx, auth, param.ID); err != nil {
return err
}
return c.JSON(nil)
}