规范curd动作流程
This commit is contained in:
1 parent
c93f3f01b3
commit
f0fde7f77c
13 files changed
+173
-86
No files matched your search
@@ -2,6 +2,7 @@ package actions
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"go-admin/service/dto"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
@@ -12,10 +13,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// CreateAction 通用新增动作
|
// CreateAction 通用新增动作
|
||||||
func CreateAction(m model.ActiveRecord) gin.HandlerFunc {
|
func CreateAction(control dto.Control) gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
m.SetCreateBy(tools.GetUserIdStr(c))
|
req := control.Generate()
|
||||||
object := m.Generate()
|
|
||||||
var err error
|
var err error
|
||||||
idb, exist := c.Get("db")
|
idb, exist := c.Get("db")
|
||||||
if !exist {
|
if !exist {
|
||||||
@@ -26,15 +26,19 @@ func CreateAction(m model.ActiveRecord) gin.HandlerFunc {
|
|||||||
case *gorm.DB:
|
case *gorm.DB:
|
||||||
//新增操作
|
//新增操作
|
||||||
db := idb.(*gorm.DB)
|
db := idb.(*gorm.DB)
|
||||||
err = c.Bind(object)
|
err = req.Bind(c)
|
||||||
tools.HasError(err, "参数验证失败", 422)
|
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
|
err = db.WithContext(c).Create(object).Error
|
||||||
tools.HasError(err, "创建失败", 500)
|
tools.HasError(err, "创建失败", 500)
|
||||||
|
app.OK(c, object.GetId(), "创建成功")
|
||||||
default:
|
default:
|
||||||
err = errors.New("db connect not exist")
|
err = errors.New("db connect not exist")
|
||||||
tools.HasError(err, "", 500)
|
tools.HasError(err, "", 500)
|
||||||
}
|
}
|
||||||
app.OK(c, object.GetId(), "创建成功")
|
|
||||||
c.Next()
|
c.Next()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+12
-11
@@ -2,20 +2,19 @@ package actions
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"go-admin/service/dto"
|
||||||
|
"go-admin/tools/app"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
|
||||||
"go-admin/tools"
|
"go-admin/tools"
|
||||||
"go-admin/tools/app"
|
|
||||||
"go-admin/tools/model"
|
"go-admin/tools/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DeleteAction 通用删除动作
|
// DeleteAction 通用删除动作
|
||||||
func DeleteAction(m model.ActiveRecord) gin.HandlerFunc {
|
func DeleteAction(control dto.Control) gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
m.SetUpdateBy(tools.GetUserIdStr(c))
|
|
||||||
object := m.Generate()
|
|
||||||
var err error
|
var err error
|
||||||
idb, exist := c.Get("db")
|
idb, exist := c.Get("db")
|
||||||
if !exist {
|
if !exist {
|
||||||
@@ -24,20 +23,22 @@ func DeleteAction(m model.ActiveRecord) gin.HandlerFunc {
|
|||||||
}
|
}
|
||||||
switch idb.(type) {
|
switch idb.(type) {
|
||||||
case *gorm.DB:
|
case *gorm.DB:
|
||||||
//新增操作
|
//删除操作
|
||||||
db := idb.(*gorm.DB)
|
db := idb.(*gorm.DB)
|
||||||
var generalDelDto tools.GeneralDelDto
|
req := control.Generate()
|
||||||
err = c.Bind(&generalDelDto)
|
err = req.Bind(c)
|
||||||
tools.HasError(err, "参数验证失败", 422)
|
tools.HasError(err, "参数验证失败", 422)
|
||||||
err = c.BindUri(&generalDelDto)
|
var object model.ActiveRecord
|
||||||
tools.HasError(err, "参数验证失败", 422)
|
object, err = req.GenerateM()
|
||||||
err = db.WithContext(c).Where(generalDelDto.GetIds()).Delete(object).Error
|
tools.HasError(err, "模型生成失败", 500)
|
||||||
|
object.SetUpdateBy(tools.GetUserIdStr(c))
|
||||||
|
err = db.WithContext(c).Delete(object).Error
|
||||||
tools.HasError(err, "更新失败", 500)
|
tools.HasError(err, "更新失败", 500)
|
||||||
|
app.OK(c, object.GetId(), "更新成功")
|
||||||
default:
|
default:
|
||||||
err = errors.New("db connect not exist")
|
err = errors.New("db connect not exist")
|
||||||
tools.HasError(err, "", 500)
|
tools.HasError(err, "", 500)
|
||||||
}
|
}
|
||||||
app.OK(c, object.GetId(), "更新成功")
|
|
||||||
c.Next()
|
c.Next()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -6,14 +6,14 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
|
||||||
"go-admin/dto"
|
"go-admin/service/dto"
|
||||||
"go-admin/tools"
|
"go-admin/tools"
|
||||||
"go-admin/tools/app"
|
"go-admin/tools/app"
|
||||||
"go-admin/tools/model"
|
"go-admin/tools/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
// IndexAction 通用查询动作
|
// 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) {
|
return func(c *gin.Context) {
|
||||||
list := f()
|
list := f()
|
||||||
object := m.Generate()
|
object := m.Generate()
|
||||||
@@ -27,11 +27,11 @@ func IndexAction(m model.ActiveRecord, d dto.Dtor, f func() interface{}) gin.Han
|
|||||||
var count int64
|
var count int64
|
||||||
switch idb.(type) {
|
switch idb.(type) {
|
||||||
case *gorm.DB:
|
case *gorm.DB:
|
||||||
//新增操作
|
//查询列表
|
||||||
db := idb.(*gorm.DB)
|
db := idb.(*gorm.DB)
|
||||||
err = c.Bind(req)
|
err = c.Bind(req)
|
||||||
tools.HasError(err, "参数验证失败", 422)
|
tools.HasError(err, "参数验证失败", 422)
|
||||||
err = req.Validate()
|
err = req.Bind(c)
|
||||||
tools.HasError(err, "参数验证失败", 422)
|
tools.HasError(err, "参数验证失败", 422)
|
||||||
var p = new(dataPermission)
|
var p = new(dataPermission)
|
||||||
if userId := tools.GetUserIdStr(c); userId != "" {
|
if userId := tools.GetUserIdStr(c); userId != "" {
|
||||||
|
|||||||
+10
-6
@@ -2,6 +2,7 @@ package actions
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"go-admin/service/dto"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
@@ -12,10 +13,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// UpdateAction 通用更新动作
|
// UpdateAction 通用更新动作
|
||||||
func UpdateAction(m model.ActiveRecord) gin.HandlerFunc {
|
func UpdateAction(control dto.Control) gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
m.SetUpdateBy(tools.GetUserIdStr(c))
|
req := control.Generate()
|
||||||
object := m.Generate()
|
|
||||||
var err error
|
var err error
|
||||||
idb, exist := c.Get("db")
|
idb, exist := c.Get("db")
|
||||||
if !exist {
|
if !exist {
|
||||||
@@ -24,17 +24,21 @@ func UpdateAction(m model.ActiveRecord) gin.HandlerFunc {
|
|||||||
}
|
}
|
||||||
switch idb.(type) {
|
switch idb.(type) {
|
||||||
case *gorm.DB:
|
case *gorm.DB:
|
||||||
//新增操作
|
//更新操作
|
||||||
db := idb.(*gorm.DB)
|
db := idb.(*gorm.DB)
|
||||||
err = c.Bind(object)
|
err = req.Bind(c)
|
||||||
tools.HasError(err, "参数验证失败", 422)
|
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
|
err = db.WithContext(c).Updates(object).Error
|
||||||
tools.HasError(err, "更新失败", 500)
|
tools.HasError(err, "更新失败", 500)
|
||||||
|
app.OK(c, object.GetId(), "更新成功")
|
||||||
default:
|
default:
|
||||||
err = errors.New("db connect not exist")
|
err = errors.New("db connect not exist")
|
||||||
tools.HasError(err, "", 500)
|
tools.HasError(err, "", 500)
|
||||||
}
|
}
|
||||||
app.OK(c, object.GetId(), "更新成功")
|
|
||||||
c.Next()
|
c.Next()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+11
-8
@@ -2,19 +2,19 @@ package actions
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"go-admin/service/dto"
|
||||||
|
"go-admin/tools/app"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
|
||||||
"go-admin/tools"
|
"go-admin/tools"
|
||||||
"go-admin/tools/app"
|
|
||||||
"go-admin/tools/model"
|
"go-admin/tools/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ViewAction 通用详情动作
|
// ViewAction 通用详情动作
|
||||||
func ViewAction(m model.ActiveRecord) gin.HandlerFunc {
|
func ViewAction(control dto.Control) gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
object := m.Generate()
|
|
||||||
var err error
|
var err error
|
||||||
idb, exist := c.Get("db")
|
idb, exist := c.Get("db")
|
||||||
if !exist {
|
if !exist {
|
||||||
@@ -23,18 +23,21 @@ func ViewAction(m model.ActiveRecord) gin.HandlerFunc {
|
|||||||
}
|
}
|
||||||
switch idb.(type) {
|
switch idb.(type) {
|
||||||
case *gorm.DB:
|
case *gorm.DB:
|
||||||
//新增操作
|
//查看详情
|
||||||
db := idb.(*gorm.DB)
|
db := idb.(*gorm.DB)
|
||||||
var generalGetDto tools.GeneralGetDto
|
req := control.Generate()
|
||||||
err = c.BindUri(&generalGetDto)
|
err = req.Bind(c)
|
||||||
tools.HasError(err, "参数验证失败", 422)
|
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)
|
tools.HasError(err, "查看失败", 500)
|
||||||
|
app.OK(c, object, "查看成功")
|
||||||
default:
|
default:
|
||||||
err = errors.New("db connect not exist")
|
err = errors.New("db connect not exist")
|
||||||
tools.HasError(err, "", 500)
|
tools.HasError(err, "", 500)
|
||||||
}
|
}
|
||||||
app.OK(c, object, "查看成功")
|
|
||||||
c.Next()
|
c.Next()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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
|
|
||||||
}
|
|
||||||
@@ -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"`
|
|
||||||
}
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
package dto
|
|
||||||
|
|
||||||
type Dtor interface {
|
|
||||||
Validate() error
|
|
||||||
Generate() Dtor
|
|
||||||
GetPageIndex() int
|
|
||||||
GetPageSize() int
|
|
||||||
GetNeedSearch() interface{}
|
|
||||||
}
|
|
||||||
+5
-5
@@ -10,12 +10,12 @@ import (
|
|||||||
"go-admin/apis/system/dict"
|
"go-admin/apis/system/dict"
|
||||||
. "go-admin/apis/tools"
|
. "go-admin/apis/tools"
|
||||||
_ "go-admin/docs"
|
_ "go-admin/docs"
|
||||||
"go-admin/dto"
|
|
||||||
"go-admin/handler"
|
"go-admin/handler"
|
||||||
"go-admin/middleware"
|
"go-admin/middleware"
|
||||||
"go-admin/models"
|
"go-admin/models"
|
||||||
jwt "go-admin/pkg/jwtauth"
|
jwt "go-admin/pkg/jwtauth"
|
||||||
"go-admin/pkg/ws"
|
"go-admin/pkg/ws"
|
||||||
|
"go-admin/service/dto"
|
||||||
"mime"
|
"mime"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@@ -116,10 +116,10 @@ func registerSysJobRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddlew
|
|||||||
list := make([]models.SysJob, 0)
|
list := make([]models.SysJob, 0)
|
||||||
return &list
|
return &list
|
||||||
}))
|
}))
|
||||||
r.GET("/:id", actions.ViewAction(sysJob))
|
r.GET("/:id", actions.ViewAction(new(dto.SysJobById)))
|
||||||
r.POST("", actions.CreateAction(sysJob))
|
r.POST("", actions.CreateAction(new(dto.SysJobControl)))
|
||||||
r.PUT("", actions.UpdateAction(sysJob))
|
r.PUT("", actions.UpdateAction(new(dto.SysJobControl)))
|
||||||
r.DELETE("/:id", actions.DeleteAction(sysJob))
|
r.DELETE("/:id", actions.DeleteAction(new(dto.SysJobById)))
|
||||||
}
|
}
|
||||||
|
|
||||||
v1.GET("/job/remove/:jobId", sysjob.RemoveJob)
|
v1.GET("/job/remove/:jobId", sysjob.RemoveJob)
|
||||||
|
|||||||
File renamed without changes.
@@ -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
|
||||||
|
}
|
||||||
@@ -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"`
|
||||||
|
}
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
Reference in new issue
Block a user