mirror of
https://github.com/go-admin-team/go-admin.git
synced 2026-09-21 02:04:09 +00:00
feat✨: 新增 app/demo 标准 CRUD 参照模块
作为编码约定的可执行参照物:文档会滞后,而这个模块过时会导致构建或测试 失败,因此以它为准。 目录骨架与自动注册文件由项目自带的脚手架生成: go run main.go app -n demo 它同时产出 cmd/api/demo.go,其中的 init() 将路由追加进 AppRouters, 无需在任何中心文件手工登记。 模块本身演示了单表 CRUD 的推荐写法——直接使用 common/actions 提供的五个 通用 Action,因此只有 model、dto、router 三个业务文件,没有 apis 与 service。手写 Handler 的场景仅在业务超出单表 CRUD 时才需要。 DTO 中详情/删除入参内嵌 dto.ObjectById 以复用其 Bind 与 GetId,不重复 实现 uri 绑定与批量 ids 合并逻辑。 补充 8 项测试锁定通用 Action 的接口约束,其中最关键的是 Generate() 必须 返回副本——Action 在并发请求间复用实例,就地返回会串数据。反向验证:将 Generate 改为就地返回,测试立即失败。
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"go-admin/common/models"
|
||||
)
|
||||
|
||||
// DemoProduct 示例模型
|
||||
//
|
||||
// 内嵌 ControlBy 与 ModelTime 后,创建人/更新人与时间戳由框架自动维护;
|
||||
// 数据权限(actions.Permission)正是按 create_by 过滤,缺少 ControlBy 会使其失效。
|
||||
type DemoProduct struct {
|
||||
models.Model
|
||||
|
||||
Name string `json:"name" gorm:"size:128;comment:名称"`
|
||||
Code string `json:"code" gorm:"size:64;comment:编码"`
|
||||
Price float64 `json:"price" gorm:"comment:单价"`
|
||||
Status string `json:"status" gorm:"size:4;comment:状态"`
|
||||
Remark string `json:"remark" gorm:"size:255;comment:备注"`
|
||||
|
||||
models.ControlBy
|
||||
models.ModelTime
|
||||
}
|
||||
|
||||
func (DemoProduct) TableName() string {
|
||||
return "demo_product"
|
||||
}
|
||||
|
||||
// Generate 返回副本,供通用 Action 使用。
|
||||
// 必须返回新实例:Action 在并发请求间复用同一个模型指针,就地返回会串数据。
|
||||
func (e *DemoProduct) Generate() models.ActiveRecord {
|
||||
o := *e
|
||||
return &o
|
||||
}
|
||||
|
||||
func (e *DemoProduct) GetId() interface{} {
|
||||
return e.Id
|
||||
}
|
||||
Reference in New Issue
Block a user