Help us shape the future of Go! Take the 2026 Go Developer Survey

volume

package
v1.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 13, 2026 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Index

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

type Store struct {
	DB *gorm.DB
	// contains filtered or unexported fields
}

Store 管理 Volume 持久化。

事务约定:每个方法都接受一个可选的 tx 参数——

  • 事务外调用传 nil,方法用 Store 自己的连接
  • 事务内调用必须传 tx,否则会去抢本应用唯一的连接(SetMaxOpenConns(1))而死锁

func NewStore

func NewStore(db *gorm.DB, logger *slog.Logger) *Store

NewStore 创建 volume 存储。

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) Delete

func (s *Store) Delete(ctx context.Context, tx *gorm.DB, novelID, volumeID int64) error

Delete 删除卷。卷下仍有章节时返回 ErrHasChapters。

func (*Store) GetByID

func (s *Store) GetByID(ctx context.Context, tx *gorm.DB, novelID, volumeID int64) (*Volume, error)

GetByID 按 id 取卷,并校验归属该小说。不存在返回 ErrNotFound。

func (*Store) LastByNovel

func (s *Store) LastByNovel(ctx context.Context, tx *gorm.DB, novelID int64) (*Volume, error)

LastByNovel 返回 sort_order 最大的卷(最后一卷),无卷返回 (nil, nil)。

func (*Store) ListByNovel

func (s *Store) ListByNovel(ctx context.Context, tx *gorm.DB, novelID int64) ([]Volume, error)

ListByNovel 返回该小说全部卷,按 sort_order 升序。

func (*Store) Place

func (s *Store) Place(ctx context.Context, tx *gorm.DB, input PlaceInput) (*Volume, error)

Place 创建或移动卷,并将其放到锚点卷前或小说末尾。 最终顺序会在同一事务内以两阶段写入落库,避免卷顺序唯一索引的中间态冲突。

func (*Store) Update

func (s *Store) Update(ctx context.Context, tx *gorm.DB, novelID, volumeID int64, name string) error

Update 重命名卷。名称未变时视为无操作。

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) 唯一索引:同一小说内卷名不可重复

func (Volume) TableName

func (Volume) TableName() string

TableName 指定 GORM 表名。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL