From f0fde7f77c55c3abae1879f78c4005d0fc460f62 Mon Sep 17 00:00:00 2001 From: linwenxiang <991154416@qq.com> Date: Tue, 1 Sep 2020 13:37:49 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=84=E8=8C=83curd=E5=8A=A8=E4=BD=9C?= =?UTF-8?q?=E6=B5=81=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apis/actions/create.go | 14 +++-- apis/actions/delete.go | 23 +++---- apis/actions/index.go | 8 +-- apis/actions/update.go | 16 +++-- apis/actions/view.go | 19 +++--- dto/sysjob.go | 32 ---------- dto/systables.go | 6 -- dto/type.go | 9 --- router/sysrouter.go | 10 ++-- {dto => service/dto}/pagination.go | 0 service/dto/sysjob.go | 96 ++++++++++++++++++++++++++++++ service/dto/systables.go | 6 ++ service/dto/type.go | 20 +++++++ 13 files changed, 173 insertions(+), 86 deletions(-) delete mode 100644 dto/sysjob.go delete mode 100644 dto/systables.go delete mode 100644 dto/type.go rename {dto => service/dto}/pagination.go (100%) create mode 100644 service/dto/sysjob.go create mode 100644 service/dto/systables.go create mode 100644 service/dto/type.go diff --git a/apis/actions/create.go b/apis/actions/create.go index 5ac3250..894b215 100644 --- a/apis/actions/create.go +++ b/apis/actions/create.go @@ -2,6 +2,7 @@ package actions import ( "errors" + "go-admin/service/dto" "github.com/gin-gonic/gin" "gorm.io/gorm" @@ -12,10 +13,9 @@ import ( ) // CreateAction 通用新增动作 -func CreateAction(m model.ActiveRecord) gin.HandlerFunc { +func CreateAction(control dto.Control) gin.HandlerFunc { return func(c *gin.Context) { - m.SetCreateBy(tools.GetUserIdStr(c)) - object := m.Generate() + req := control.Generate() var err error idb, exist := c.Get("db") if !exist { @@ -26,15 +26,19 @@ func CreateAction(m model.ActiveRecord) gin.HandlerFunc { case *gorm.DB: //新增操作 db := idb.(*gorm.DB) - err = c.Bind(object) + err = req.Bind(c) tools.HasError(err, "参数验证失败", 422) + var object model.ActiveRecord + object, err = req.GenerateM() + tools.HasError(err, "模型生成失败", 422) + object.SetCreateBy(tools.GetUserIdStr(c)) err = db.WithContext(c).Create(object).Error tools.HasError(err, "创建失败", 500) + app.OK(c, object.GetId(), "创建成功") default: err = errors.New("db connect not exist") tools.HasError(err, "", 500) } - app.OK(c, object.GetId(), "创建成功") c.Next() } } diff --git a/apis/actions/delete.go b/apis/actions/delete.go index 6d3a6d5..a6f5c74 100644 --- a/apis/actions/delete.go +++ b/apis/actions/delete.go @@ -2,20 +2,19 @@ package actions import ( "errors" + "go-admin/service/dto" + "go-admin/tools/app" "github.com/gin-gonic/gin" "gorm.io/gorm" "go-admin/tools" - "go-admin/tools/app" "go-admin/tools/model" ) // DeleteAction 通用删除动作 -func DeleteAction(m model.ActiveRecord) gin.HandlerFunc { +func DeleteAction(control dto.Control) gin.HandlerFunc { return func(c *gin.Context) { - m.SetUpdateBy(tools.GetUserIdStr(c)) - object := m.Generate() var err error idb, exist := c.Get("db") if !exist { @@ -24,20 +23,22 @@ func DeleteAction(m model.ActiveRecord) gin.HandlerFunc { } switch idb.(type) { case *gorm.DB: - //新增操作 + //删除操作 db := idb.(*gorm.DB) - var generalDelDto tools.GeneralDelDto - err = c.Bind(&generalDelDto) + req := control.Generate() + err = req.Bind(c) tools.HasError(err, "参数验证失败", 422) - err = c.BindUri(&generalDelDto) - tools.HasError(err, "参数验证失败", 422) - err = db.WithContext(c).Where(generalDelDto.GetIds()).Delete(object).Error + var object model.ActiveRecord + object, err = req.GenerateM() + tools.HasError(err, "模型生成失败", 500) + object.SetUpdateBy(tools.GetUserIdStr(c)) + err = db.WithContext(c).Delete(object).Error tools.HasError(err, "更新失败", 500) + app.OK(c, object.GetId(), "更新成功") default: err = errors.New("db connect not exist") tools.HasError(err, "", 500) } - app.OK(c, object.GetId(), "更新成功") c.Next() } } diff --git a/apis/actions/index.go b/apis/actions/index.go index 899fa0e..bdb7b02 100644 --- a/apis/actions/index.go +++ b/apis/actions/index.go @@ -6,14 +6,14 @@ import ( "github.com/gin-gonic/gin" "gorm.io/gorm" - "go-admin/dto" + "go-admin/service/dto" "go-admin/tools" "go-admin/tools/app" "go-admin/tools/model" ) // IndexAction 通用查询动作 -func IndexAction(m model.ActiveRecord, d dto.Dtor, f func() interface{}) gin.HandlerFunc { +func IndexAction(m model.ActiveRecord, d dto.Index, f func() interface{}) gin.HandlerFunc { return func(c *gin.Context) { list := f() object := m.Generate() @@ -27,11 +27,11 @@ func IndexAction(m model.ActiveRecord, d dto.Dtor, f func() interface{}) gin.Han var count int64 switch idb.(type) { case *gorm.DB: - //新增操作 + //查询列表 db := idb.(*gorm.DB) err = c.Bind(req) tools.HasError(err, "参数验证失败", 422) - err = req.Validate() + err = req.Bind(c) tools.HasError(err, "参数验证失败", 422) var p = new(dataPermission) if userId := tools.GetUserIdStr(c); userId != "" { diff --git a/apis/actions/update.go b/apis/actions/update.go index e6816ec..0ed8ba2 100644 --- a/apis/actions/update.go +++ b/apis/actions/update.go @@ -2,6 +2,7 @@ package actions import ( "errors" + "go-admin/service/dto" "github.com/gin-gonic/gin" "gorm.io/gorm" @@ -12,10 +13,9 @@ import ( ) // UpdateAction 通用更新动作 -func UpdateAction(m model.ActiveRecord) gin.HandlerFunc { +func UpdateAction(control dto.Control) gin.HandlerFunc { return func(c *gin.Context) { - m.SetUpdateBy(tools.GetUserIdStr(c)) - object := m.Generate() + req := control.Generate() var err error idb, exist := c.Get("db") if !exist { @@ -24,17 +24,21 @@ func UpdateAction(m model.ActiveRecord) gin.HandlerFunc { } switch idb.(type) { case *gorm.DB: - //新增操作 + //更新操作 db := idb.(*gorm.DB) - err = c.Bind(object) + err = req.Bind(c) tools.HasError(err, "参数验证失败", 422) + var object model.ActiveRecord + object, err = req.GenerateM() + tools.HasError(err, "参数验证失败", 422) + object.SetUpdateBy(tools.GetUserIdStr(c)) err = db.WithContext(c).Updates(object).Error tools.HasError(err, "更新失败", 500) + app.OK(c, object.GetId(), "更新成功") default: err = errors.New("db connect not exist") tools.HasError(err, "", 500) } - app.OK(c, object.GetId(), "更新成功") c.Next() } } diff --git a/apis/actions/view.go b/apis/actions/view.go index 5d98b51..c55f0b5 100644 --- a/apis/actions/view.go +++ b/apis/actions/view.go @@ -2,19 +2,19 @@ package actions import ( "errors" + "go-admin/service/dto" + "go-admin/tools/app" "github.com/gin-gonic/gin" "gorm.io/gorm" "go-admin/tools" - "go-admin/tools/app" "go-admin/tools/model" ) // ViewAction 通用详情动作 -func ViewAction(m model.ActiveRecord) gin.HandlerFunc { +func ViewAction(control dto.Control) gin.HandlerFunc { return func(c *gin.Context) { - object := m.Generate() var err error idb, exist := c.Get("db") if !exist { @@ -23,18 +23,21 @@ func ViewAction(m model.ActiveRecord) gin.HandlerFunc { } switch idb.(type) { case *gorm.DB: - //新增操作 + //查看详情 db := idb.(*gorm.DB) - var generalGetDto tools.GeneralGetDto - err = c.BindUri(&generalGetDto) + req := control.Generate() + err = req.Bind(c) tools.HasError(err, "参数验证失败", 422) - err = db.WithContext(c).Where(generalGetDto.Id).First(object).Error + var object model.ActiveRecord + object, err = req.GenerateM() + tools.HasError(err, "模型生成失败", 500) + err = db.WithContext(c).First(object).Error tools.HasError(err, "查看失败", 500) + app.OK(c, object, "查看成功") default: err = errors.New("db connect not exist") tools.HasError(err, "", 500) } - app.OK(c, object, "查看成功") c.Next() } } diff --git a/dto/sysjob.go b/dto/sysjob.go deleted file mode 100644 index d741475..0000000 --- a/dto/sysjob.go +++ /dev/null @@ -1,32 +0,0 @@ -package dto - -type SysJobSearch struct { - Pagination `search:"-"` - JobId int `form:"jobId" search:"type:exact;column:job_id;table:sys_job"` - JobName string `form:"jobName" search:"type:icontains;column:job_name;table:sys_job"` - JobGroup string `form:"jobGroup" search:"type:exact;column:job_group;table:sys_job"` - CronExpression string `form:"cronExpression" search:"type:exact;column:cron_expression;table:sys_job"` - InvokeTarget string `form:"invokeTarget" search:"type:exact;column:invoke_target;table:sys_job"` - Status int `form:"status" search:"type:exact;column:status;table:sys_job"` -} - -func (m *SysJobSearch) GetNeedSearch() interface{} { - return *m -} - -func (m *SysJobSearch) Validate() error { - return nil -} - -func (m *SysJobSearch) Generate() Dtor { - o := *m - return &o -} - -func (m *SysJobSearch) GetPageIndex() int { - return m.PageIndex -} - -func (m *SysJobSearch) GetPageSize() int { - return m.PageSize -} diff --git a/dto/systables.go b/dto/systables.go deleted file mode 100644 index 4ec51aa..0000000 --- a/dto/systables.go +++ /dev/null @@ -1,6 +0,0 @@ -package dto - -type SysTableSearch struct { - TBName string `form:"tableName" search:"type:exact;column:table_name;table:table_name"` - TableComment string `form:"tableComment" search:"type:icontains;column:table_comment;table:table_comment"` -} \ No newline at end of file diff --git a/dto/type.go b/dto/type.go deleted file mode 100644 index ba6fa63..0000000 --- a/dto/type.go +++ /dev/null @@ -1,9 +0,0 @@ -package dto - -type Dtor interface { - Validate() error - Generate() Dtor - GetPageIndex() int - GetPageSize() int - GetNeedSearch() interface{} -} diff --git a/router/sysrouter.go b/router/sysrouter.go index cf4cf32..ff3b36f 100644 --- a/router/sysrouter.go +++ b/router/sysrouter.go @@ -10,12 +10,12 @@ import ( "go-admin/apis/system/dict" . "go-admin/apis/tools" _ "go-admin/docs" - "go-admin/dto" "go-admin/handler" "go-admin/middleware" "go-admin/models" jwt "go-admin/pkg/jwtauth" "go-admin/pkg/ws" + "go-admin/service/dto" "mime" "github.com/gin-gonic/gin" @@ -116,10 +116,10 @@ func registerSysJobRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddlew list := make([]models.SysJob, 0) return &list })) - r.GET("/:id", actions.ViewAction(sysJob)) - r.POST("", actions.CreateAction(sysJob)) - r.PUT("", actions.UpdateAction(sysJob)) - r.DELETE("/:id", actions.DeleteAction(sysJob)) + r.GET("/:id", actions.ViewAction(new(dto.SysJobById))) + r.POST("", actions.CreateAction(new(dto.SysJobControl))) + r.PUT("", actions.UpdateAction(new(dto.SysJobControl))) + r.DELETE("/:id", actions.DeleteAction(new(dto.SysJobById))) } v1.GET("/job/remove/:jobId", sysjob.RemoveJob) diff --git a/dto/pagination.go b/service/dto/pagination.go similarity index 100% rename from dto/pagination.go rename to service/dto/pagination.go diff --git a/service/dto/sysjob.go b/service/dto/sysjob.go new file mode 100644 index 0000000..4fd62f7 --- /dev/null +++ b/service/dto/sysjob.go @@ -0,0 +1,96 @@ +package dto + +import ( + "github.com/gin-gonic/gin" + "go-admin/models" + "go-admin/tools/model" +) + +type SysJobSearch struct { + Pagination `search:"-"` + JobId int `form:"jobId" search:"type:exact;column:job_id;table:sys_job"` + JobName string `form:"jobName" search:"type:icontains;column:job_name;table:sys_job"` + JobGroup string `form:"jobGroup" search:"type:exact;column:job_group;table:sys_job"` + CronExpression string `form:"cronExpression" search:"type:exact;column:cron_expression;table:sys_job"` + InvokeTarget string `form:"invokeTarget" search:"type:exact;column:invoke_target;table:sys_job"` + Status int `form:"status" search:"type:exact;column:status;table:sys_job"` +} + +func (m *SysJobSearch) GetNeedSearch() interface{} { + return *m +} + +func (m *SysJobSearch) Bind(ctx *gin.Context) error { + return ctx.Bind(m) +} + +func (m *SysJobSearch) Generate() Index { + o := *m + return &o +} + +func (m *SysJobSearch) GetPageIndex() int { + return m.PageIndex +} + +func (m *SysJobSearch) GetPageSize() int { + return m.PageSize +} + +type SysJobControl struct { + JobId int `json:"jobId"` + JobName string `json:"jobName" validate:"required"` // 名称 + JobGroup string `json:"jobGroup"` // 任务分组 + JobType int `json:"jobType"` // 任务类型 + CronExpression string `json:"cronExpression"` // cron表达式 + InvokeTarget string `json:"invokeTarget"` // 调用目标 + Args string `json:"args"` // 目标参数 + MisfirePolicy int `json:"misfirePolicy"` // 执行策略 + Concurrent int `json:"concurrent"` // 是否并发 + Status int `json:"status"` // 状态 + EntryId int `json:"entry_id"` // job启动时返回的id +} + +func (s *SysJobControl) Bind(ctx *gin.Context) error { + return ctx.Bind(s) +} + +func (s *SysJobControl) Generate() Control { + cp := *s + return &cp +} + +func (s *SysJobControl) GenerateM() (model.ActiveRecord, error) { + return &models.SysJob{ + JobId: s.JobId, + JobName: s.JobName, + JobGroup: s.JobGroup, + JobType: s.JobType, + CronExpression: s.CronExpression, + InvokeTarget: s.InvokeTarget, + Args: s.Args, + MisfirePolicy: s.MisfirePolicy, + Concurrent: s.Concurrent, + Status: s.Status, + EntryId: s.EntryId, + }, nil +} + +type SysJobById struct { + Id int `uri:"id" validate:"required"` +} + +func (s *SysJobById) Bind(ctx *gin.Context) error { + return ctx.BindUri(s) +} + +func (s *SysJobById) Generate() Control { + cp := *s + return &cp +} + +func (s *SysJobById) GenerateM() (model.ActiveRecord, error) { + return &models.SysJob{ + JobId: s.Id, + }, nil +} diff --git a/service/dto/systables.go b/service/dto/systables.go new file mode 100644 index 0000000..2e8b923 --- /dev/null +++ b/service/dto/systables.go @@ -0,0 +1,6 @@ +package dto + +type SysTableSearch struct { + TBName string `form:"tableName" search:"type:exact;column:table_name;table:table_name"` + TableComment string `form:"tableComment" search:"type:icontains;column:table_comment;table:table_comment"` +} diff --git a/service/dto/type.go b/service/dto/type.go new file mode 100644 index 0000000..cb73d76 --- /dev/null +++ b/service/dto/type.go @@ -0,0 +1,20 @@ +package dto + +import ( + "github.com/gin-gonic/gin" + "go-admin/tools/model" +) + +type Index interface { + Generate() Index + Bind(ctx *gin.Context) error + GetPageIndex() int + GetPageSize() int + GetNeedSearch() interface{} +} + +type Control interface { + Generate() Control + Bind(ctx *gin.Context) error + GenerateM() (model.ActiveRecord, error) +}