-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathUser.go
65 lines (52 loc) · 1.65 KB
/
User.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
package domain
import (
"errors"
"github.com/pjvds/go-cqrs/sourcing"
"github.com/pjvds/go-cqrs/tests/events"
)
// Holds the state of our user. Note that the
// state, like Username, is not updated directly.
type User struct {
// Make this user an event source.
// This fields needs to be initialized at construction before any state is changed.
sourcing.EventSource
// Holds the username, do not update this fields directly.
// It should only be modified by ctor and ChangeUsername method.
Username string
}
// Creates an new User object.
func NewUser(username string) *User {
user := new(User)
user.EventSource = sourcing.CreateNew(user)
user.Apply(events.UserCreated{
Username: username,
})
return user
}
// Creates an new User object and builds the state from the history.
func NewUserFromHistory(sourceId sourcing.EventSourceId, history []sourcing.Event) *User {
var user = new(User)
user.EventSource = sourcing.CreateFromHistory(user, sourceId, history)
return user
}
// Change the username to a new name.
func (user *User) ChangeUsername(username string) error {
// Validate username
if length := len(username); length < 3 || length > 20 {
return errors.New("invalid username length")
}
// Raise the fact that the username is changed.
user.Apply(events.UsernameChanged{
OldUsername: user.Username,
NewUsername: username,
})
return nil
}
// Update the User state for an UserCreated event.
func (user *User) HandleUserCreated(e events.UserCreated) {
user.Username = e.Username
}
// Update the User state for an UsernameChanged event.
func (user *User) HandleUsernameChanged(e events.UsernameChanged) {
user.Username = e.NewUsername
}