blob: ee2a966d21fe3af1d88e6f4f7b1dd1e56e53a91b (
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 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
}
|