feat(api): 初始化项目基础结构与API定义 新增 `.gitignore` 文件,忽略编译输出、生成代码及数据库文件。 新增 `README.md`,包含 Gin 框架和 Swagger 工具的安装与使用说明。 新增 `config/api.yaml`,定义 bookmarks 相关的文件夹与书签操作的 OpenAPI 3.0 接口规范。 新增 `config/cfg.yaml`,配置 oapi-codegen 工具生成 Gin 服务和模型代码。 新增 `go.mod` 和 `go.sum` 文件,初始化 Go 模块并引入 Gin、GORM、SQLite 及 oapi-codegen 等依赖。 ```
56 lines
1.8 KiB
Go
56 lines
1.8 KiB
Go
package models
|
|
|
|
import (
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// Bookmark 书签结构体
|
|
type Bookmark struct {
|
|
ID int64 `json:"id" gorm:"primaryKey"`
|
|
Name string `json:"name" gorm:"not null;index;size:255"`
|
|
Link string `json:"link" gorm:"not null"`
|
|
Detail string `json:"detail" gorm:"type:text"`
|
|
Description string `json:"description" gorm:"type:text"`
|
|
ParentPathID int64 `json:"parent_path_id" gorm:"index;not null;default:1"`
|
|
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`
|
|
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime"`
|
|
DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"index"`
|
|
}
|
|
|
|
// Folder 文件夹结构体
|
|
type Folder struct {
|
|
ID int64 `json:"id" gorm:"primaryKey"`
|
|
Name string `json:"name" gorm:"not null;index;size:255"`
|
|
ParentPathID int64 `json:"parent_path_id" gorm:"index;not null;default:1"`
|
|
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`
|
|
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime"`
|
|
DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"index"`
|
|
}
|
|
|
|
// IsValidParent 检查父文件夹ID是否有效
|
|
func (f *Folder) IsValidParent(db *gorm.DB, parentID int64) bool {
|
|
// 检查父文件夹是否存在且未被删除
|
|
if err := db.Where("id = ?", parentID).First(&Folder{}).Error; err != nil {
|
|
return false
|
|
}
|
|
|
|
// 防止循环引用(不能将文件夹设置为自己的子文件夹)
|
|
if parentID == f.ID {
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
// IsValidParent 检查书签的父文件夹ID是否有效
|
|
func (b *Bookmark) IsValidParent(db *gorm.DB, parentID int64) bool {
|
|
// 检查父文件夹是否存在且未被删除
|
|
if err := db.Where("id = ?", parentID).First(&Folder{}).Error; err != nil {
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|