refactor🎨: move app/jobs onto the generic actions

The five /sysjob routes use the generic actions. The detail route uses
ViewAs to keep answering SysJobItem, whose entryId the edit form reads
where the model marshals entry_id. GenerateM becomes ToModel, and the
Generate methods go.

Recorded before the change and replayed after it, the same 14 requests
get byte-identical responses; answering the detail with the model
instead of SysJobItem makes exactly the two detail requests differ.

With a real ToModel on both modules, a test now builds a package that
pairs the demo model with the jobs request and requires the build to
fail on that pairing, next to the same package with the pair corrected.
This commit is contained in:
zhangwenjian
2026-09-27 20:46:12 +08:00
parent f7c223e0dc
commit fda94c4965
6 changed files with 67 additions and 41 deletions
-5
View File
@@ -27,11 +27,6 @@ func (*SysJob) TableName() string {
return "sys_job"
}
func (e *SysJob) Generate() models.ActiveRecord {
o := *e
return &o
}
func (e *SysJob) GetId() interface{} {
return e.JobId
}
+7 -11
View File
@@ -19,17 +19,13 @@ func registerSysJobRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddlew
r := v1.Group("/sysjob").Use(authMiddleware.MiddlewareFunc()).Use(middleware.AuthCheckRole())
{
sysJob := &models2.SysJob{}
r.GET("", actions.PermissionAction(), actions.IndexAction(sysJob, new(dto2.SysJobSearch), func() interface{} {
list := make([]models2.SysJob, 0)
return &list
}))
r.GET("/:id", actions.PermissionAction(), actions.ViewAction(new(dto2.SysJobById), func() interface{} {
return &dto2.SysJobItem{}
}))
r.POST("", actions.CreateAction(new(dto2.SysJobControl)))
r.PUT("", actions.PermissionAction(), actions.UpdateAction(new(dto2.SysJobControl)))
r.DELETE("", actions.PermissionAction(), actions.DeleteAction(new(dto2.SysJobById)))
r.GET("", actions.PermissionAction(), actions.Index[models2.SysJob, dto2.SysJobSearch]())
// The detail answers SysJobItem, whose entryId the edit form reads;
// the model itself marshals it as entry_id.
r.GET("/:id", actions.PermissionAction(), actions.ViewAs[models2.SysJob, dto2.SysJobById, dto2.SysJobItem]())
r.POST("", actions.Create[models2.SysJob, dto2.SysJobControl]())
r.PUT("", actions.PermissionAction(), actions.Update[models2.SysJob, dto2.SysJobControl]())
r.DELETE("", actions.PermissionAction(), actions.Delete[models2.SysJob, dto2.SysJobById]())
}
sysJob := apis.SysJob{}
+1 -25
View File
@@ -6,7 +6,6 @@ import (
"go-admin/app/jobs/models"
"go-admin/common/dto"
common "go-admin/common/models"
)
type SysJobSearch struct {
@@ -32,11 +31,6 @@ func (m *SysJobSearch) Bind(ctx *gin.Context) error {
return err
}
func (m *SysJobSearch) Generate() dto.Index {
o := *m
return &o
}
type SysJobControl struct {
JobId int `json:"jobId"`
JobName string `json:"jobName" validate:"required"` // 名称
@@ -55,12 +49,7 @@ func (s *SysJobControl) Bind(ctx *gin.Context) error {
return ctx.ShouldBind(s)
}
func (s *SysJobControl) Generate() dto.Control {
cp := *s
return &cp
}
func (s *SysJobControl) GenerateM() (common.ActiveRecord, error) {
func (s *SysJobControl) ToModel() (*models.SysJob, error) {
return &models.SysJob{
JobId: s.JobId,
JobName: s.JobName,
@@ -76,23 +65,10 @@ func (s *SysJobControl) GenerateM() (common.ActiveRecord, error) {
}, nil
}
func (s *SysJobControl) GetId() interface{} {
return s.JobId
}
type SysJobById struct {
dto.ObjectById
}
func (s *SysJobById) Generate() dto.Control {
cp := *s
return &cp
}
func (s *SysJobById) GenerateM() (common.ActiveRecord, error) {
return &models.SysJob{}, nil
}
type SysJobItem struct {
JobId int `json:"jobId"`
JobName string `json:"jobName" validate:"required"` // 名称