12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- package actions
- import (
- "gas-cylinder-api/common/dto"
- "gas-cylinder-api/common/model"
- "net/http"
- "github.com/gin-gonic/gin"
- "gogs.baozhida.cn/zoie/OAuth-core/api"
- "gogs.baozhida.cn/zoie/OAuth-core/pkg"
- "gogs.baozhida.cn/zoie/OAuth-core/pkg/jwtauth/user"
- "gogs.baozhida.cn/zoie/OAuth-core/pkg/response"
- )
- // CreateAction 通用新增动作
- func CreateAction(control dto.Control) gin.HandlerFunc {
- return func(c *gin.Context) {
- log := api.GetRequestLogger(c)
- db, err := pkg.GetOrm(c)
- if err != nil {
- log.Error(err)
- return
- }
- //新增操作
- req := control.Generate()
- err = req.Bind(c)
- if err != nil {
- response.Error(c, http.StatusUnprocessableEntity, err, err.Error())
- return
- }
- var object model.ActiveRecord
- object, err = req.GenerateM()
- if err != nil {
- response.Error(c, 500, err, "模型生成失败")
- return
- }
- object.SetCreateBy(user.GetUserId(c))
- err = db.WithContext(c).Create(object).Error
- if err != nil {
- log.Errorf("Create error: %s", err)
- response.Error(c, 500, err, "创建失败")
- return
- }
- response.OK(c, object.GetId(), "创建成功")
- c.Next()
- }
- }
|