feat(api): doc folders curd implemention

This commit is contained in:
neo-f
2023-03-23 21:27:28 +08:00
parent 28edda5c7a
commit 6851fe95a0
17 changed files with 539 additions and 155 deletions

61
internal/router/auth.go Normal file
View File

@@ -0,0 +1,61 @@
package router
import (
"github.com/casdoor/casdoor-go-sdk/casdoorsdk"
"github.com/gofiber/fiber/v2"
"github.com/neo-f/soda"
"github.com/rs/zerolog/log"
)
func RegisterAuthRouter(app *soda.Soda) {
app.Get("/auth/sign-in", GetSignInURL).
AddTags("Auth").
SetSummary("登录").
SetParameters(OauthSchema{}).
OK()
app.Get("/auth/callback", TokenCallback).
AddTags("Auth").
SetSummary("Oauth回调地址").
SetParameters(OauthSchema{}).
OK()
}
type OauthSchema struct {
Code string `query:"code"`
State string `query:"state"`
}
func TokenCallback(c *fiber.Ctx) error {
k := c.Locals(soda.KeyParameter).(*OauthSchema)
token, err := casdoorsdk.GetOAuthToken(k.Code, k.State)
if err != nil {
return err
}
return c.JSON(token)
}
func GetSignInURL(c *fiber.Ctx) error {
url := casdoorsdk.GetSigninUrl("http://localhost:8080/auth/callback")
return c.Redirect(url)
}
var userKey = struct{}{}
func JWTRequired(c *fiber.Ctx) error {
jwt := c.Get("Authorization")
if jwt == "" {
return fiber.ErrUnauthorized
}
claims, err := casdoorsdk.ParseJwtToken(jwt[7:])
if err != nil {
log.Ctx(c.UserContext()).Error().Err(err).Msg("Unauthorized user")
return fiber.ErrUnauthorized
}
c.Locals(userKey, &claims.User)
return nil
}
func getAuth(c *fiber.Ctx) *casdoorsdk.User {
return c.Locals(userKey).(*casdoorsdk.User)
}

View File

@@ -1,23 +0,0 @@
package router
import (
"github.com/casdoor/casdoor-go-sdk/casdoorsdk"
"github.com/gofiber/fiber/v2"
"github.com/rs/zerolog/log"
)
var userKey = struct{}{}
func JWTRequired(c *fiber.Ctx) error {
jwt := c.Get("Authorization")
if jwt == "" {
return fiber.ErrUnauthorized
}
claims, err := casdoorsdk.ParseJwtToken(jwt)
if err != nil {
log.Ctx(c.UserContext()).Error().Err(err).Msg("Unauthorized user")
return fiber.ErrUnauthorized
}
c.Locals(userKey, claims.User)
return nil
}

View File

