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

View File

@@ -3,8 +3,8 @@ package model
import "time"
type Base struct {
OrgID string `gorm:"column:org_id;type:varchar;primaryKey;comment:组织ID"` // 组织ID
ID string `gorm:"column:id;type:varchar;primaryKey;comment:ID"` // ID
OrgID string `gorm:"column:org_id;type:varchar;primaryKey;comment:组织ID"`
ID string `gorm:"column:id;type:varchar;primaryKey;comment:ID"`
CreatedAt time.Time `gorm:"column:created_at;comment:创建时间"`
UpdatedAt time.Time `gorm:"column:updated_at;comment:更新时间"`
}

View File

@@ -5,6 +5,7 @@ import (
"net/url"
"octopus/internal/dal"
"octopus/internal/schema"
"strings"
"time"
"github.com/rs/xid"
@@ -16,24 +17,29 @@ const TableNameDoc = "docs"
type DocFolder struct {
Base
Name string `gorm:"column:name;type:varchar;not null"` // 文件夹名称
IsDeletable bool `gorm:"column:is_deletable;type:boolean;not null;default:true"` // 是否允许被删除
IsEditable bool `gorm:"column:is_editable;type:boolean;not null;default:true"` // 是否允许被修改
Path string `gorm:"column:path;type:varchar;index:idx_path"` // 路径索引
CreatedBy string `gorm:"column:created_by;type:varchar;not null"` // 创建人
Name string `gorm:"column:name;type:varchar;not null"` // 文件夹名称
IsDefault bool `gorm:"column:is_default;type:boolean;not null;default:false"` // 是否默认分组
ParentPath string `gorm:"column:parent_path;type:varchar;index:idx_path"` // 路径索引
CreatedBy string `gorm:"column:created_by;type:varchar;not null"` // 创建人
}
func (*DocFolder) TableName() string {
return TableNameDocFolder
}
func (f *DocFolder) ParentID() string {
if f.ParentPath == "" {
return ""
}
parts := strings.Split(f.ParentPath, "/")
return parts[len(parts)-1]
}
func (df *DocFolder) ToSchema() *schema.DocFolder {
return &schema.DocFolder{
ID: df.ID,
IsDeletable: df.IsDeletable,
IsEditable: df.IsEditable,
CreatedAt: df.CreatedAt,
UpdatedAt: df.UpdatedAt,
ID: df.ID,
Name: df.Name,
CreatedAt: df.CreatedAt,
UpdatedAt: df.UpdatedAt,
}
}
@@ -42,6 +48,28 @@ func (d *DocFolder) BeforeCreate(*gorm.DB) error {
return nil
}
// 在返回树结构的时候有用
type DocFolderWithChildren struct {
*DocFolder
Children []*DocFolderWithChildren // 子文件夹
}
func (df *DocFolderWithChildren) ToTreeSchema() *schema.DocFolderWithChildren {
var traverse func(folder *DocFolderWithChildren) *schema.DocFolderWithChildren
traverse = func(folder *DocFolderWithChildren) *schema.DocFolderWithChildren {
children := make([]*schema.DocFolderWithChildren, 0, len(folder.Children))
for _, child := range folder.Children {
children = append(children, traverse(child))
}
return &schema.DocFolderWithChildren{
DocFolder: folder.ToSchema(),
Children: children,
}
}
return traverse(df)
}
type Doc struct {
Base
Name string `gorm:"column:name;type:varchar;not null"` // 文件夹名称
@@ -49,7 +77,7 @@ type Doc struct {
IsEditable bool `gorm:"column:is_editable;type:boolean;not null;default:true"` // 是否允许编辑名称
FolderID string `gorm:"column:folder_id;type:varchar;index:idx_folder_id"` // 文件夹ID
ObjectName string `gorm:"column:object_name;type:varchar"` // 对象存储中对应的object_name
UploadedAt time.Time `gorm:"column:uploaded_at;type:datetime"` // 上传时间
UploadedAt time.Time `gorm:"column:uploaded_at"` // 上传时间
CreatedBy string `gorm:"column:created_by;type:varchar;not null"` // 创建人
Folder *DocFolder `gorm:"foreignKey:FolderID;references:ID"`
@@ -62,8 +90,6 @@ func (d *Doc) ToSchema(ctx context.Context) *schema.Doc {
ID: d.ID,
Folder: d.Folder.ToSchema(),
PresignedURL: url,
IsDeletable: d.IsDeletable,
IsEditable: d.IsEditable,
UploadedAt: d.UploadedAt,
CreatedAt: d.CreatedAt,
UpdatedAt: d.UpdatedAt,
@@ -77,6 +103,7 @@ func (d *Doc) PresignedURL(ctx context.Context) (*url.URL, error) {
func (*Doc) TableName() string {
return TableNameDoc
}
func (d *Doc) BeforeCreate(*gorm.DB) error {
d.ID = xid.New().String()
return nil