feat: organizations and API keys
This commit is contained in:
+57
-6
@@ -8,19 +8,67 @@ import (
|
||||
"gitlab.com/unboundsoftware/eventsourced/eventsourced"
|
||||
)
|
||||
|
||||
type SubGraph struct {
|
||||
type Organization struct {
|
||||
eventsourced.BaseAggregate
|
||||
Ref string
|
||||
Service string
|
||||
Url *string
|
||||
WSUrl *string
|
||||
Sdl string
|
||||
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 *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
|
||||
}
|
||||
|
||||
func (s *SubGraph) Apply(event eventsourced.Event) error {
|
||||
switch e := event.(type) {
|
||||
case *SubGraphUpdated:
|
||||
@@ -28,6 +76,9 @@ func (s *SubGraph) Apply(event eventsourced.Event) error {
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user