规范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

+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()
}
}