Skip to content

Commit

Permalink
generator: improve with trx & construct usecase
Browse files Browse the repository at this point in the history
  • Loading branch information
agungdwiprasetyo committed Oct 13, 2022
1 parent 8f6bbff commit fa3b055
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
21 changes: 16 additions & 5 deletions cmd/candi/template_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,21 @@ func (r *repoSQLImpl) WithTransaction(ctx context.Context, txFunc func(ctx conte
trace, ctx := tracer.StartTraceWithContext(ctx, "RepoSQL:Transaction")
defer trace.Finish()
tx{{if not .SQLUseGORM}}, err{{end}} := r.writeDB.Begin()` + "{{if .SQLUseGORM}}\n err = tx.Error{{end}}" + `
if err != nil {
return err
}
{{if .SQLUseGORM}}tx, ok := candishared.GetValueFromContext(ctx, candishared.ContextKeySQLTransaction).(*gorm.DB)
if !ok {
tx = r.writeDB.Begin()
if tx.Error != nil {
return tx.Error
}
ctx = candishared.SetToContext(ctx, candishared.ContextKeySQLTransaction, tx)
}{{else}}tx, ok := candishared.GetValueFromContext(ctx, candishared.ContextKeySQLTransaction).(*sql.Tx)
if !ok {
tx, err = r.writeDB.Begin()
if err != nil {
return err
}
ctx = candishared.SetToContext(ctx, candishared.ContextKeySQLTransaction, tx)
}{{end}}
defer func() {
if err != nil {
Expand All @@ -136,7 +147,7 @@ func (r *repoSQLImpl) WithTransaction(ctx context.Context, txFunc func(ctx conte
if err := txFunc(ctx); err != nil {
errChan <- err
}
}(candishared.SetToContext(ctx, candishared.ContextKeySQLTransaction, tx))
}(ctx)
select {
case <-ctx.Done():
Expand Down
12 changes: 7 additions & 5 deletions cmd/candi/template_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ import (
{{ if not (or .SQLDeps .MongoDeps .ArangoDeps) }}// {{end}}"{{.PackagePrefix}}/pkg/shared/repository"
"{{$.PackagePrefix}}/pkg/shared/usecase/common"
"{{.LibraryName}}/candishared"
"{{.LibraryName}}/codebase/factory/dependency"{{if or .KafkaHandler .RabbitMQHandler}}
"{{.LibraryName}}/codebase/factory/types"{{end}}
"{{.LibraryName}}/codebase/factory/dependency"
"{{.LibraryName}}/codebase/factory/types"
"{{.LibraryName}}/codebase/interfaces"
)
Expand Down Expand Up @@ -115,15 +115,17 @@ func New{{upper (camel .ModuleName)}}Usecase(deps dependency.Dependency) ({{uppe
uc := &{{camel .ModuleName}}UsecaseImpl{
{{if not .SQLDeps}}// {{end}}repoSQL: repository.GetSharedRepoSQL(),
{{if not .MongoDeps}}// {{end}}repoMongo: repository.GetSharedRepoMongo(),{{if .ArangoDeps}}
repoArango: repository.GetSharedRepoArango(),{{end}}{{if .RabbitMQHandler}}
rabbitmqPub: deps.GetBroker(types.RabbitMQ).GetPublisher(),{{ end }}
repoArango: repository.GetSharedRepoArango(),{{end}}
}
if redisPool := deps.GetRedisPool(); redisPool != nil {
uc.cache = redisPool.Cache()
}
if kafkaBroker := deps.GetBroker(types.Kafka); kafkaBroker != nil {
uc.kafkaPub = kafkaBroker.GetPublisher()
}
}{{if .RabbitMQHandler}}
if rabbitmqBroker := deps.GetBroker(types.RabbitMQ); rabbitmqBroker != nil {
uc.rabbitmqPub = rabbitmqBroker.GetPublisher()
}{{ end }}
return uc, func(sharedUsecase common.Usecase) {
uc.sharedUsecase = sharedUsecase
}
Expand Down
2 changes: 1 addition & 1 deletion init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ package candi

const (
// Version of this library
Version = "v1.12.4"
Version = "v1.12.5"
)

0 comments on commit fa3b055

Please sign in to comment.