Files
schemas/domain/aggregates.go
T
2022-10-09 20:37:48 +02:00

51 lines
919 B
Go

package domain
import (
"fmt"
"strings"
"time"
"gitlab.com/unboundsoftware/eventsourced/eventsourced"
)
type SubGraph struct {
eventsourced.BaseAggregate
Ref string
Service string
Url *string
WSUrl *string
Sdl string
CreatedBy string
CreatedAt time.Time
ChangedBy string
ChangedAt time.Time
}
func (s *SubGraph) Apply(event eventsourced.Event) error {
switch e := event.(type) {
case *SubGraphUpdated:
if s.Ref == "" {
s.CreatedBy = e.Initiator
s.CreatedAt = e.When()
}
s.ChangedBy = e.Initiator
s.ChangedAt = e.When()
s.Ref = e.Ref
s.Service = e.Service
if e.Url != nil {
url := strings.TrimSpace(*e.Url)
s.Url = &url
}
if e.WSUrl != nil {
url := strings.TrimSpace(*e.WSUrl)
s.WSUrl = &url
}
s.Sdl = e.Sdl
default:
return fmt.Errorf("unexpected event type: %+v", event)
}
return nil
}
var _ eventsourced.Aggregate = &SubGraph{}