规范curd动作流程

This commit is contained in:
linwenxiang committed 2020-09-01 13:37:49 +08:00
1 parent c93f3f01b3
commit f0fde7f77c
13 files changed
+173 -86

No files matched your search

+9 -5
View File
@@ -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
View File
@@ -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()
}
}
+4 -4
View File
@@ -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 != "" {
+10 -6
View File
@@ -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
View File
@@ -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()
}
}