Documentation
¶
Index ¶
- Variables
- type PlaceInput
- type Store
- func (s *Store) AllocateChapterSortOrder(ctx context.Context, tx *gorm.DB, novelID int64, volumeID *int64) (int, error)
- func (s *Store) Delete(ctx context.Context, tx *gorm.DB, novelID, volumeID int64) error
- func (s *Store) GetByID(ctx context.Context, tx *gorm.DB, novelID, volumeID int64) (*Volume, error)
- func (s *Store) LastByNovel(ctx context.Context, tx *gorm.DB, novelID int64) (*Volume, error)
- func (s *Store) ListByNovel(ctx context.Context, tx *gorm.DB, novelID int64) ([]Volume, error)
- func (s *Store) Place(ctx context.Context, tx *gorm.DB, input PlaceInput) (*Volume, error)
- func (s *Store) Update(ctx context.Context, tx *gorm.DB, novelID, volumeID int64, name string) error
- type Volume
Constants ¶
This section is empty.
Variables ¶
View Source
var ErrHasChapters = errors.New("卷下仍有章节,请先移动或删除这些章节")
ErrHasChapters 卷下仍有章节,不可删除。
View Source
var ErrNameTaken = errors.New("卷名已存在")
ErrNameTaken 同一小说内卷名重复。
View Source
var ErrNotFound = errors.New("卷不存在(或不属于当前小说)")
ErrNotFound 卷不存在,或不属于该小说。
Functions ¶
This section is empty.
Types ¶
type PlaceInput ¶
type PlaceInput struct {
NovelID int64 `json:"novel_id"`
SourceVolumeID *int64 `json:"source_volume_id"`
Name *string `json:"name"`
BeforeVolumeID *int64 `json:"before_volume_id"`
}
PlaceInput 描述创建或移动卷的目标位置。 SourceVolumeID 为空时创建新卷,此时 Name 必填;有值时移动已有卷,Name 必须为空。 BeforeVolumeID 为空表示追加到小说末尾。
type Store ¶
Store 管理 Volume 持久化。
事务约定:每个方法都接受一个可选的 tx 参数——
- 事务外调用传 nil,方法用 Store 自己的连接
- 事务内调用必须传 tx,否则会去抢本应用唯一的连接(SetMaxOpenConns(1))而死锁
func (*Store) AllocateChapterSortOrder ¶
func (s *Store) AllocateChapterSortOrder(ctx context.Context, tx *gorm.DB, novelID int64, volumeID *int64) (int, error)
AllocateChapterSortOrder 为「新建章节」分配所属分组内的 sort_order,返回末尾位置。
分组规则:volumeID 非 nil 时为对应卷;nil 时为未分卷组。 新建章节总是追加到目标分组末尾,不影响其他卷或未分卷组的 sort_order。 事务内必须传 tx。卷不存在时返回 ErrNotFound。
func (*Store) LastByNovel ¶
LastByNovel 返回 sort_order 最大的卷(最后一卷),无卷返回 (nil, nil)。
func (*Store) ListByNovel ¶
ListByNovel 返回该小说全部卷,按 sort_order 升序。
type Volume ¶
type Volume struct {
ID int64 `gorm:"column:id;primaryKey;autoIncrement" json:"id"`
NovelID int64 `gorm:"column:novel_id;not null;uniqueIndex:uk_novel_volume_sort;uniqueIndex:uk_novel_volume_name;index" json:"novel_id"`
Name string `gorm:"column:name;not null;uniqueIndex:uk_novel_volume_name" json:"name"`
SortOrder int `gorm:"column:sort_order;not null;uniqueIndex:uk_novel_volume_sort" json:"sort_order"`
CreatedAt time.Time `gorm:"column:created_at;autoCreateTime" json:"created_at"`
UpdatedAt time.Time `gorm:"column:updated_at;autoUpdateTime" json:"updated_at"`
OutlineFilePath string `gorm:"-" json:"outline_file_path"`
}
Volume 是小说分卷的元数据。 卷纲正文以文件形式存储在 Git 仓库 volumes/id_{id}.md,DB 仅保存索引。
设计原则:
- sort_order 是内部排序键,仅用于 ORDER BY,对用户/AI 不可见
- "第 N 卷" 实时按 (novel_id, sort_order) ASC 排序后位次生成,不存 DB
- 卷的增删移动只改 sort_order,不影响 chapter 文件路径(chapter 用 chapter.id 命名)
约束:
- (novel_id, sort_order) 唯一索引:同一小说内卷顺序不可重复
- (novel_id, name) 唯一索引:同一小说内卷名不可重复
Click to show internal directories.
Click to hide internal directories.