middleware_patch.go 3.6 KB

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