diff options
Diffstat (limited to 'app/db/publisher.go')
| -rw-r--r-- | app/db/publisher.go | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/app/db/publisher.go b/app/db/publisher.go new file mode 100644 index 0000000..ee2a966 --- /dev/null +++ b/app/db/publisher.go @@ -0,0 +1,31 @@ +package db + +import ( + "context" + + "github.com/xlgmokha/x/pkg/event" + "github.com/xlgmokha/x/pkg/x" + "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/domain" +) + +type publisher[T domain.Entity] struct { + aggregator *event.TypedAggregator[T] + domain.Repository[T] +} + +func WithPublishing[T domain.Entity](aggregator *event.TypedAggregator[T]) x.Option[domain.Repository[T]] { + return func(repository domain.Repository[T]) domain.Repository[T] { + return &publisher[T]{ + aggregator: aggregator, + Repository: repository, + } + } +} + +func (r *publisher[T]) Save(ctx context.Context, item T) error { + err := r.Repository.Save(ctx, item) + if err == nil { + r.aggregator.Publish("after.create", item) + } + return err +} |
