mirror of
https://github.com/go-admin-team/go-admin.git
synced 2026-09-20 17:57:54 +00:00
Merge pull request #226 from matchstalk/table/dev
调整indexAction,允许用户自定义接收的slice,配合gorm只能搜索
This commit is contained in:
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
+12
-11
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,19 +2,20 @@ package actions
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"go-admin/dto"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"go-admin/service/dto"
|
||||
"go-admin/tools"
|
||||
"go-admin/tools/app"
|
||||
"go-admin/tools/model"
|
||||
)
|
||||
|
||||
// IndexAction 通用查询动作
|
||||
func IndexAction(m model.ActiveRecord, d dto.Dtor) 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()
|
||||
req := d.Generate()
|
||||
var err error
|
||||
@@ -23,15 +24,14 @@ func IndexAction(m model.ActiveRecord, d dto.Dtor) gin.HandlerFunc {
|
||||
err = errors.New("db connect not exist")
|
||||
tools.HasError(err, "", 500)
|
||||
}
|
||||
list := object.GenerateList()
|
||||
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 != "" {
|
||||
|
||||
+10
-6
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
+11
-8
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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{}
|
||||
}
|
||||
+2
-7
@@ -33,20 +33,15 @@ func (e *SysJob) Generate() model.ActiveRecord {
|
||||
return &o
|
||||
}
|
||||
|
||||
func (e *SysJob) GenerateList() interface{} {
|
||||
list := make([]SysJob, 0)
|
||||
return &list
|
||||
}
|
||||
|
||||
func (e *SysJob) GetId() interface{} {
|
||||
return e.JobId
|
||||
}
|
||||
|
||||
func (e *SysJob) SetCreateBy(createBy string){
|
||||
func (e *SysJob) SetCreateBy(createBy string) {
|
||||
e.CreateBy = createBy
|
||||
}
|
||||
|
||||
func (e *SysJob) SetUpdateBy(updateBy string){
|
||||
func (e *SysJob) SetUpdateBy(updateBy string) {
|
||||
e.UpdateBy = updateBy
|
||||
}
|
||||
|
||||
|
||||
+9
-6
@@ -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"
|
||||
@@ -112,11 +112,14 @@ func registerSysJobRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddlew
|
||||
r := v1.Group("/sysjob").Use(authMiddleware.MiddlewareFunc()).Use(middleware.AuthCheckRole())
|
||||
{
|
||||
sysJob := &models.SysJob{}
|
||||
r.GET("", actions.IndexAction(sysJob, new(dto.SysJobSearch)))
|
||||
r.GET("/:id", actions.ViewAction(sysJob))
|
||||
r.POST("", actions.CreateAction(sysJob))
|
||||
r.PUT("", actions.UpdateAction(sysJob))
|
||||
r.DELETE("/:id", actions.DeleteAction(sysJob))
|
||||
r.GET("", actions.IndexAction(sysJob, new(dto.SysJobSearch), func() interface{} {
|
||||
list := make([]models.SysJob, 0)
|
||||
return &list
|
||||
}))
|
||||
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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
@@ -8,5 +8,4 @@ type ActiveRecord interface {
|
||||
SetUpdateBy(updateBy string)
|
||||
Generate() ActiveRecord
|
||||
GetId() interface{}
|
||||
GenerateList() interface{}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user