summaryrefslogtreecommitdiff
path: root/app/db/publishing_repository.go
blob: 049129ed9cf812a481c49807f3da6a1a6b9958a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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 publishingRepository[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 &publishingRepository[T]{
			aggregator: aggregator,
			Repository: repository,
		}
	}
}

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
}