feat(api): docs basic curd implemention
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
package schema
|
||||
|
||||
import (
|
||||
"gorm.io/gen/field"
|
||||
)
|
||||
|
||||
const (
|
||||
maxPageSize = 1000
|
||||
defaultPageSize = 10
|
||||
@@ -35,22 +39,33 @@ type SortableQuery struct {
|
||||
SortBy *[]string `query:"sort_by" oai:"description=排序字段, 如: +id,-created_at,test 表示依次按照id正序,created_at倒序,test正序"`
|
||||
}
|
||||
|
||||
func (s *SortableQuery) GetOrderField() []SortField {
|
||||
type canOrder interface {
|
||||
GetFieldByName(fieldName string) (field.OrderExpr, bool)
|
||||
}
|
||||
|
||||
func (s *SortableQuery) GetOrderByFields(table canOrder) []field.Expr {
|
||||
if s.SortBy == nil {
|
||||
return nil
|
||||
}
|
||||
fields := make([]SortField, 0, len(*s.SortBy))
|
||||
fields := make([]field.Expr, 0, len(*s.SortBy))
|
||||
// query.Doc
|
||||
for _, v := range *s.SortBy {
|
||||
if v == "" {
|
||||
continue
|
||||
}
|
||||
switch v[0] {
|
||||
case '+':
|
||||
fields = append(fields, SortField{Field: v[1:], Asc: true})
|
||||
if field, ok := table.GetFieldByName(v[1:]); ok {
|
||||
fields = append(fields, field)
|
||||
}
|
||||
case '-':
|
||||
fields = append(fields, SortField{Field: v[1:], Asc: false})
|
||||
if field, ok := table.GetFieldByName(v[1:]); ok {
|
||||
fields = append(fields, field.Desc())
|
||||
}
|
||||
default:
|
||||
fields = append(fields, SortField{Field: v, Asc: true})
|
||||
if field, ok := table.GetFieldByName(v); ok {
|
||||
fields = append(fields, field)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,10 +2,7 @@ package schema
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"octopus/pkg/tools"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
type DocFolder struct {
|
||||
@@ -89,23 +86,13 @@ type DocsBatchUpdate struct {
|
||||
FolderID string `json:"folder_id" validate:"required" oai:"description=更新归属文件夹ID"`
|
||||
}
|
||||
|
||||
type DocActionResult struct {
|
||||
ID string `json:"id" oai:"description=文档ID"`
|
||||
Success bool `json:"success" oai:"description=操作是否成功"`
|
||||
Message *string `json:"message,omitempty" oai:"description=操作失败信息"`
|
||||
type DocBatchResult struct {
|
||||
ID string `json:"id" oai:"description=文档ID"`
|
||||
Success bool `json:"success" oai:"description=操作是否成功"`
|
||||
Error string `json:"message,omitempty" oai:"description=操作失败信息"`
|
||||
}
|
||||
|
||||
type DocsBatchResults []DocActionResult
|
||||
|
||||
func (q *ListDocQuery) Validate() error {
|
||||
sortable := tools.NewSet("uploaded_at")
|
||||
for _, s := range q.GetOrderField() {
|
||||
if !sortable.Has(s.Field) {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "unsupported order field")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
type DocBatchResults []DocBatchResult
|
||||
|
||||
type CreateUploadURL struct {
|
||||
FileName string `query:"file_name" validate:"required" oai:"description=上传文件名"`
|
||||
|
||||
Reference in New Issue
Block a user