feat: initial commit

This commit is contained in:
2022-10-09 15:23:52 +02:00
commit a1b4d4fc27
39 changed files with 5810 additions and 0 deletions
+50
View File
@@ -0,0 +1,50 @@
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{}
+52
View File
@@ -0,0 +1,52 @@
package domain
import (
"context"
"fmt"
"strings"
"gitlab.com/unboundsoftware/eventsourced/eventsourced"
)
type UpdateSubGraph struct {
Ref string
Service string
Url *string
WSUrl *string
Sdl string
Initiator string
}
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{
Ref: u.Ref,
Service: u.Service,
Url: u.Url,
WSUrl: u.WSUrl,
Sdl: u.Sdl,
Initiator: u.Initiator,
}
}
var _ eventsourced.Command = UpdateSubGraph{}
+14
View File
@@ -0,0 +1,14 @@
package domain
import "gitlab.com/unboundsoftware/eventsourced/eventsourced"
type SubGraphUpdated struct {
eventsourced.EventAggregateId
eventsourced.EventTime
Ref string
Service string
Url *string
WSUrl *string
Sdl string
Initiator string
}