Skip to content

Commit

Permalink
update usecase generator for using broker publisher
Browse files Browse the repository at this point in the history
  • Loading branch information
agungdwiprasetyo committed Apr 30, 2024
1 parent 6dd31cf commit ceabce1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 29 deletions.
40 changes: 12 additions & 28 deletions cmd/candi/template_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,8 @@ import (
"{{$.PackagePrefix}}/internal/modules/{{cleanPathModule .ModuleName}}/domain"
{{ if not (or .SQLDeps .MongoDeps .ArangoDeps) }}// {{end}}"{{.PackagePrefix}}/pkg/shared/repository"
"{{$.PackagePrefix}}/pkg/shared/usecase/common"
"{{.LibraryName}}/codebase/factory/dependency"
"{{.LibraryName}}/codebase/factory/types"
"{{.LibraryName}}/codebase/interfaces"
)
// {{upper (camel .ModuleName)}}Usecase abstraction
Expand All @@ -100,36 +99,21 @@ type {{upper (camel .ModuleName)}}Usecase interface {
}
type {{camel .ModuleName}}UsecaseImpl struct {
deps dependency.Dependency
sharedUsecase common.Usecase
cache interfaces.Cache
locker interfaces.Locker
{{if not .SQLDeps}}// {{end}}repoSQL repository.RepoSQL
{{if not .MongoDeps}}// {{end}}repoMongo repository.RepoMongo{{if .ArangoDeps}}
repoArango repository.RepoArango{{end}}
publisher map[types.Worker]interfaces.Publisher
}
// New{{upper (camel .ModuleName)}}Usecase usecase impl constructor
func New{{upper (camel .ModuleName)}}Usecase(deps dependency.Dependency) ({{upper (camel .ModuleName)}}Usecase, func(sharedUsecase common.Usecase)) {
uc := &{{camel .ModuleName}}UsecaseImpl{
deps: deps,
{{if not .SQLDeps}}// {{end}}repoSQL: repository.GetSharedRepoSQL(),
{{if not .MongoDeps}}// {{end}}repoMongo: repository.GetSharedRepoMongo(),
locker: deps.GetLocker(),
publisher: make(map[types.Worker]interfaces.Publisher),{{if .ArangoDeps}}
repoArango: repository.GetSharedRepoArango(),{{end}}
}
if redisPool := deps.GetRedisPool(); redisPool != nil {
uc.cache = redisPool.Cache()
{{if .ArangoDeps}}repoArango: repository.GetSharedRepoArango(),{{end}}
}
if kafkaBroker := deps.GetBroker(types.Kafka); kafkaBroker != nil {
uc.publisher[types.Kafka] = kafkaBroker.GetPublisher()
}{{if .RedisSubsHandler}}
if redisBroker := deps.GetBroker(types.RedisSubscriber); redisBroker != nil {
uc.publisher[types.RedisSubscriber] = redisBroker.GetPublisher()
}{{ end }}{{if .RabbitMQHandler}}
if rabbitmqBroker := deps.GetBroker(types.RabbitMQ); rabbitmqBroker != nil {
uc.publisher[types.RabbitMQ] = rabbitmqBroker.GetPublisher()
}{{ end }}
return uc, func(sharedUsecase common.Usecase) {
uc.sharedUsecase = sharedUsecase
}
Expand Down Expand Up @@ -296,14 +280,14 @@ func (uc *{{camel .ModuleName}}UsecaseImpl) Create{{upper (camel .ModuleName)}}(
err = {{if or .SQLDeps .MongoDeps .ArangoDeps}}uc.repo{{if .SQLDeps}}SQL{{else if .MongoDeps}}Mongo{{else if .ArangoDeps}}Arango{{end}}.{{upper (camel .ModuleName)}}Repo().Save(ctx, &data){{end}}
result.Serialize(&data)
/*
// Sample using publisher
uc.publisher[types.Kafka].PublishMessage(ctx, &candishared.PublisherArgument{
Topic: "[topic]",
Key: "[key]",
Message: candihelper.ToBytes([message]),
})
*/
// Sample using broker publisher
// uc.deps.GetBroker(types.Kafka). // get registered broker type (sample Kafka)
// GetPublisher().
// PublishMessage(ctx, &candishared.PublisherArgument{
// Topic: "[topic]",
// Key: "[key]",
// Message: candihelper.ToBytes("[message]"),
// })
return
}
`
Expand Down
8 changes: 7 additions & 1 deletion codebase/factory/dependency/dependency.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dependency

import (
"log"

"github.com/golangid/candi/codebase/factory/types"
"github.com/golangid/candi/codebase/interfaces"
)
Expand Down Expand Up @@ -137,7 +139,11 @@ func (d *deps) SetMiddleware(mw interfaces.Middleware) {
d.mw = mw
}
func (d *deps) GetBroker(brokerType types.Worker) interfaces.Broker {
return d.brokers[brokerType]
bk := d.brokers[brokerType]
if bk == nil {
log.Panicf(`Broker "%s" is not registered in dependency config`, string(brokerType))
}
return bk
}
func (d *deps) FetchBroker(fn func(types.Worker, interfaces.Broker)) {
for t, bk := range d.brokers {
Expand Down

0 comments on commit ceabce1

Please sign in to comment.