-
Notifications
You must be signed in to change notification settings - Fork 12
/
aggregate_with_id.go
60 lines (50 loc) · 1.6 KB
/
aggregate_with_id.go
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package aggregate
import (
"context"
"github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/eventstore"
"strings"
)
type CommonIdAggregate struct {
*eventstore.AggregateBase
when *func(event eventstore.Event) error
}
func (a CommonIdAggregate) NotFound() bool {
return a.GetVersion() < 0
}
func NewCommonAggregateWithId(aggregateType eventstore.AggregateType, id string) *CommonIdAggregate {
if id == "" {
return nil
}
aggregate := &CommonIdAggregate{}
base := eventstore.NewAggregateBase(aggregate.When)
base.SetType(aggregateType)
aggregate.AggregateBase = base
aggregate.SetID(id)
return aggregate
}
func (ca *CommonIdAggregate) setWhen(when func(event eventstore.Event) error) {
ca.when = &when
}
func GetAggregateWithIdObjectID(aggregateID string, aggregateType eventstore.AggregateType) string {
return strings.ReplaceAll(aggregateID, string(aggregateType)+"-", "")
}
func LoadCommonAggregateWithId(ctx context.Context, eventStore eventstore.AggregateStore, aggregateType eventstore.AggregateType, objectID string) (*CommonIdAggregate, error) {
aggregate := NewCommonAggregateWithId(aggregateType, objectID)
err := eventStore.Load(ctx, aggregate)
if err != nil {
return nil, err
}
return aggregate, nil
}
func (a *CommonIdAggregate) When(event eventstore.Event) error {
if a.when != nil {
return (*a.when)(event)
}
return nil
}
func (a *CommonIdAggregate) SetWhen(when func(event eventstore.Event) error) {
a.when = &when
}
func (a *CommonIdAggregate) HandleGRPCRequest(ctx context.Context, request any, params map[string]any) (any, error) {
return nil, nil
}