package domain import ( "fmt" "strings" "time" "gitlab.com/unboundsoftware/eventsourced/eventsourced" ) type Organization struct { eventsourced.BaseAggregate Name string Users []string APIKeys []APIKey CreatedBy string CreatedAt time.Time ChangedBy string ChangedAt time.Time } func (o *Organization) Apply(event eventsourced.Event) error { switch e := event.(type) { case *OrganizationAdded: e.UpdateOrganization(o) case *UserAddedToOrganization: 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() case *APIKeyRemoved: e.UpdateOrganization(o) case *OrganizationRemoved: e.UpdateOrganization(o) 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 } 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() } if s.OrganizationId == "" { s.OrganizationId = e.OrganizationId } 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{}