diff options
| author | mo khan <mo@mokhan.ca> | 2025-07-30 10:54:20 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-07-30 10:54:20 -0600 |
| commit | 0626bc0cfffa89b73adc2f9576354e8462270eae (patch) | |
| tree | 8a2ae5e982224f8234cdfd9243307a4b5c485049 /app/db | |
| parent | f39fe4ef183164af559768e09ff3388f3617997c (diff) | |
refactor: add ctor option to add repository publishing
Diffstat (limited to 'app/db')
| -rw-r--r-- | app/db/in_memory_repository.go | 6 | ||||
| -rw-r--r-- | app/db/publishing_repository.go | 27 | ||||
| -rw-r--r-- | app/db/publishing_repository_test.go | 3 |
3 files changed, 15 insertions, 21 deletions
diff --git a/app/db/in_memory_repository.go b/app/db/in_memory_repository.go index 2aa1fed..5ee4fea 100644 --- a/app/db/in_memory_repository.go +++ b/app/db/in_memory_repository.go @@ -15,11 +15,11 @@ type inMemoryRepository[T domain.Entity] struct { mu sync.RWMutex } -func NewRepository[T domain.Entity]() domain.Repository[T] { - return &inMemoryRepository[T]{ +func NewRepository[T domain.Entity](options ...x.Option[domain.Repository[T]]) domain.Repository[T] { + return x.NewWith[domain.Repository[T]](&inMemoryRepository[T]{ items: []T{}, mu: sync.RWMutex{}, - } + }, options...) } func (r *inMemoryRepository[T]) All(ctx context.Context) []T { diff --git a/app/db/publishing_repository.go b/app/db/publishing_repository.go index 6be8cf8..049129e 100644 --- a/app/db/publishing_repository.go +++ b/app/db/publishing_repository.go @@ -4,31 +4,26 @@ 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 PublishingRepository[T domain.Entity] struct { +type publishingRepository[T domain.Entity] struct { aggregator *event.TypedAggregator[T] - repository domain.Repository[T] + 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 WithPublishing[T domain.Entity](aggregator *event.TypedAggregator[T]) x.Option[domain.Repository[T]] { + return func(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) +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) } diff --git a/app/db/publishing_repository_test.go b/app/db/publishing_repository_test.go index 7bbc999..4a2c05f 100644 --- a/app/db/publishing_repository_test.go +++ b/app/db/publishing_repository_test.go @@ -11,8 +11,7 @@ import ( func TestPublishingRepository(t *testing.T) { aggregator := event.New[*domain.Sparkle]() - repository := NewRepository[*domain.Sparkle]() - storage := NewPublishingRepository[*domain.Sparkle](aggregator, repository) + storage := NewRepository[*domain.Sparkle](WithPublishing(aggregator)) t.Run("Save", func(t *testing.T) { t.Run("publishes an event", func(t *testing.T) { |
