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

chapter

package
v1.6.1 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Chapter

type Chapter struct {
	ID              int64     `gorm:"column:id;primaryKey;autoIncrement"  json:"id"`
	NovelID         int64     `gorm:"column:novel_id;not null;index"      json:"novel_id"`
	VolumeID        *int64    `gorm:"column:volume_id;index"              json:"volume_id"` // 可空外键 → volumes.id,NULL=未分卷
	SortOrder       int       `gorm:"column:sort_order;default:0"        json:"sort_order"` // 所属分组内的内部排序键
	Title           string    `gorm:"column:title"                       json:"title"`
	Summary         string    `gorm:"column:summary"                     json:"summary"` // AI 生成的章节简介
	WordCount       int       `gorm:"column:word_count;default:0"        json:"word_count"`
	CreatedAt       time.Time `gorm:"column:created_at;autoCreateTime"   json:"created_at"`
	UpdatedAt       time.Time `gorm:"column:updated_at;autoUpdateTime"   json:"updated_at"`
	FilePath        string    `gorm:"-"                                  json:"file_path"` // 不存 DB,由 git.ChapterPath 计算
	OutlineFilePath string    `gorm:"-" json:"outline_file_path"`                          // 不存 DB,由 git.OutlinePath 计算
	// ReadingNumber 是当前阅读顺序的展示编号,不持久化。
	ReadingNumber int `gorm:"-" json:"reading_number"`
}

Chapter 是章节元数据,正文和大纲以文件形式存储在 Git 仓库中。 DB 仅保存索引和统计信息,不存实际内容。

阅读顺序由 VolumeID/SortOrder 决定,展示的“第几章”按当前顺序实时计算, 不作为章节记录的持久化字段。

func (Chapter) TableName

func (Chapter) TableName() string

TableName 指定 GORM 表名。

type ChapterCacheInvalidator added in v1.6.0

type ChapterCacheInvalidator interface {
	DeleteCachedChapter(novelID, chapterID int64)
}

ChapterCacheInvalidator 清理某章节的搜索缓存。

type ChapterChunkCleaner added in v1.6.0

type ChapterChunkCleaner interface {
	DeleteChapterChunks(ctx context.Context, novelID, chapterID int64) error
}

ChapterChunkCleaner 清理某章节派生的向量数据。

type DeleteResult added in v1.6.0

type DeleteResult struct {
	Deleted    bool               `json:"deleted"`
	References []deletion.Blocker `json:"references"`
}

DeleteResult 描述章节删除的结果。References 非空时 Deleted 为 false。

type ListByNovelOptions

type ListByNovelOptions struct {
	PageParams storage.PageParams
	Order      string // "asc"(默认) 或 "desc",按阅读顺序排序
}

ListByNovelOptions 是 ListByNovel 的可选参数。零值即可直接使用(默认升序)。

type PlaceInput added in v1.6.0

type PlaceInput struct {
	NovelID         int64   `json:"novel_id"`
	SourceChapterID *int64  `json:"source_chapter_id"`
	Title           *string `json:"title"`
	TargetVolumeID  *int64  `json:"target_volume_id"`
	BeforeChapterID *int64  `json:"before_chapter_id"`
}

PlaceInput 描述创建或移动章节的目标位置。 SourceChapterID 为空时创建章节,此时 Title 必填;有值时移动已有章节,Title 必须为空。 TargetVolumeID 为空表示未分卷组,BeforeChapterID 为空表示追加到目标组末尾。

type Service added in v1.6.0

type Service struct {
	// contains filtered or unexported fields
}

Service 编排章节用例涉及的数据库、Git 文件与派生数据生命周期。

func NewService added in v1.6.0

func NewService(store *Store, volumes *volume.Store, deletionGuard *deletion.Guard, logger *slog.Logger,
	chunkCleaner func() ChapterChunkCleaner, cacheInvalidator func() ChapterCacheInvalidator) *Service

NewService 创建章节领域服务。provider 允许可选基础设施在运行时延迟初始化。

func (*Service) CreateDefault added in v1.6.0

