Files
zzyxyz_go_api/internal/models/bookmark.go
zzy 6e513dbeb8 fix(bookmark): 更新书签模型字段为指针类型以支持可选字段
将 Bookmark 模型中的 Link、Detail 和 Description 字段修改为指针类型,
使其在数据库中可以为 NULL,并更新了对应的请求和响应处理逻辑。
同时修复根目录 ParentPathID 的初始化值为 -1。
此外,测试用例中暂时注释掉时间更新的断言并添加 FIXME 注释。
主程序监听地址从 127.0.0.1 改为 localhost。
2025-09-21 20:59:51 +08:00

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:"type:url"`
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
}