2022-10-09 15:23:52 +02:00
|
|
|
package domain
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"gitlab.com/unboundsoftware/eventsourced/eventsourced"
|
2023-04-27 07:09:10 +02:00
|
|
|
|
|
|
|
|
"gitlab.com/unboundsoftware/schemas/hash"
|
2022-10-09 15:23:52 +02:00
|
|
|
)
|
|
|
|
|
|
2023-04-27 07:09:10 +02:00
|
|
|
type AddOrganization struct {
|
|
|
|
|
Name string
|
|
|
|
|
Initiator string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a AddOrganization) Validate(_ context.Context, aggregate eventsourced.Aggregate) error {
|
|
|
|
|
if aggregate.Identity() != nil {
|
|
|
|
|
return fmt.Errorf("organization already exists")
|
|
|
|
|
}
|
|
|
|
|
if len(a.Name) == 0 {
|
|
|
|
|
return fmt.Errorf("name is required")
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a AddOrganization) Event(context.Context) eventsourced.Event {
|
|
|
|
|
return &OrganizationAdded{
|
|
|
|
|
Name: a.Name,
|
|
|
|
|
Initiator: a.Initiator,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var _ eventsourced.Command = AddOrganization{}
|
|
|
|
|
|
|
|
|
|
type AddAPIKey struct {
|
|
|
|
|
Name string
|
|
|
|
|
Key string
|
|
|
|
|
Refs []string
|
|
|
|
|
Read bool
|
|
|
|
|
Publish bool
|
2022-10-09 15:23:52 +02:00
|
|
|
Initiator string
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-27 07:09:10 +02:00
|
|
|
func (a AddAPIKey) Validate(_ context.Context, aggregate eventsourced.Aggregate) error {
|
|
|
|
|
if aggregate.Identity() == nil {
|
|
|
|
|
return fmt.Errorf("organization does not exist")
|
|
|
|
|
}
|
|
|
|
|
for _, k := range aggregate.(*Organization).APIKeys {
|
|
|
|
|
if k.Name == a.Name {
|
|
|
|
|
return fmt.Errorf("a key named '%s' already exist", a.Name)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a AddAPIKey) Event(context.Context) eventsourced.Event {
|
|
|
|
|
return &APIKeyAdded{
|
|
|
|
|
Name: a.Name,
|
|
|
|
|
Key: hash.String(a.Key),
|
|
|
|
|
Refs: a.Refs,
|
|
|
|
|
Read: a.Read,
|
|
|
|
|
Publish: a.Publish,
|
|
|
|
|
Initiator: a.Initiator,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var _ eventsourced.Command = AddAPIKey{}
|
|
|
|
|
|
|
|
|
|
type UpdateSubGraph struct {
|
|
|
|
|
OrganizationId string
|
|
|
|
|
Ref string
|
|
|
|
|
Service string
|
|
|
|
|
Url *string
|
|
|
|
|
WSUrl *string
|
|
|
|
|
Sdl string
|
|
|
|
|
Initiator string
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-09 15:23:52 +02:00
|
|
|
func (u UpdateSubGraph) Validate(_ context.Context, aggregate eventsourced.Aggregate) error {
|
|
|
|
|
switch a := aggregate.(type) {
|
|
|
|
|
case *SubGraph:
|
|
|
|
|
if strings.TrimSpace(u.Ref) == "" {
|
|
|
|
|
return fmt.Errorf("ref is missing")
|
|
|
|
|
}
|
|
|
|
|
if strings.TrimSpace(u.Service) == "" {
|
|
|
|
|
return fmt.Errorf("service is missing")
|
|
|
|
|
}
|
|
|
|
|
if strings.TrimSpace(u.Sdl) == "" {
|
|
|
|
|
return fmt.Errorf("SDL is missing")
|
|
|
|
|
}
|
|
|
|
|
if (u.Url == nil || strings.TrimSpace(*u.Url) == "") && a.Url == nil {
|
|
|
|
|
return fmt.Errorf("url is missing")
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
return fmt.Errorf("aggregate is not a SubGraph")
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (u UpdateSubGraph) Event(context.Context) eventsourced.Event {
|
|
|
|
|
return &SubGraphUpdated{
|
2023-04-27 07:09:10 +02:00
|
|
|
OrganizationId: u.OrganizationId,
|
|
|
|
|
Ref: u.Ref,
|
|
|
|
|
Service: u.Service,
|
|
|
|
|
Url: u.Url,
|
|
|
|
|
WSUrl: u.WSUrl,
|
|
|
|
|
Sdl: u.Sdl,
|
|
|
|
|
Initiator: u.Initiator,
|
2022-10-09 15:23:52 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var _ eventsourced.Command = UpdateSubGraph{}
|