func (s *Service) CreateDefault(ctx context.Context, novelID int64, title string) (*Chapter, error)

CreateDefault 按既有规则在最后一卷末尾创建章节;没有卷时创建到未分卷组。

func (*Service) Delete added in v1.6.0

func (s *Service) Delete(ctx context.Context, novelID, chapterID int64) (*DeleteResult, error)

Delete 删除没有交叉引用的章节及其正文、大纲与派生索引。

func (*Service) Place added in v1.6.0

func (s *Service) Place(ctx context.Context, input PlaceInput) (*Chapter, error)

Place 创建或移动章节,并将其放入目标章节组的指定位置。

type Store

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

Store 管理 Chapter 持久化。

事务约定:每个方法都接受可选 tx;事务外传 nil,事务内必须传入 tx, 避免在 SetMaxOpenConns(1) 时从 Store 自身连接重新取连接而死锁。

func NewStore

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

NewStore 创建 chapter 存储。

func (*Store) CountByNovel added in v1.6.0

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

CountByNovel 返回小说当前的章节总数。 阅读顺序中的章节号从 1 连续编号,因此该值也等于当前最大的展示章节号。

func (*Store) Create added in v1.6.0

func (s *Store) Create(ctx context.Context, tx *gorm.DB, novelID int64, volumeID *int64, title string) (*Chapter, error)

Create 新建章节记录,并追加到指定卷或未分卷组的末尾。 volumeID 为 nil 时创建未分卷章节;非 nil 时必须属于该小说。 tx 可为 nil;传入时创建与 sort_order 分配使用同一事务。

func (*Store) Delete added in v1.6.0

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

Delete 删除指定小说中的章节记录。

func (*Store) GetByID added in v1.6.0

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

GetByID 按 novel_id + id 取单章。

func (*Store) GetReadingNumberByID added in v1.6.0

func (s *Store) GetReadingNumberByID(ctx context.Context, tx *gorm.DB, novelID, chapterID int64) (int, error)

GetReadingNumberByID 返回章节在当前阅读顺序中的 1-based 位次。 章节号由卷顺序、卷内 sort_order 和未分卷末尾规则实时计算,不依赖 chapter_number 列。

func (*Store) GetReadingNumbersByNovel added in v1.6.0

func (s *Store) GetReadingNumbersByNovel(ctx context.Context, tx *gorm.DB, novelID int64) (map[int64]int, error)

GetReadingNumbersByNovel 返回小说中每个章节在当前阅读顺序中的 1-based 位次。

func (*Store) GetRecent

func (s *Store) GetRecent(ctx context.Context, tx *gorm.DB, novelID int64, limit int) ([]Chapter, error)

GetRecent 取阅读顺序最后 N 章,按阅读顺序倒序返回。

func (*Store) ListAllByNovel

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

ListAllByNovel 返回某小说的全部章节(不分页),按阅读顺序升序。

func (*Store) ListByNovel

func (s *Store) ListByNovel(ctx context.Context, tx *gorm.DB, novelID int64, opts ListByNovelOptions) (*storage.PageResult[Chapter], error)

ListByNovel 分页列出某小说的章节。

func (*Store) MissingIDsByNovel added in v1.6.0

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

MissingIDsByNovel 返回不存在或不属于指定小说的章节 ID。 返回值按输入中的首次出现顺序排列,重复 ID 只返回一次。

func (*Store) Place added in v1.6.0

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

Place 将新章节或已有章节放入目标章节组的指定位置。 所有顺序调整都在一个事务中完成,避免跨卷移动暴露中间的“先追加、再重排”状态。

func (*Store) SearchByNovel

func (s *Store) SearchByNovel(ctx context.Context, tx *gorm.DB, novelID int64, query string, limit int) ([]Chapter, error)

SearchByNovel 按关键词搜索某小说的章节,匹配标题和摘要。

func (*Store) UpdateTitle

func (s *Store) UpdateTitle(ctx context.Context, tx *gorm.DB, novelID, chapterID int64, title string) error

UpdateTitle 按 novel_id + id 更新章节标题。

Jump to

Keyboard shortcuts

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