diff options
| author | mo khan <mo@mokhan.ca> | 2025-07-30 10:15:17 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-07-30 10:15:17 -0600 |
| commit | f39fe4ef183164af559768e09ff3388f3617997c (patch) | |
| tree | 2ddb050d2ead1d88d6a59e6b975f688c3d213b6a /app/db/publishing_repository.go | |
| parent | a9a093fcc7a4e0c65f363eb2e2439631062de645 (diff) | |
refactor: extract a separate repository to publish events
Diffstat (limited to 'app/db/publishing_repository.go')
| -rw-r--r-- | app/db/publishing_repository.go | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/app/db/publishing_repository.go b/app/db/publishing_repository.go new file mode 100644 index 0000000..6be8cf8 --- /dev/null +++ b/app/db/publishing_repository.go @@ -0,0 +1,36 @@ +package db + +import ( + "context" + + "github.com/xlgmokha/x/pkg/event" + "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/domain" +) + +type PublishingRepository[T domain.Entity] struct { + aggregator *event.TypedAggregator[T] + repository domain.Repository[T] +} + +func NewPublishingRepository[T domain.Entity](aggregator *event.TypedAggregator[T], repository domain.Repository[T]) domain.Repository[T] { + return &PublishingRepository[T]{ + aggregator: aggregator, + repository: repository, + } +} + +func (r *PublishingRepository[T]) All(ctx context.Context) []T { + return r.repository.All(ctx) +} + +func (r *PublishingRepository[T]) Find(ctx context.Context, id domain.ID) T { + return r.repository.Find(ctx, id) +} + +func (r *PublishingRepository[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 +} |
