Documentation
¶
Overview ¶
Package taxonomy 提供分类(树形)与标签。
这是第一个业务模块,也是模块形态的样板:自带迁移、经三平面注册面挂接口、 声明所用权限;核心对它一无所知,仅在 cmd/lumo/modules.go 装配。
Index ¶
- Constants
- Variables
- type Category
- type CategoryNode
- type Handler
- type Module
- type Slugger
- type Store
- func (s *Store) CreateCategory(ctx context.Context, c *Category) error
- func (s *Store) CreateTag(ctx context.Context, tag *Tag) error
- func (s *Store) DeleteCategory(ctx context.Context, id int64) error
- func (s *Store) DeleteTag(ctx context.Context, id int64) error
- func (s *Store) GetCategory(ctx context.Context, id int64) (*Category, error)
- func (s *Store) GetCategoryBySlug(ctx context.Context, slug string) (*Category, error)
- func (s *Store) GetTag(ctx context.Context, id int64) (*Tag, error)
- func (s *Store) GetTagBySlug(ctx context.Context, slug string) (*Tag, error)
- func (s *Store) ListCategories(ctx context.Context) ([]Category, error)
- func (s *Store) ListTags(ctx context.Context) ([]Tag, error)
- func (s *Store) NextPosition(ctx context.Context, parentID *int64) (int, error)
- func (s *Store) PageCategories(ctx context.Context, params api.PageParams) ([]Category, int, error)
- func (s *Store) PageTags(ctx context.Context, q string, params api.PageParams) ([]Tag, int, error)
- func (s *Store) UpdateCategory(ctx context.Context, c *Category) error
- func (s *Store) UpdateTag(ctx context.Context, tag *Tag) error
- type Tag
Constants ¶
const Name = "taxonomy"
Name 是模块名,也是迁移版本表的后缀(goose_db_version_taxonomy)。
Variables ¶
var ( // ErrNotFound 表示分类或标签不存在。 ErrNotFound = errors.New("对象不存在") // ErrSlugTaken 表示 slug 已被占用。 ErrSlugTaken = errors.New("slug 已被占用") // ErrNameTaken 表示名称已被占用(分类为同级、标签为全局)。 ErrNameTaken = errors.New("名称已被占用") // ErrParentNotFound 表示父分类不存在。 ErrParentNotFound = errors.New("父分类不存在") // ErrCycle 表示移动会让分类树成环。 ErrCycle = errors.New("父分类不能是自身或其后代") // ErrInvalid 表示字段违反了数据库约束。 ErrInvalid = errors.New("字段不合法") )
错误哨兵。处理器据此映射为 400 / 404 / 409。
Functions ¶
This section is empty.
Types ¶
type Category ¶
type Category struct {
bun.BaseModel `bun:"table:categories,alias:c"`
ID int64 `bun:"id,pk,autoincrement" json:"id" doc:"分类 ID"`
ParentID *int64 `bun:"parent_id" json:"parentId" doc:"父分类 ID,根分类为 null"`
Name string `bun:"name,notnull" json:"name" doc:"名称,同一父分类下唯一"`
Slug string `bun:"slug,notnull" json:"slug" doc:"URL 片段,全局唯一"`
Description string `bun:"description" json:"description" doc:"描述"`
CoverURL string `bun:"cover_url" json:"coverUrl" doc:"封面图地址"`
Position int `bun:"position" json:"position" doc:"同级排序,越小越靠前"`
CreatedAt time.Time `bun:"created_at,nullzero" json:"createdAt"`
UpdatedAt time.Time `bun:"updated_at,nullzero" json:"updatedAt"`
}
Category 是分类实体。分类构成一棵森林:ParentID 为 nil 的是根分类。
type CategoryNode ¶
type CategoryNode struct {
Category
Children []*CategoryNode `json:"children" doc:"子分类,按 position 排序"`
}
CategoryNode 是分类树的节点:分类本身加上按 position 排序的子分类。
func BuildTree ¶
func BuildTree(categories []Category) []*CategoryNode
BuildTree 由平铺列表构造分类森林。
同级按 position、name、id 排序;父分类不在列表中的节点当作根处理, 因此对任意子集都能得到一棵可渲染的树。叶子节点的 Children 为空切片而非 nil, 序列化后是 [] 而不是 null,前端不必做空值判断。
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler 提供分类与标签的 Console 与 Public 接口。
func NewHandler ¶
NewHandler 构造 Handler;slugify 为 nil 时使用保留中文的缺省策略。
type Module ¶
type Module struct {
// contains filtered or unexported fields
}
Module 是分类与标签模块。
func (*Module) Navigation ¶
func (m *Module) Navigation() app.Navigation
Navigation 实现 app.NavigationProvider。
分类与标签不设权限门槛:服务端对 Console 平面的读操作对任何已认证用户开放 (作者写文章要能选分类),写操作在页面内部按 taxonomies:manage 收起。
func (*Module) Permissions ¶
func (m *Module) Permissions() []app.Permission
Permissions 实现 app.PermissionProvider。
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store 提供分类与标签的持久化操作。
func (*Store) CreateCategory ¶
CreateCategory 创建分类;成功后回填 ID 与时间戳。
func (*Store) GetCategory ¶
GetCategory 按 ID 查分类。
func (*Store) GetCategoryBySlug ¶
GetCategoryBySlug 按 slug 查分类。
func (*Store) GetTagBySlug ¶
GetTagBySlug 按 slug 查标签。
func (*Store) ListCategories ¶
ListCategories 返回全部分类。
func (*Store) NextPosition ¶
NextPosition 返回给定父分类下新分类应取的排序值(末尾)。
func (*Store) PageCategories ¶
PageCategories 分页返回分类,并给出总数。
func (*Store) UpdateCategory ¶
UpdateCategory 更新分类的可编辑字段,并在事务内校验父分类存在且不成环。
type Tag ¶
type Tag struct {
bun.BaseModel `bun:"table:tags,alias:t"`
ID int64 `bun:"id,pk,autoincrement" json:"id" doc:"标签 ID"`
Name string `bun:"name,notnull" json:"name" doc:"名称,大小写不敏感唯一"`
Slug string `bun:"slug,notnull" json:"slug" doc:"URL 片段,全局唯一"`
Description string `bun:"description" json:"description" doc:"描述"`
Color string `bun:"color" json:"color" doc:"展示颜色,如 #3b82f6;空串表示主题默认"`
CreatedAt time.Time `bun:"created_at,nullzero" json:"createdAt"`
UpdatedAt time.Time `bun:"updated_at,nullzero" json:"updatedAt"`
}
Tag 是标签实体。