feat(api): prepare for svc implemention
This commit is contained in:
@@ -2,76 +2,31 @@ package router
|
||||
|
||||
import (
|
||||
"octopus/internal/schema"
|
||||
"octopus/internal/service"
|
||||
|
||||
"github.com/casdoor/casdoor-go-sdk/casdoorsdk"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/neo-f/soda"
|
||||
)
|
||||
|
||||
func RegisterDocRouter(app *soda.Soda) {
|
||||
app.Get("/docs", nil).
|
||||
AddTags("文档管理").
|
||||
SetSummary("获取文档列表").
|
||||
AddJWTSecurity(JWTRequired).
|
||||
SetParameters(schema.ListDocQuery{}).
|
||||
AddJSONResponse(200, schema.DocList{}).OK()
|
||||
|
||||
app.Post("/docs", nil).
|
||||
AddTags("文档管理").
|
||||
SetSummary("新建文档").
|
||||
AddJWTSecurity(JWTRequired).
|
||||
SetJSONRequestBody(schema.CreateDoc{}).
|
||||
AddJSONResponse(200, schema.Doc{}).OK()
|
||||
|
||||
app.Put("/docs/:id", nil).
|
||||
AddTags("文档管理").
|
||||
SetSummary("更新文档").
|
||||
AddJWTSecurity(JWTRequired).
|
||||
SetParameters(schema.DocID{}).
|
||||
SetJSONRequestBody(schema.UpdateDoc{}).
|
||||
AddJSONResponse(200, schema.Doc{}).OK()
|
||||
|
||||
// get presigned url for tmp file upload
|
||||
app.Get("/docs/upload-url", nil).
|
||||
AddTags("文档管理").
|
||||
SetSummary("获取临时上传文件用的预签名URL").
|
||||
AddJWTSecurity(JWTRequired).
|
||||
SetParameters(schema.CreateUploadURL{}).
|
||||
AddJSONResponse(200, schema.UploadURL{}).OK()
|
||||
|
||||
app.Delete("/docs/:id", nil).
|
||||
AddTags("文档管理").
|
||||
SetSummary("获取文档列表").
|
||||
AddJWTSecurity(JWTRequired).
|
||||
SetParameters(schema.DocID{}).
|
||||
AddJSONResponse(200, nil).OK()
|
||||
|
||||
app.Post("/docs/batch/delete", nil).
|
||||
AddTags("文档管理").
|
||||
SetSummary("批量-文件删除").
|
||||
AddJWTSecurity(JWTRequired).
|
||||
SetJSONRequestBody(schema.DocsBatchDelete{}).
|
||||
AddJSONResponse(200, schema.DocsBatchResults{}).OK()
|
||||
|
||||
app.Post("/docs/batch/update", nil).
|
||||
AddTags("文档管理").
|
||||
SetSummary("批量-文件更新").
|
||||
AddJWTSecurity(JWTRequired).
|
||||
SetJSONRequestBody(schema.DocsBatchUpdate{}).
|
||||
AddJSONResponse(200, schema.DocsBatchResults{}).OK()
|
||||
registerDocs(app)
|
||||
registerDocFolders(app)
|
||||
}
|
||||
|
||||
func registerDocFolders(app *soda.Soda) {
|
||||
app.Get("/doc-folders", nil).
|
||||
AddTags("文件夹管理").
|
||||
SetSummary("获取文件夹树").
|
||||
AddJWTSecurity(JWTRequired).
|
||||
SetParameters(schema.GetDocFolderTree{}).
|
||||
AddJSONResponse(200, schema.DocFolderWithChildren{}).OK()
|
||||
|
||||
app.Post("/doc-folders", nil).
|
||||
AddTags("文件夹管理").
|
||||
SetSummary("新建文件夹").
|
||||
AddJWTSecurity(JWTRequired).
|
||||
SetJSONRequestBody(schema.CreateDocFolder{}).
|
||||
AddJSONResponse(200, schema.DocFolder{}).OK()
|
||||
|
||||
app.Put("/doc-folders/:id", nil).
|
||||
AddTags("文件夹管理").
|
||||
SetSummary("更新文件夹").
|
||||
@@ -79,7 +34,6 @@ func RegisterDocRouter(app *soda.Soda) {
|
||||
SetParameters(schema.DocFolderID{}).
|
||||
SetJSONRequestBody(schema.UpdateDocFolder{}).
|
||||
AddJSONResponse(200, schema.DocFolder{}).OK()
|
||||
|
||||
app.Delete("/doc-folders/:id", nil).
|
||||
AddTags("文件夹管理").
|
||||
SetSummary("删除文件夹").
|
||||
@@ -87,3 +41,138 @@ func RegisterDocRouter(app *soda.Soda) {
|
||||
SetParameters(schema.DocFolderID{}).
|
||||
AddJSONResponse(200, nil).OK()
|
||||
}
|
||||
|
||||
func registerDocs(app *soda.Soda) {
|
||||
app.Get("/docs", nil).
|
||||
AddTags("文档管理").
|
||||
SetSummary("获取文档列表").
|
||||
AddJWTSecurity(JWTRequired).
|
||||
SetParameters(schema.ListDocQuery{}).
|
||||
AddJSONResponse(200, schema.DocList{}).OK()
|
||||
app.Post("/docs", nil).
|
||||
AddTags("文档管理").
|
||||
SetSummary("新建文档").
|
||||
AddJWTSecurity(JWTRequired).
|
||||
SetJSONRequestBody(schema.CreateDoc{}).
|
||||
AddJSONResponse(200, schema.Doc{}).OK()
|
||||
app.Put("/docs/:id", nil).
|
||||
AddTags("文档管理").
|
||||
SetSummary("更新文档").
|
||||
AddJWTSecurity(JWTRequired).
|
||||
SetParameters(schema.DocID{}).
|
||||
SetJSONRequestBody(schema.UpdateDoc{}).
|
||||
AddJSONResponse(200, schema.Doc{}).OK()
|
||||
app.Delete("/docs/:id", nil).
|
||||
AddTags("文档管理").
|
||||
SetSummary("获取文档列表").
|
||||
AddJWTSecurity(JWTRequired).
|
||||
SetParameters(schema.DocID{}).
|
||||
AddJSONResponse(200, nil).OK()
|
||||
app.Post("/docs/batch/delete", nil).
|
||||
AddTags("文档管理").
|
||||
SetSummary("批量-文件删除").
|
||||
AddJWTSecurity(JWTRequired).
|
||||
SetJSONRequestBody(schema.DocsBatchDelete{}).
|
||||
AddJSONResponse(200, schema.DocsBatchResults{}).OK()
|
||||
app.Post("/docs/batch/update", nil).
|
||||
AddTags("文档管理").
|
||||
SetSummary("批量-文件更新").
|
||||
AddJWTSecurity(JWTRequired).
|
||||
SetJSONRequestBody(schema.DocsBatchUpdate{}).
|
||||
AddJSONResponse(200, schema.DocsBatchResults{}).OK()
|
||||
// get presigned url for tmp file upload
|
||||
app.Get("/docs/upload-url", nil).
|
||||
AddTags("文档管理").
|
||||
SetSummary("获取临时上传文件用的预签名URL").
|
||||
AddJWTSecurity(JWTRequired).
|
||||
SetParameters(schema.CreateUploadURL{}).
|
||||
AddJSONResponse(200, schema.UploadURL{}).OK()
|
||||
}
|
||||
|
||||
func ListDocs(c *fiber.Ctx) error {
|
||||
auth := c.Locals(userClaimsKey).(*casdoorsdk.Claims)
|
||||
params := c.Locals(soda.KeyParameter).(*schema.ListDocQuery)
|
||||
docs, total, err := service.ListDocs(c.UserContext(), auth, params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
docSchemas := make([]*schema.Doc, 0, len(docs))
|
||||
for _, doc := range docs {
|
||||
docSchemas = append(docSchemas, doc.ToSchema(c.UserContext()))
|
||||
}
|
||||
return c.JSON(schema.DocList{Items: docSchemas, Total: total})
|
||||
}
|
||||
|
||||
func CreateDoc(c *fiber.Ctx) error {
|
||||
auth := c.Locals(userClaimsKey).(*casdoorsdk.Claims)
|
||||
body := c.Locals(soda.KeyRequestBody).(*schema.CreateDoc)
|
||||
|
||||
doc, err := service.CreateDoc(c.UserContext(), auth, body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.JSON(doc.ToSchema(c.UserContext()))
|
||||
}
|
||||
|
||||
func UpdateDoc(c *fiber.Ctx) error {
|
||||
auth := c.Locals(userClaimsKey).(*casdoorsdk.Claims)
|
||||
params := c.Locals(soda.KeyParameter).(*schema.DocID)
|
||||
body := c.Locals(soda.KeyRequestBody).(*schema.UpdateDoc)
|
||||
|
||||
ctx := c.UserContext()
|
||||
doc, err := service.UpdateDoc(ctx, auth, params.ID, body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.JSON(doc.ToSchema(ctx))
|
||||
}
|
||||
func DeleteDoc(c *fiber.Ctx) error {
|
||||
auth := c.Locals(userClaimsKey).(*casdoorsdk.Claims)
|
||||
params := c.Locals(soda.KeyParameter).(*schema.DocID)
|
||||
ctx := c.UserContext()
|
||||
|
||||
if err := service.DeleteDoc(ctx, auth, params.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
return c.JSON(nil)
|
||||
}
|
||||
|
||||
func DeleteDocBatch(c *fiber.Ctx) error {
|
||||
auth := c.Locals(userClaimsKey).(*casdoorsdk.Claims)
|
||||
body := c.Locals(soda.KeyRequestBody).(*schema.DocsBatchDelete)
|
||||
ctx := c.UserContext()
|
||||
|
||||
resp, err := service.DeleteDocBatch(ctx, auth, body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.JSON(resp)
|
||||
}
|
||||
|
||||
func UpdateDocBatch(c *fiber.Ctx) error {
|
||||
auth := c.Locals(userClaimsKey).(*casdoorsdk.Claims)
|
||||
body := c.Locals(soda.KeyRequestBody).(*schema.DocsBatchUpdate)
|
||||
ctx := c.UserContext()
|
||||
|
||||
resp, err := service.UpdateDocBatch(ctx, auth, body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.JSON(resp)
|
||||
}
|
||||
|
||||
func CreateUploadURL(c *fiber.Ctx) error {
|
||||
auth := c.Locals(userClaimsKey).(*casdoorsdk.Claims)
|
||||
params := c.Locals(soda.KeyParameter).(*schema.CreateUploadURL)
|
||||
ctx := c.UserContext()
|
||||
|
||||
u, objectName, err := service.CreateUploadURL(ctx, auth, params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.JSON(schema.UploadURL{
|
||||
URL: *u,
|
||||
ObjectName: objectName,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user