diff options
| author | mo khan <mo@mokhan.ca> | 2025-07-30 11:03:45 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-07-30 11:03:45 -0600 |
| commit | c1698f896ff343b1b65e57d3961a78d3bb263b7c (patch) | |
| tree | 3eecd594af67f4849ccea1d8ad758004ccfdec9c /app/db/publisher.go | |
| parent | 0626bc0cfffa89b73adc2f9576354e8462270eae (diff) | |
refactor: rename repository types
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 +} |
