-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathlist.go
154 lines (129 loc) · 3.39 KB
/
list.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package todo
import (
"log"
"strings"
"github.com/google/uuid"
"github.com/modernice/goes/aggregate"
"github.com/modernice/goes/command"
"github.com/modernice/goes/command/handler"
"github.com/modernice/goes/event"
)
// ListAggregate is the name of the List aggregate.
const ListAggregate = "todo.list"
// List is a "todo" list.
type List struct {
*aggregate.Base
*handler.BaseHandler
tasks []string
archive []string
}
// New returns the "todo" list with the given id.
func New(id uuid.UUID) *List {
var list *List
list = &List{
Base: aggregate.New(ListAggregate, id),
BaseHandler: handler.NewBase(
handler.BeforeHandle(func(ctx command.Context) error {
log.Printf("Handling %q command ... [list=%s]", ctx.Name(), id)
return nil
}),
handler.AfterHandle(func(command.Context) {
list.print()
}),
),
}
// Register the event appliers for each of the aggregate events.
event.ApplyWith(list, list.add, TaskAdded)
event.ApplyWith(list, list.remove, TaskRemoved)
event.ApplyWith(list, list.done, TasksDone)
// Register the commands handlers.
command.ApplyWith(list, list.Add, AddTaskCmd)
command.ApplyWith(list, list.Remove, RemoveTaskCmd)
command.ApplyWith(list, func(tasks []string) error {
return list.Done(tasks...)
}, DoneTaskCmd)
return list
}
// Tasks returns the active tasks.
func (list *List) Tasks() []string {
return list.tasks
}
// Archive returns the completed tasks.
func (list *List) Archive() []string {
return list.archive
}
// Contains returns whether the list contains the given task (case-insensitive).
func (list *List) Contains(task string) bool {
task = strings.ToLower(task)
for _, t := range list.tasks {
if strings.ToLower(t) == task {
return true
}
}
return false
}
// Add adds the given task to the list, if it doesn't contain the task yet.
func (list *List) Add(task string) error {
for _, t := range list.tasks {
if strings.ToLower(t) == strings.ToLower(task) {
return nil
}
}
aggregate.Next(list, TaskAdded, task)
return nil
}
func (list *List) add(evt event.Of[string]) {
list.tasks = append(list.tasks, evt.Data())
}
// Remove removes the given task from the list.
func (list *List) Remove(task string) error {
if !list.Contains(task) {
return nil
}
aggregate.Next(list, TaskRemoved, TaskRemovedEvent{task})
return nil
}
func (list *List) remove(evt event.Of[TaskRemovedEvent]) {
for i, task := range list.tasks {
if strings.ToLower(task) == strings.ToLower(evt.Data().Task) {
list.tasks = append(list.tasks[:i], list.tasks[i+1:]...)
return
}
}
}
// Done marks the given tasks as done.
func (list *List) Done(tasks ...string) error {
if len(tasks) == 0 {
return nil
}
var done []string
for _, task := range tasks {
ltask := strings.ToLower(task)
for _, t := range list.tasks {
if strings.ToLower(t) == ltask {
done = append(done, task)
break
}
}
}
if len(done) > 0 {
aggregate.Next(list, TasksDone, done)
}
return nil
}
func (list *List) done(evt event.Of[[]string]) {
for _, task := range evt.Data() {
ltask := strings.ToLower(task)
for i, t := range list.tasks {
if strings.ToLower(t) == ltask {
list.tasks = append(list.tasks[:i], list.tasks[i+1:]...)
list.archive = append(list.archive, task)
break
}
}
}
}
func (list *List) print() {
log.Printf("[List:%s] Tasks: %v", list.ID, list.Tasks())
log.Printf("[List:%s] Archive: %v", list.ID, list.Archive())
}