-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
command.go
67 lines (52 loc) · 1.41 KB
/
command.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package ddd
import (
"context"
"time"
"github.com/google/uuid"
)
type (
CommandHandler[T Command] interface {
HandleCommand(ctx context.Context, cmd T) (Reply, error)
}
CommandHandlerFunc[T Command] func(ctx context.Context, cmd T) (Reply, error)
CommandOption interface {
configureCommand(*command)
}
CommandPayload any
Command interface {
IDer
CommandName() string
Payload() CommandPayload
Metadata() Metadata
OccurredAt() time.Time
}
command struct {
Entity
payload CommandPayload
metadata Metadata
occurredAt time.Time
}
)
var _ Command = (*command)(nil)
func NewCommand(name string, payload CommandPayload, options ...CommandOption) Command {
return newCommand(name, payload, options...)
}
func newCommand(name string, payload CommandPayload, options ...CommandOption) command {
evt := command{
Entity: NewEntity(uuid.New().String(), name),
payload: payload,
metadata: make(Metadata),
occurredAt: time.Now(),
}
for _, option := range options {
option.configureCommand(&evt)
}
return evt
}
func (e command) CommandName() string { return e.EntityName() }
func (e command) Payload() CommandPayload { return e.payload }
func (e command) Metadata() Metadata { return e.metadata }
func (e command) OccurredAt() time.Time { return e.occurredAt }
func (f CommandHandlerFunc[T]) HandleCommand(ctx context.Context, cmd T) (Reply, error) {
return f(ctx, cmd)
}