Files
schemas/domain/aggregates.go
T

102 lines
2.0 KiB
Go
Raw Normal View History

2022-10-09 15:23:52 +02:00
package domain
import (
"fmt"
"strings"
"time"
"gitlab.com/unboundsoftware/eventsourced/eventsourced"
)
2023-04-27 07:09:10 +02:00
type Organization struct {
2022-10-09 15:23:52 +02:00
eventsourced.BaseAggregate
2023-04-27 07:09:10 +02:00
Name string
Users []string
APIKeys []APIKey
2022-10-09 15:23:52 +02:00
CreatedBy string
CreatedAt time.Time
ChangedBy string
ChangedAt time.Time
}
2023-04-27 07:09:10 +02:00
func (o *Organization) Apply(event eventsourced.Event) error {
switch e := event.(type) {
case *OrganizationAdded:
e.UpdateOrganization(o)
case *APIKeyAdded:
o.APIKeys = append(o.APIKeys, APIKey{
Name: e.Name,
OrganizationId: o.ID.String(),
Key: e.Key,
Refs: e.Refs,
Read: e.Read,
Publish: e.Publish,
CreatedBy: e.Initiator,
CreatedAt: e.When(),
})
o.ChangedBy = e.Initiator
o.ChangedAt = e.When()
default:
return fmt.Errorf("unexpected event type: %+v", event)
}
return nil
}
var _ eventsourced.Aggregate = &Organization{}
type APIKey struct {
Name string
OrganizationId string
Key string
Refs []string
Read bool
Publish bool
CreatedBy string
CreatedAt time.Time
}
type SubGraph struct {
eventsourced.BaseAggregate
OrganizationId string
Ref string
Service string
Url *string
WSUrl *string
Sdl string
CreatedBy string
CreatedAt time.Time
ChangedBy string
ChangedAt time.Time
}
2022-10-09 15:23:52 +02:00
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()
}
2023-04-27 07:09:10 +02:00
if s.OrganizationId == "" {
s.OrganizationId = e.OrganizationId
}
2022-10-09 15:23:52 +02:00
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{}