middleware_patch.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package got
  2. import (
  3. "text/template"
  4. . "git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/common"
  5. G "github.com/dave/jennifer/jen"
  6. // "github.com/davecgh/go-spew/spew"
  7. )
  8. var (
  9. patchStmtsTmpl, patchRelationStmtsTmpl *template.Template
  10. patchStmtsErr error
  11. )
  12. func init() {
  13. patchStmtsTmpl, patchStmtsErr = ParseTemplate(`
  14. var (
  15. patchs = &models.{{.entity}}Patchs{Set: &models.{{.entity}}Input{}}
  16. entity = patchs.Set
  17. )
  18. if err = api.ReadJson(ctx, patchs); err != nil {
  19. return
  20. }
  21. entity.SetMode("patch")
  22. values := ctx.Values()
  23. values.Set("patchs", patchs)
  24. entity.LastUpdate = api.NowUnix()
  25. user := values.Get("$user.ref").(*models.UserReference)
  26. entity.UpdatedBy = user
  27. values.Set("entity", entity)
  28. {{if .preconditions}}
  29. if _, err = executeAction(
  30. ctx,
  31. {{.preconditions}},
  32. ); err != nil {
  33. return
  34. }
  35. {{end}}
  36. filter := values.Get("$filter").(*api.Filter)
  37. filter.DB = "{{.dbName}}"
  38. filter.Collection = "{{.collectionName}}"
  39. filter.Patchs = patchs.Patch()
  40. if _, err = models.Api.PatchOne(filter); err != nil {
  41. err.Details(&errs.Detail{
  42. Dominio: "",
  43. Reason: "",
  44. Location: "middleware.path",
  45. LocationType: "middleware.operation/{{.entity}}",
  46. })
  47. return
  48. }
  49. filter.Entity = &models.{{.entity}}{}
  50. if _, err = models.Api.FindOne(filter); err != nil {
  51. err.Details(&errs.Detail{
  52. Dominio: "",
  53. Reason: "",
  54. Location: "middleware.findOne",
  55. LocationType: "middleware.operation/{{.entity}}",
  56. })
  57. return
  58. }
  59. {{if .hasUpdateRelation}} // Cria uma thread que executa a atualizaca das referências.
  60. go func() {
  61. UpdateRelation{{.entity}}(filter)
  62. }() {{end}}
  63. {{if .beforeResponse}}
  64. if resp, err = executeAction(
  65. ctx,
  66. {{.beforeResponse}},
  67. ); err != nil || resp != nil {
  68. return
  69. }
  70. {{end}}
  71. resp = filter.Entity
  72. return`)
  73. if patchStmtsErr != nil {
  74. panic(patchStmtsErr)
  75. }
  76. }
  77. var (
  78. GenPatchStmts = &Middleware{
  79. Id: "implement",
  80. Type: "method",
  81. Fn: func(ctx *MiddlewareContext) error {
  82. var (
  83. project = ctx.Project
  84. method = ctx.Method
  85. dbName = project.GetEntityDB(method.Entity)
  86. collectionName = project.GetCollection(method.Entity)
  87. dependenceMethod = BASE_HAS_DEPENDE + method.Entity
  88. beforeSend = method.Hook("beforeSend")
  89. beforePersist = method.Hook("beforePersist")
  90. context = map[string]interface{}{
  91. "dbName": dbName,
  92. "collectionName": collectionName,
  93. "entity": method.Entity,
  94. "dependenceMethod": dependenceMethod,
  95. "beforePersist": beforePersist,
  96. "beforeSend": beforeSend,
  97. "hasUpdateRelation": SR.Has(method.Entity),
  98. "preconditions": parseMethodActions(method.Preconditions),
  99. "beforeResponse": parseMethodActions(method.BeforeResponse),
  100. }
  101. )
  102. if beforePersist {
  103. generateHookCall(project, method, "beforePersist")
  104. }
  105. if beforeSend {
  106. generateHookCall(project, method, "beforeSend")
  107. }
  108. // Nome do metodo que verifica se a entidade tem dependencias
  109. out, _ := TemplateToString(patchStmtsTmpl, context)
  110. afterMethod := ctx.Statement.Block(G.Id(out)).Line()
  111. if body, err := createUpdateRelationMethod(method, context); err == nil {
  112. afterMethod.Id(body)
  113. }
  114. return nil
  115. },
  116. }
  117. )