@@ -4,84 +4,50 @@ 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) {
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("更新文件夹").
AddJWTSecurity(JWTRequired).
SetParameters(schema.DocFolderID{}).
SetJSONRequestBody(schema.UpdateDocFolder{}).
AddJSONResponse(200, schema.DocFolder{}).OK()
app.Delete("/doc-folders/:id", nil).
AddTags("文件夹管理").
SetSummary("删除文件夹").
AddJWTSecurity(JWTRequired).
SetParameters(schema.DocFolderID{}).
AddJSONResponse(200, nil).OK()
}
func registerDocs(app *soda.Soda) {
app.Get("/docs", nil).
app.Get("/docs", ListDocs).
AddTags("文档管理").
SetSummary("获取文档列表").
AddJWTSecurity(JWTRequired).
SetParameters(schema.ListDocQuery{}).
AddJSONResponse(200, schema.DocList{}).OK()
app.Post("/docs", nil).
app.Post("/docs", CreateDoc).
AddTags("文档管理").
SetSummary("新建文档").
AddJWTSecurity(JWTRequired).
SetJSONRequestBody(schema.CreateDoc{}).
AddJSONResponse(200, schema.Doc{}).OK()
app.Put("/docs/:id", nil).
app.Put("/docs/:id", UpdateDoc).
AddTags("文档管理").
SetSummary("更新文档").
AddJWTSecurity(JWTRequired).
SetParameters(schema.DocID{}).
SetJSONRequestBody(schema.UpdateDoc{}).
AddJSONResponse(200, schema.Doc{}).OK()
app.Delete("/docs/:id", nil).
app.Delete("/docs/:id", DeleteDoc).
AddTags("文档管理").
SetSummary("获取文档列表").
AddJWTSecurity(JWTRequired).
SetParameters(schema.DocID{}).
AddJSONResponse(200, nil).OK()
app.Post("/docs/batch/delete", nil).
app.Post("/docs/batch/delete", DeleteDoc).
AddTags("文档管理").
SetSummary("批量-文件删除").
AddJWTSecurity(JWTRequired).
SetJSONRequestBody(schema.DocsBatchDelete{}).
AddJSONResponse(200, schema.DocBatchResults{}).OK()
app.Post("/docs/batch/update", nil).
app.Post("/docs/batch/update", UpdateDoc).
AddTags("文档管理").
SetSummary("批量-文件更新").
AddJWTSecurity(JWTRequired).
SetJSONRequestBody(schema.DocsBatchUpdate{}).
AddJSONResponse(200, schema.DocBatchResults{}).OK()
// get presigned url for tmp file upload
app.Get("/docs/upload-url", nil).
app.Get("/docs/upload-url", CreateUploadURL).
AddTags("文档管理").
SetSummary("获取临时上传文件用的预签名URL").
AddJWTSecurity(JWTRequired).
@@ -90,7 +56,7 @@ func registerDocs(app *soda.Soda) {
}
func ListDocs(c *fiber.Ctx) error {
auth := c.Locals(userKey).(*casdoorsdk.User)
auth := getAuth(c)
params := c.Locals(soda.KeyParameter).(*schema.ListDocQuery)
docs, total, err := service.ListDocs(c.UserContext(), auth, params)
if err != nil {
@@ -104,7 +70,7 @@ func ListDocs(c *fiber.Ctx) error {
}
func CreateDoc(c *fiber.Ctx) error {
auth := c.Locals(userKey).(*casdoorsdk.User)
auth := getAuth(c)
body := c.Locals(soda.KeyRequestBody).(*schema.CreateDoc)
doc, err := service.CreateDoc(c.UserContext(), auth, body)
@@ -115,7 +81,7 @@ func CreateDoc(c *fiber.Ctx) error {
}
func UpdateDoc(c *fiber.Ctx) error {
auth := c.Locals(userKey).(*casdoorsdk.User)
auth := getAuth(c)
params := c.Locals(soda.KeyParameter).(*schema.DocID)
body := c.Locals(soda.KeyRequestBody).(*schema.UpdateDoc)
@@ -127,7 +93,7 @@ func UpdateDoc(c *fiber.Ctx) error {
return c.JSON(doc.ToSchema(ctx))
}
func DeleteDoc(c *fiber.Ctx) error {
auth := c.Locals(userKey).(*casdoorsdk.User)
auth := getAuth(c)
params := c.Locals(soda.KeyParameter).(*schema.DocID)
ctx := c.UserContext()
@@ -138,7 +104,7 @@ func DeleteDoc(c *fiber.Ctx) error {
}
func DeleteDocBatch(c *fiber.Ctx) error {
auth := c.Locals(userKey).(*casdoorsdk.User)
auth := getAuth(c)
body := c.Locals(soda.KeyRequestBody).(*schema.DocsBatchDelete)
ctx := c.UserContext()
@@ -147,7 +113,7 @@ func DeleteDocBatch(c *fiber.Ctx) error {
}
func UpdateDocBatch(c *fiber.Ctx) error {
auth := c.Locals(userKey).(*casdoorsdk.User)
auth := getAuth(c)
body := c.Locals(soda.KeyRequestBody).(*schema.DocsBatchUpdate)
ctx := c.UserContext()
@@ -156,7 +122,7 @@ func UpdateDocBatch(c *fiber.Ctx) error {
}
func CreateUploadURL(c *fiber.Ctx) error {
auth := c.Locals(userKey).(*casdoorsdk.User)
auth := getAuth(c)
params := c.Locals(soda.KeyParameter).(*schema.CreateUploadURL)
ctx := c.UserContext()

View File

@@ -0,0 +1,96 @@
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)
}