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

View File

@@ -31,9 +31,8 @@ func newDocFolder(db *gorm.DB, opts ...gen.DOOption) docFolder {
_docFolder.CreatedAt = field.NewTime(tableName, "created_at")
_docFolder.UpdatedAt = field.NewTime(tableName, "updated_at")
_docFolder.Name = field.NewString(tableName, "name")
_docFolder.IsDeletable = field.NewBool(tableName, "is_deletable")
_docFolder.IsEditable = field.NewBool(tableName, "is_editable")
_docFolder.Path = field.NewString(tableName, "path")
_docFolder.IsDefault = field.NewBool(tableName, "is_default")
_docFolder.ParentPath = field.NewString(tableName, "parent_path")
_docFolder.CreatedBy = field.NewString(tableName, "created_by")
_docFolder.fillFieldMap()
@@ -44,16 +43,15 @@ func newDocFolder(db *gorm.DB, opts ...gen.DOOption) docFolder {
type docFolder struct {
docFolderDo
ALL field.Asterisk
OrgID field.String
ID field.String
CreatedAt field.Time
UpdatedAt field.Time
Name field.String
IsDeletable field.Bool
IsEditable field.Bool
Path field.String
CreatedBy field.String
ALL field.Asterisk
OrgID field.String
ID field.String
CreatedAt field.Time
UpdatedAt field.Time
Name field.String
IsDefault field.Bool
ParentPath field.String
CreatedBy field.String
fieldMap map[string]field.Expr
}
@@ -75,9 +73,8 @@ func (d *docFolder) updateTableName(table string) *docFolder {
d.CreatedAt = field.NewTime(table, "created_at")
d.UpdatedAt = field.NewTime(table, "updated_at")
d.Name = field.NewString(table, "name")
d.IsDeletable = field.NewBool(table, "is_deletable")
d.IsEditable = field.NewBool(table, "is_editable")
d.Path = field.NewString(table, "path")
d.IsDefault = field.NewBool(table, "is_default")
d.ParentPath = field.NewString(table, "parent_path")
d.CreatedBy = field.NewString(table, "created_by")
d.fillFieldMap()
@@ -95,15 +92,14 @@ func (d *docFolder) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
}
func (d *docFolder) fillFieldMap() {
d.fieldMap = make(map[string]field.Expr, 9)
d.fieldMap = make(map[string]field.Expr, 8)
d.fieldMap["org_id"] = d.OrgID
d.fieldMap["id"] = d.ID
d.fieldMap["created_at"] = d.CreatedAt
d.fieldMap["updated_at"] = d.UpdatedAt
d.fieldMap["name"] = d.Name
d.fieldMap["is_deletable"] = d.IsDeletable
d.fieldMap["is_editable"] = d.IsEditable
d.fieldMap["path"] = d.Path
d.fieldMap["is_default"] = d.IsDefault
d.fieldMap["parent_path"] = d.ParentPath
d.fieldMap["created_by"] = d.CreatedBy
}

View File

@@ -22,7 +22,7 @@ func GetStorage() *storage.StorageMinio {
func initMinio() {
log.Info().Msg("loading minio configs")
s, err := storage.NewObjectStorage(config.Get().Databases.OSS)
s, err := storage.NewObjectStorage(config.Get().Databases.Storage)
if err != nil {
log.Fatal().Msgf("storage client init failed: %v", err)
}