/
utils.go
48 lines (38 loc) · 1.28 KB
/
utils.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
package aggregate
import (
"context"
"strings"
"github.com/EventStore/EventStore-Client-Go/esdb"
"github.com/ashkan-maleki/go-es-cqrs/internal/order/models"
"github.com/ashkan-maleki/go-es-cqrs/pkg/es"
"github.com/opentracing/opentracing-go/log"
"github.com/pkg/errors"
)
func GetShopItemsTotalPrice(shopItems []*models.ShopItem) float64 {
var totalPrice float64 = 0
for _, item := range shopItems {
totalPrice += item.Price * float64(item.Quantity)
}
return totalPrice
}
// GetOrderAggregateID get order aggregate id for eventstoredb
func GetOrderAggregateID(eventAggregateID string) string {
return strings.ReplaceAll(eventAggregateID, "order-", "")
}
func IsAggregateNotFound(aggregate es.Aggregate) bool {
return aggregate.GetVersion() == 0
}
func LoadOrderAggregate(ctx context.Context, eventStore es.AggregateStore, aggregateID string) (*OrderAggregate, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "LoadOrderAggregate")
defer span.Finish()
span.LogFields(log.String("AggregateID", aggregateID))
order := NewOrderAggregateWithID(aggregateID)
err := eventStore.Exists(ctx, order.GetID())
if err != nil && !errors.Is(err, esdb.ErrStreamNotFound) {
return nil, err
}
if err := eventStore.Load(ctx, order); err != nil {
return nil, err
}
return order, nil
}