🚧 Refactor basic services

This commit is contained in:
2025-12-14 02:19:50 +08:00
parent d16905c0a3
commit cc4c2189dc
126 changed files with 18164 additions and 4247 deletions

View File

@@ -0,0 +1,65 @@
package schema
import (
"entgo.io/ent"
"entgo.io/ent/dialect/entsql"
"entgo.io/ent/schema"
"entgo.io/ent/schema/field"
"entgo.io/ent/schema/index"
"voidraft/internal/models/schema/mixin"
)
// Document holds the schema definition for the Document entity.
type Document struct {
ent.Schema
}
// Annotations of the Document.
func (Document) Annotations() []schema.Annotation {
return []schema.Annotation{
entsql.Annotation{Table: "documents"},
}
}
// Mixin of the Document.
func (Document) Mixin() []ent.Mixin {
return []ent.Mixin{
mixin.TimeMixin{},
mixin.SoftDeleteMixin{},
}
}
// Fields of the Document.
func (Document) Fields() []ent.Field {
return []ent.Field{
field.String("title").
MaxLen(255).
NotEmpty().
StructTag(`json:"title"`).
Comment("document title"),
field.Text("content").
Optional().
Default("\n∞∞∞text-a\n").
StructTag(`json:"content"`).
Comment("document content"),
field.Bool("locked").
Default(false).
StructTag(`json:"locked"`).
Comment("document locked status"),
}
}
// Edges of the Document.
func (Document) Edges() []ent.Edge {
return nil
}
// Indexes of the Document.
func (Document) Indexes() []ent.Index {
return []ent.Index{
index.Fields("title"),
index.Fields("created_at"),
index.Fields("updated_at"),
}
}

View File

@@ -0,0 +1,63 @@
package schema
import (
"entgo.io/ent"
"entgo.io/ent/dialect/entsql"
"entgo.io/ent/schema"
"entgo.io/ent/schema/field"
"entgo.io/ent/schema/index"
"voidraft/internal/models/schema/mixin"
)
// Extension holds the schema definition for the Extension entity.
type Extension struct {
ent.Schema
}
// Annotations of the Extension.
func (Extension) Annotations() []schema.Annotation {
return []schema.Annotation{
entsql.Annotation{Table: "extensions"},
}
}
// Mixin of the Extension.
func (Extension) Mixin() []ent.Mixin {
return []ent.Mixin{
mixin.TimeMixin{},
mixin.SoftDeleteMixin{},
}
}
// Fields of the Extension.
func (Extension) Fields() []ent.Field {
return []ent.Field{
field.String("key").
MaxLen(100).
NotEmpty().
Unique().
StructTag(`json:"key"`).
Comment("extension key"),
field.Bool("enabled").
Default(true).
StructTag(`json:"enabled"`).
Comment("extension enabled or not"),
field.JSON("config", map[string]interface{}{}).
Optional().
StructTag(`json:"config"`).
Comment("extension config"),
}
}
// Edges of the Extension.
func (Extension) Edges() []ent.Edge {
return nil
}
// Indexes of the Extension.
func (Extension) Indexes() []ent.Index {
return []ent.Index{
index.Fields("enabled"),
}
}

View File

@@ -0,0 +1,70 @@
package schema
import (
"entgo.io/ent"
"entgo.io/ent/dialect/entsql"
"entgo.io/ent/schema"
"entgo.io/ent/schema/field"
"entgo.io/ent/schema/index"
"voidraft/internal/models/schema/mixin"
)
// KeyBinding holds the schema definition for the KeyBinding entity.
type KeyBinding struct {
ent.Schema
}
// Annotations of the KeyBinding.
func (KeyBinding) Annotations() []schema.Annotation {
return []schema.Annotation{
entsql.Annotation{Table: "key_bindings"},
}
}
// Mixin of the KeyBinding.
func (KeyBinding) Mixin() []ent.Mixin {
return []ent.Mixin{
mixin.TimeMixin{},
mixin.SoftDeleteMixin{},
}
}
// Fields of the KeyBinding.
func (KeyBinding) Fields() []ent.Field {
return []ent.Field{
field.String("key").
MaxLen(100).
NotEmpty().
Unique().
StructTag(`json:"key"`).
Comment("key binding key"),
field.String("command").
MaxLen(100).
NotEmpty().
StructTag(`json:"command"`).
Comment("key binding command"),
field.String("extension").
MaxLen(100).
Optional().
StructTag(`json:"extension,omitempty"`).
Comment("key binding extension"),
field.Bool("enabled").
Default(true).
StructTag(`json:"enabled"`).
Comment("key binding enabled"),
}
}
// Edges of the KeyBinding.
func (KeyBinding) Edges() []ent.Edge {
return nil
}
// Indexes of the KeyBinding.
func (KeyBinding) Indexes() []ent.Index {
return []ent.Index{
index.Fields("extension"),
index.Fields("enabled"),
}
}

View File

