Skip to content

Commit dc6e9fb

Browse files
implement todo aggregate with create command
1 parent ab81186 commit dc6e9fb

File tree

5 files changed

+59
-0
lines changed

5 files changed

+59
-0
lines changed

lib/todo_backend/app.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
defmodule TodoBackend.App do
22
use Commanded.Application, otp_app: :todo_backend
3+
4+
router(TodoBackend.Router)
35
end

lib/todo_backend/router.ex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
defmodule TodoBackend.Router do
2+
use Commanded.Commands.Router
3+
4+
alias TodoBackend.Todos.Aggregates.Todo
5+
alias TodoBackend.Todos.Commands.CreateTodo
6+
7+
dispatch([CreateTodo], to: Todo, identity: :uuid)
8+
end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
defmodule TodoBackend.Todos.Aggregates.Todo do
2+
defstruct [
3+
:uuid,
4+
:title,
5+
:completed,
6+
:order
7+
]
8+
9+
alias TodoBackend.Todos.Aggregates.Todo
10+
11+
alias TodoBackend.Todos.Commands.CreateTodo
12+
13+
alias TodoBackend.Todos.Events.TodoCreated
14+
15+
def execute(%Todo{uuid: nil}, %CreateTodo{} = create) do
16+
%TodoCreated{
17+
uuid: create.uuid,
18+
title: create.title,
19+
completed: create.completed,
20+
order: create.order
21+
}
22+
end
23+
24+
def apply(%Todo{} = todo, %TodoCreated{} = created) do
25+
%Todo{
26+
todo
27+
| uuid: created.uuid,
28+
title: created.title,
29+
completed: created.completed,
30+
order: created.order
31+
}
32+
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
defmodule TodoBackend.Todos.Commands.CreateTodo do
2+
defstruct [
3+
:uuid,
4+
:title,
5+
:completed,
6+
:order
7+
]
8+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
defmodule TodoBackend.Todos.Events.TodoCreated do
2+
@derive Jason.Encoder
3+
defstruct [
4+
:uuid,
5+
:title,
6+
:completed,
7+
:order
8+
]
9+
end

0 commit comments

Comments
 (0)