使用action代替api单个操作
改写job
This commit is contained in:
1 parent
78417c2d33
commit
562e61cf4f
15 files changed
+331
-5
No files matched your search
@@ -0,0 +1,39 @@
|
|||||||
|
package actions
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
|
||||||
|
"go-admin/tools"
|
||||||
|
"go-admin/tools/app"
|
||||||
|
"go-admin/tools/model"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CreateAction 通用新增动作
|
||||||
|
func CreateAction(m model.ActiveRecord) gin.HandlerFunc {
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
object := m.Generate()
|
||||||
|
var err error
|
||||||
|
idb, exist := c.Get("db")
|
||||||
|
if !exist {
|
||||||
|
err = errors.New("db connect not exist")
|
||||||
|
tools.HasError(err, "", 500)
|
||||||
|
}
|
||||||
|
switch idb.(type) {
|
||||||
|
case *gorm.DB:
|
||||||
|
//新增操作
|
||||||
|
db := idb.(*gorm.DB)
|
||||||
|
err = c.Bind(object)
|
||||||
|
tools.HasError(err, "参数验证失败", 422)
|
||||||
|
err = db.WithContext(c).Create(object).Error
|
||||||
|
tools.HasError(err, "创建失败", 500)
|
||||||
|
default:
|
||||||
|
err = errors.New("db connect not exist")
|
||||||
|
tools.HasError(err, "", 500)
|
||||||
|
}
|
||||||
|
app.OK(c, object.GetId(), "创建成功")
|
||||||
|
c.Next()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
package actions
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"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 {
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
object := m.Generate()
|
||||||
|
var err error
|
||||||
|
idb, exist := c.Get("db")
|
||||||
|
if !exist {
|
||||||
|
err = errors.New("db connect not exist")
|
||||||
|
tools.HasError(err, "", 500)
|
||||||
|
}
|
||||||
|
switch idb.(type) {
|
||||||
|
case *gorm.DB:
|
||||||
|
//新增操作
|
||||||
|
db := idb.(*gorm.DB)
|
||||||
|
var generalDelDto tools.GeneralDelDto
|
||||||
|
err = c.Bind(&generalDelDto)
|
||||||
|
tools.HasError(err, "参数验证失败", 422)
|
||||||
|
err = c.BindUri(&generalDelDto)
|
||||||
|
tools.HasError(err, "参数验证失败", 422)
|
||||||
|
err = db.WithContext(c).Where(generalDelDto.GetIds()).Delete(object).Error
|
||||||
|
tools.HasError(err, "更新失败", 500)
|
||||||
|
default:
|
||||||
|
err = errors.New("db connect not exist")
|
||||||
|
tools.HasError(err, "", 500)
|
||||||
|
}
|
||||||
|
app.OK(c, object.GetId(), "更新成功")
|
||||||
|
c.Next()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
package actions
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"go-admin/dto"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
|
||||||
|
"go-admin/tools"
|
||||||
|
"go-admin/tools/app"
|
||||||
|
"go-admin/tools/model"
|
||||||
|
)
|
||||||
|
|
||||||
|
// IndexAction 通用查询动作
|
||||||
|
func IndexAction(m model.ActiveRecord, d dto.Dtor) gin.HandlerFunc {
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
object := m.Generate()
|
||||||
|
req := d.Generate()
|
||||||
|
var err error
|
||||||
|
idb, exist := c.Get("db")
|
||||||
|
if !exist {
|
||||||
|
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()
|
||||||
|
tools.HasError(err, "参数验证失败", 422)
|
||||||
|
err = db.WithContext(c).Model(object).
|
||||||
|
Scopes(tools.MakeCondition(req.GetNeedSearch())).
|
||||||
|
Scopes(tools.Paginate(req.GetPageSize(), req.GetPageIndex())).
|
||||||
|
Find(list).Limit(-1).Offset(-1).
|
||||||
|
Count(&count).Error
|
||||||
|
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
tools.HasError(err, "查询失败", 500)
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
err = errors.New("db connect not exist")
|
||||||
|
tools.HasError(err, "", 500)
|
||||||
|
}
|
||||||
|
app.PageOK(c, list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
|
||||||
|
c.Next()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package actions
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
|
||||||
|
"go-admin/tools"
|
||||||
|
"go-admin/tools/app"
|
||||||
|
"go-admin/tools/model"
|
||||||
|
)
|
||||||
|
|
||||||
|
// UpdateAction 通用更新动作
|
||||||
|
func UpdateAction(m model.ActiveRecord) gin.HandlerFunc {
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
object := m.Generate()
|
||||||
|
var err error
|
||||||
|
idb, exist := c.Get("db")
|
||||||
|
if !exist {
|
||||||
|
err = errors.New("db connect not exist")
|
||||||
|
tools.HasError(err, "", 500)
|
||||||
|
}
|
||||||
|
switch idb.(type) {
|
||||||
|
case *gorm.DB:
|
||||||
|
//新增操作
|
||||||
|
db := idb.(*gorm.DB)
|
||||||
|
err = c.Bind(object)
|
||||||
|
tools.HasError(err, "参数验证失败", 422)
|
||||||
|
err = db.WithContext(c).Updates(object).Error
|
||||||
|
tools.HasError(err, "更新失败", 500)
|
||||||
|
default:
|
||||||
|
err = errors.New("db connect not exist")
|
||||||
|
tools.HasError(err, "", 500)
|
||||||
|
}
|
||||||
|
app.OK(c, object.GetId(), "更新成功")
|
||||||
|
c.Next()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package actions
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"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 {
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
object := m.Generate()
|
||||||
|
var err error
|
||||||
|
idb, exist := c.Get("db")
|
||||||
|
if !exist {
|
||||||
|
err = errors.New("db connect not exist")
|
||||||
|
tools.HasError(err, "", 500)
|
||||||
|
}
|
||||||
|
switch idb.(type) {
|
||||||
|
case *gorm.DB:
|
||||||
|
//新增操作
|
||||||
|
db := idb.(*gorm.DB)
|
||||||
|
var generalGetDto tools.GeneralGetDto
|
||||||
|
err = c.BindUri(&generalGetDto)
|
||||||
|
tools.HasError(err, "参数验证失败", 422)
|
||||||
|
err = db.WithContext(c).Where(generalGetDto.Id).First(object).Error
|
||||||
|
tools.HasError(err, "查看失败", 500)
|
||||||
|
default:
|
||||||
|
err = errors.New("db connect not exist")
|
||||||
|
tools.HasError(err, "", 500)
|
||||||
|
}
|
||||||
|
app.OK(c, object, "查看成功")
|
||||||
|
c.Next()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package dto
|
||||||
|
|
||||||
|
type Pagination struct {
|
||||||
|
PageIndex int `form:"pageIndex"`
|
||||||
|
PageSize int `form:"pageSize"`
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package dto
|
package dto
|
||||||
|
|
||||||
type SysJobSearch struct {
|
type SysJobSearch struct {
|
||||||
|
Pagination `search:"-"`
|
||||||
JobId int `form:"jobId" search:"type:exact;column:job_id;table:sys_job"`
|
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"`
|
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"`
|
JobGroup string `form:"jobGroup" search:"type:exact;column:job_group;table:sys_job"`
|
||||||
@@ -8,3 +9,24 @@ type SysJobSearch struct {
|
|||||||
InvokeTarget string `form:"invokeTarget" search:"type:exact;column:invoke_target;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"`
|
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
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package dto
|
||||||
|
|
||||||
|
type Dtor interface {
|
||||||
|
Validate() error
|
||||||
|
Generate() Dtor
|
||||||
|
GetPageIndex() int
|
||||||
|
GetPageSize() int
|
||||||
|
GetNeedSearch() interface{}
|
||||||
|
}
|
||||||
@@ -30,6 +30,7 @@ require (
|
|||||||
github.com/unrolled/secure v1.0.8
|
github.com/unrolled/secure v1.0.8
|
||||||
go.uber.org/multierr v1.5.0 // indirect
|
go.uber.org/multierr v1.5.0 // indirect
|
||||||
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a
|
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a
|
||||||
|
gopkg.in/ffmt.v1 v1.5.6
|
||||||
gorm.io/driver/mysql v0.3.2
|
gorm.io/driver/mysql v0.3.2
|
||||||
gorm.io/driver/postgres v0.2.9
|
gorm.io/driver/postgres v0.2.9
|
||||||
gorm.io/driver/sqlite v1.0.9
|
gorm.io/driver/sqlite v1.0.9
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package middleware
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
func WithContextDb(dbMap map[string]*gorm.DB) gin.HandlerFunc {
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
if db, ok := dbMap["*"]; ok {
|
||||||
|
c.Set("db", db)
|
||||||
|
} else {
|
||||||
|
c.Set("db", dbMap[c.Request.Host])
|
||||||
|
}
|
||||||
|
c.Next()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@ package models
|
|||||||
import (
|
import (
|
||||||
orm "go-admin/global"
|
orm "go-admin/global"
|
||||||
"go-admin/tools"
|
"go-admin/tools"
|
||||||
|
"go-admin/tools/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SysJob struct {
|
type SysJob struct {
|
||||||
@@ -27,6 +28,20 @@ func (SysJob) TableName() string {
|
|||||||
return "sys_job"
|
return "sys_job"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e *SysJob) Generate() model.ActiveRecord {
|
||||||
|
o := *e
|
||||||
|
return &o
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *SysJob) GenerateList() interface{} {
|
||||||
|
list := make([]SysJob, 0)
|
||||||
|
return &list
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *SysJob) GetId() interface{} {
|
||||||
|
return e.JobId
|
||||||
|
}
|
||||||
|
|
||||||
// 创建SysJob
|
// 创建SysJob
|
||||||
func (e *SysJob) Create() (err error) {
|
func (e *SysJob) Create() (err error) {
|
||||||
return orm.Eloquent.Table(e.TableName()).Create(e).Error
|
return orm.Eloquent.Table(e.TableName()).Create(e).Error
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
_ "go-admin/pkg/jwtauth"
|
_ "go-admin/pkg/jwtauth"
|
||||||
"go-admin/tools"
|
"go-admin/tools"
|
||||||
config2 "go-admin/tools/config"
|
config2 "go-admin/tools/config"
|
||||||
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func InitRouter() *gin.Engine {
|
func InitRouter() *gin.Engine {
|
||||||
@@ -20,6 +21,7 @@ func InitRouter() *gin.Engine {
|
|||||||
if config2.SslConfig.Enable {
|
if config2.SslConfig.Enable {
|
||||||
r.Use(handler.TlsHandler())
|
r.Use(handler.TlsHandler())
|
||||||
}
|
}
|
||||||
|
r.Use(middleware.WithContextDb(map[string]*gorm.DB{"*": global.Eloquent}))
|
||||||
middleware.InitMiddleware(r)
|
middleware.InitMiddleware(r)
|
||||||
// the jwt middleware
|
// the jwt middleware
|
||||||
authMiddleware, err := middleware.AuthInit()
|
authMiddleware, err := middleware.AuthInit()
|
||||||
|
|||||||
+15
-5
@@ -1,6 +1,7 @@
|
|||||||
package router
|
package router
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"go-admin/apis/actions"
|
||||||
log2 "go-admin/apis/log"
|
log2 "go-admin/apis/log"
|
||||||
"go-admin/apis/monitor"
|
"go-admin/apis/monitor"
|
||||||
"go-admin/apis/public"
|
"go-admin/apis/public"
|
||||||
@@ -9,8 +10,10 @@ 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"
|
||||||
jwt "go-admin/pkg/jwtauth"
|
jwt "go-admin/pkg/jwtauth"
|
||||||
"go-admin/pkg/ws"
|
"go-admin/pkg/ws"
|
||||||
"mime"
|
"mime"
|
||||||
@@ -110,11 +113,18 @@ func registerSysJobRouter(v1 *gin.RouterGroup) {
|
|||||||
|
|
||||||
r := v1.Group("/sysjob")
|
r := v1.Group("/sysjob")
|
||||||
{
|
{
|
||||||
r.GET("", sysjob.GetSysJobList)
|
//r.GET("", sysjob.GetSysJobList)
|
||||||
r.GET("/:id", sysjob.GetSysJob)
|
//r.GET("/:id", sysjob.GetSysJob)
|
||||||
r.POST("", sysjob.InsertSysJob)
|
//r.POST("", sysjob.InsertSysJob)
|
||||||
r.PUT("", sysjob.UpdateSysJob)
|
//r.PUT("", sysjob.UpdateSysJob)
|
||||||
r.DELETE("/:id", sysjob.DeleteSysJob)
|
//r.DELETE("/:id", sysjob.DeleteSysJob)
|
||||||
|
|
||||||
|
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))
|
||||||
}
|
}
|
||||||
|
|
||||||
v1.GET("/job/remove/:jobId", sysjob.RemoveJob)
|
v1.GET("/job/remove/:jobId", sysjob.RemoveJob)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package tools
|
package tools
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/google/uuid"
|
||||||
"go-admin/tools/config"
|
"go-admin/tools/config"
|
||||||
|
|
||||||
"github.com/matchstalk/go-admin-core/search"
|
"github.com/matchstalk/go-admin-core/search"
|
||||||
@@ -9,7 +10,29 @@ import (
|
|||||||
|
|
||||||
type GeneralDelDto struct {
|
type GeneralDelDto struct {
|
||||||
Id string `uri:"id" json:"id" validate:"required"`
|
Id string `uri:"id" json:"id" validate:"required"`
|
||||||
|
Ids []string `json:"ids"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (g GeneralDelDto) GetIds() []string {
|
||||||
|
ids := make([]string, 0)
|
||||||
|
if len(g.Ids) > 0 {
|
||||||
|
for _, id := range g.Ids {
|
||||||
|
if len(id) > 0 {
|
||||||
|
ids = append(ids, id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if len(g.Id) > 0 {
|
||||||
|
ids = append(ids, g.Id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(ids) <= 0 {
|
||||||
|
//方式全部删除
|
||||||
|
ids = append(ids, uuid.New().String())
|
||||||
|
}
|
||||||
|
return ids
|
||||||
|
}
|
||||||
|
|
||||||
type GeneralGetDto struct {
|
type GeneralGetDto struct {
|
||||||
Id string `uri:"id" json:"id" validate:"required"`
|
Id string `uri:"id" json:"id" validate:"required"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
import "gorm.io/gorm/schema"
|
||||||
|
|
||||||
|
type ActiveRecord interface {
|
||||||
|
schema.Tabler
|
||||||
|
Generate() ActiveRecord
|
||||||
|
GetId() interface{}
|
||||||
|
GenerateList() interface{}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user