@@ -0,0 +1,114 @@
package mixin
import (
"context"
"fmt"
"entgo.io/ent"
"entgo.io/ent/dialect/sql"
"entgo.io/ent/schema/field"
"entgo.io/ent/schema/index"
"entgo.io/ent/schema/mixin"
)
// SoftDeleteMixin 实现软删除模式
// 使用方法:
// - 正常删除(软删除): client.User.DeleteOneID(id).Exec(ctx)
// - 永久删除: client.User.DeleteOneID(id).Exec(SkipSoftDelete(ctx))
// - 查询包含已删除: client.User.Query().All(SkipSoftDelete(ctx))
//
// 参考https://entgo.io/docs/interceptors/#soft-delete
type SoftDeleteMixin struct {
mixin.Schema
}
// Fields of the SoftDeleteMixin.
func (SoftDeleteMixin) Fields() []ent.Field {
return []ent.Field{
field.String("deleted_at").
Optional().
Nillable().
Comment("deleted at"),
}
}
// Indexes of the SoftDeleteMixin.
func (SoftDeleteMixin) Indexes() []ent.Index {
return []ent.Index{
// 注意:部分索引 WHERE deleted_at IS NOT NULL 已在各表中单独定义
index.Fields("deleted_at"),
}
}
// softDeleteKey 用于在 context 中传递是否跳过软删除的标志
type softDeleteKey struct{}
// SkipSoftDelete 返回一个新的 context跳过软删除拦截器和钩子
func SkipSoftDelete(parent context.Context) context.Context {
return context.WithValue(parent, softDeleteKey{}, true)
}
// Interceptors of the SoftDeleteMixin.
func (d SoftDeleteMixin) Interceptors() []ent.Interceptor {
return []ent.Interceptor{
ent.InterceptFunc(func(next ent.Querier) ent.Querier {
return ent.QuerierFunc(func(ctx context.Context, q ent.Query) (ent.Value, error) {
// 如果 context 中设置了跳过软删除标志,则不过滤
if skip, _ := ctx.Value(softDeleteKey{}).(bool); skip {
return next.Query(ctx, q)
}
// 添加 WHERE deleted_at IS NULL 条件
if w, ok := q.(interface{ WhereP(...func(*sql.Selector)) }); ok {
d.P(w)
}
return next.Query(ctx, q)
})
}),
}
}
// Hooks of the SoftDeleteMixin.
func (d SoftDeleteMixin) Hooks() []ent.Hook {
return []ent.Hook{
func(next ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
// 只处理删除操作
if m.Op() != ent.OpDeleteOne && m.Op() != ent.OpDelete {
return next.Mutate(ctx, m)
}
// 如果 context 中设置了跳过软删除标志,则执行真正的删除
if skip, _ := ctx.Value(softDeleteKey{}).(bool); skip {
return next.Mutate(ctx, m)
}
// 类型断言,确保 mutation 支持软删除所需的方法
mx, ok := m.(interface {
SetOp(ent.Op)
SetDeletedAt(string)
WhereP(...func(*sql.Selector))
})
if !ok {
return nil, fmt.Errorf("SoftDeleteMixin: unexpected mutation type %T", m)
}
// 添加 WHERE deleted_at IS NULL 条件(确保不会重复软删除)
d.P(mx)
// 将删除操作转换为更新操作
mx.SetOp(ent.OpUpdate)
mx.SetDeletedAt(NowString())
// 执行更新操作
return next.Mutate(ctx, m)
})
},
}
}
// P 添加存储层级的过滤条件到查询和变更操作
func (d SoftDeleteMixin) P(w interface{ WhereP(...func(*sql.Selector)) }) {
w.WhereP(
sql.FieldIsNull(d.Fields()[0].Descriptor().Name),
)
}

View File

@@ -0,0 +1,57 @@
package mixin
import (
"context"
"time"
"entgo.io/ent"
"entgo.io/ent/schema/field"
"entgo.io/ent/schema/mixin"
)
// TimeFormat ISO 8601 时间格式
const TimeFormat = time.RFC3339
// NowString 返回当前时间的 ISO 8601 格式字符串
func NowString() string {
return time.Now().Format(TimeFormat)
}
// TimeMixin 时间字段混入
// created_at: 创建时间
// updated_at: 更新时间(自动更新)
type TimeMixin struct {
mixin.Schema
}
// Fields of the TimeMixin.
func (TimeMixin) Fields() []ent.Field {
return []ent.Field{
field.String("created_at").
DefaultFunc(NowString).
Immutable().
StructTag(`json:"created_at"`).
Comment("creation time"),
field.String("updated_at").
DefaultFunc(NowString).
StructTag(`json:"updated_at"`).
Comment("update time"),
}
}
// Hooks of the TimeMixin.
func (TimeMixin) Hooks() []ent.Hook {
return []ent.Hook{
func(next ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
// 只在更新操作时设置 updated_at
if m.Op().Is(ent.OpUpdate | ent.OpUpdateOne) {
if setter, ok := m.(interface{ SetUpdatedAt(string) }); ok {
setter.SetUpdatedAt(NowString())
}
}
return next.Mutate(ctx, m)
})
},
}
}

View File

@@ -0,0 +1,55 @@
package schema
import (
"entgo.io/ent"
"entgo.io/ent/dialect/entsql"
"entgo.io/ent/schema"
"entgo.io/ent/schema/field"
"voidraft/internal/models/schema/mixin"
)
// Theme holds the schema definition for the Theme entity.
type Theme struct {
ent.Schema
}
// Annotations of the Theme.
func (Theme) Annotations() []schema.Annotation {
return []schema.Annotation{
entsql.Annotation{Table: "themes"},
}
}
// Mixin of the Theme.
func (Theme) Mixin() []ent.Mixin {
return []ent.Mixin{
mixin.TimeMixin{},
mixin.SoftDeleteMixin{},
}
}
// Fields of the Theme.
func (Theme) Fields() []ent.Field {
return []ent.Field{
field.String("key").
MaxLen(100).
NotEmpty().
Unique().
StructTag(`json:"key"`).
Comment("theme key"),
field.Enum("type").
Values("dark", "light").
StructTag(`json:"type"`).
Comment("theme type"),
field.JSON("colors", map[string]interface{}{}).
Optional().
StructTag(`json:"colors"`).
Comment("theme colors"),
}
}
// Edges of the Theme.
func (Theme) Edges() []ent.Edge {
return nil
}