middleware_patch.go 3.6 KB

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