create.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package actions
  2. import (
  3. "Medical_OAuth/common/dto"
  4. "Medical_OAuth/common/model"
  5. "net/http"
  6. "github.com/gin-gonic/gin"
  7. "gogs.baozhida.cn/zoie/OAuth-core/api"
  8. "gogs.baozhida.cn/zoie/OAuth-core/pkg"
  9. "gogs.baozhida.cn/zoie/OAuth-core/pkg/jwtauth/user"
  10. "gogs.baozhida.cn/zoie/OAuth-core/pkg/response"
  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 model.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. }