123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- package got
- import (
- "text/template"
- . "git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/common"
- G "github.com/dave/jennifer/jen"
- )
- var (
- getStmtsTmpl *template.Template
- getStmtsErr error
- )
- func init() {
- getStmtsTmpl, getStmtsErr = ParseTemplate(`
- var (
- entity = &models.{{.entity}}{}
- values = ctx.Values()
- )
- values.Set("entity", entity)
- options := values.Get("$filter").(*api.Filter)
- options.DB = "{{.dbName}}"
- options.Collection = "{{.collectionName}}"
- options.Entity = entity
- if err = t.Fields(options); err != nil {
- return
- }
-
- {{if .preconditions}}
- if _, err = executeAction(
- ctx,
- {{.preconditions}},
- ); err != nil {
- return
- }
- {{end}}
- if _, err = models.Api.FindOne(options); err != nil {
- return
- }
-
- {{if .beforeResponse}}
- if resp, err = executeAction(
- ctx,
- {{.beforeResponse}},
- ); err != nil || resp != nil {
- return
- }
- {{end}}
- resp = entity
- return`)
- if getStmtsErr != nil {
- panic(getStmtsErr)
- }
- }
- var (
- GenGetStmtsOne = &Middleware{
- Id: "get_one",
- Type: "method",
- Fn: func(ctx *MiddlewareContext) error {
- var (
- project = ctx.Project
- method = ctx.Method
- dbName = project.GetEntityDB(method.Entity)
- collectionName = project.GetCollection(method.Entity)
- context = map[string]interface{}{
- "dbName": dbName,
- "collectionName": collectionName,
- "entity": method.Entity,
- "beforeFind": method.Hook("beforeFind"),
- "beforeSend": method.Hook("beforeSend"),
- "preconditions": parseMethodActions(method.Preconditions),
- "beforeResponse": parseMethodActions(method.BeforeResponse),
- }
- )
- // Nome do metodo que verifica se a entidade tem dependencias
- out, _ := TemplateToString(getStmtsTmpl, context)
- ctx.Statement.Block(G.Id(out)).Line()
- return nil
- },
- }
- )
|