middleware_patch.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 .entityAlias }}
  29. values.Set("{{.entityAlias}}", entity)
  30. values.Set("{{.entityAlias}}.patch", patchs)
  31. {{end}}
  32. {{if .preconditions}}
  33. if _, err = executeAction(
  34. ctx,
  35. {{.preconditions}},
  36. ); err != nil {
  37. return
  38. }
  39. {{end}}
  40. filter := values.Get("$filter").(*api.Filter)
  41. filter.DB = "{{.dbName}}"
  42. filter.Collection = "{{.collectionName}}"
  43. filter.Patchs = patchs.Patch()
  44. if _, err = models.Api.PatchOne(filter); err != nil {
  45. err.Details(&errs.Detail{
  46. Dominio: "",
  47. Reason: "",
  48. Location: "middleware.path",
  49. LocationType: "middleware.operation/{{.entity}}",
  50. })
  51. return
  52. }
  53. filter.Entity = &models.{{.entity}}{}
  54. if _, err = models.Api.FindOne(filter); err != nil {
  55. err.Details(&errs.Detail{
  56. Dominio: "",
  57. Reason: "",
  58. Location: "middleware.findOne",
  59. LocationType: "middleware.operation/{{.entity}}",
  60. })
  61. return
  62. }
  63. values.Set("{{.entityAlias}}.after.patch", filter.Entity)
  64. {{if .hasUpdateRelation}} // Cria uma thread que executa a atualizaca das referências.
  65. go func() {
  66. UpdateRelation{{.entity}}(filter)
  67. }() {{end}}
  68. {{if .beforeResponse}}
  69. if resp, err = executeAction(
  70. ctx,
  71. {{.beforeResponse}},
  72. ); err != nil || resp != nil {
  73. return
  74. }
  75. {{end}}
  76. resp = filter.Entity
  77. return`)
  78. if patchStmtsErr != nil {
  79. panic(patchStmtsErr)
  80. }
  81. }
  82. var (
  83. GenPatchStmts = &Middleware{
  84. Id: "implement",
  85. Type: "method",
  86. Fn: func(ctx *MiddlewareContext) error {
  87. var (
  88. project = ctx.Project
  89. method = ctx.Method
  90. dbName = project.GetEntityDB(method.Entity)
  91. collectionName = project.GetCollection(method.Entity)
  92. dependenceMethod = BASE_HAS_DEPENDE + method.Entity
  93. beforeSend = method.Hook("beforeSend")
  94. beforePersist = method.Hook("beforePersist")
  95. context = map[string]interface{}{
  96. "dbName": dbName,
  97. "collectionName": collectionName,
  98. "entity": method.Entity,
  99. "dependenceMethod": dependenceMethod,
  100. "beforePersist": beforePersist,
  101. "beforeSend": beforeSend,
  102. "hasUpdateRelation": SR.Has(method.Entity),
  103. "preconditions": parseMethodActions(method.Preconditions),
  104. "beforeResponse": parseMethodActions(method.BeforeResponse),
  105. "entityAlias": getCustom(method.Custom, "go.entity.alias"),
  106. }
  107. )
  108. if beforePersist {
  109. generateHookCall(project, method, "beforePersist")
  110. }
  111. if beforeSend {
  112. generateHookCall(project, method, "beforeSend")
  113. }
  114. // Nome do metodo que verifica se a entidade tem dependencias
  115. out, _ := TemplateToString(patchStmtsTmpl, context)
  116. afterMethod := ctx.Statement.Block(G.Id(out)).Line()
  117. if body, err := createUpdateRelationMethod(method, context); err == nil {
  118. afterMethod.Id(body)
  119. }
  120. return nil
  121. },
  122. }
  123. )