create.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package actions
  2. import (
  3. "net/http"
  4. "github.com/gin-gonic/gin"
  5. "github.com/go-admin-team/go-admin-core/sdk/api"
  6. "github.com/go-admin-team/go-admin-core/sdk/pkg"
  7. "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth/user"
  8. "github.com/go-admin-team/go-admin-core/sdk/pkg/response"
  9. "git.cestong.com.cn/cecf/hx-admin-server/common/dto"
  10. "git.cestong.com.cn/cecf/hx-admin-server/common/models"
  11. )
  12. // CreateAction 通用新增动作
  13. func CreateAction(control dto.Control) gin.HandlerFunc {
  14. return func(c *gin.Context) {
  15. log := api.GetRequestLogger(c)
  16. db, err := pkg.GetOrm(c)
  17. if err != nil {
  18. log.Error(err)
  19. return
  20. }
  21. //新增操作
  22. req := control.Generate()
  23. err = req.Bind(c)
  24. if err != nil {
  25. response.Error(c, http.StatusUnprocessableEntity, err, err.Error())
  26. return
  27. }
  28. var object models.ActiveRecord
  29. object, err = req.GenerateM()
  30. if err != nil {
  31. response.Error(c, 500, err, "模型生成失败")
  32. return
  33. }
  34. object.SetCreateBy(user.GetUserId(c))
  35. err = db.WithContext(c).Create(object).Error
  36. if err != nil {
  37. log.Errorf("Create error: %s", err)
  38. response.Error(c, 500, err, "创建失败")
  39. return
  40. }
  41. response.OK(c, object.GetId(), "创建成功")
  42. c.Next()
  43. }
  44. }