Skip to content

Commit 05b8507

Browse files
implement todo update command
1 parent 488627b commit 05b8507

File tree

7 files changed

+79
-0
lines changed

7 files changed

+79
-0
lines changed

lib/todo_backend/router.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ defmodule TodoBackend.Router do
44
alias TodoBackend.Todos.Aggregates.Todo
55
alias TodoBackend.Todos.Commands.CreateTodo
66
alias TodoBackend.Todos.Commands.DeleteTodo
7+
alias TodoBackend.Todos.Commands.UpdateTodo
78

89
dispatch([CreateTodo], to: Todo, identity: :uuid)
910
dispatch([DeleteTodo], to: Todo, identity: :uuid)
11+
dispatch([UpdateTodo], to: Todo, identity: :uuid)
1012
end

lib/todo_backend/todos/aggregates/todo.ex

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,14 @@ defmodule TodoBackend.Todos.Aggregates.Todo do
1010

1111
alias TodoBackend.Todos.Commands.CreateTodo
1212
alias TodoBackend.Todos.Commands.DeleteTodo
13+
alias TodoBackend.Todos.Commands.UpdateTodo
1314

1415
alias TodoBackend.Todos.Events.TodoCreated
1516
alias TodoBackend.Todos.Events.TodoDeleted
17+
alias TodoBackend.Todos.Events.TodoCompleted
18+
alias TodoBackend.Todos.Events.TodoUncompleted
19+
alias TodoBackend.Todos.Events.TodoTitleUpdated
20+
alias TodoBackend.Todos.Events.TodoOrderUpdated
1621

1722
def execute(%Todo{uuid: nil}, %CreateTodo{} = create) do
1823
%TodoCreated{
@@ -27,6 +32,28 @@ defmodule TodoBackend.Todos.Aggregates.Todo do
2732
%TodoDeleted{uuid: uuid}
2833
end
2934

35+
# TODO: validate
36+
def execute(%Todo{} = todo, %UpdateTodo{} = update) do
37+
completion_command =
38+
if todo.completed != update.completed and not is_nil(update.completed) do
39+
if update.completed do
40+
%TodoCompleted{uuid: todo.uuid}
41+
else
42+
%TodoUncompleted{uuid: todo.uuid}
43+
end
44+
end
45+
46+
title_command =
47+
if todo.title != update.title and not is_nil(update.title),
48+
do: %TodoTitleUpdated{uuid: todo.uuid, title: update.title}
49+
50+
order_command =
51+
if todo.order != update.order and not is_nil(update.order),
52+
do: %TodoOrderUpdated{uuid: todo.uuid, order: update.order}
53+
54+
[completion_command, title_command, order_command] |> Enum.filter(&Function.identity/1)
55+
end
56+
3057
def apply(%Todo{} = todo, %TodoCreated{} = created) do
3158
%Todo{
3259
todo
@@ -37,6 +64,22 @@ defmodule TodoBackend.Todos.Aggregates.Todo do
3764
}
3865
end
3966

67+
def apply(%Todo{} = todo, %TodoCompleted{}) do
68+
%Todo{todo | completed: true}
69+
end
70+
71+
def apply(%Todo{} = todo, %TodoUncompleted{}) do
72+
%Todo{todo | completed: false}
73+
end
74+
75+
def apply(%Todo{} = todo, %TodoTitleUpdated{title: title}) do
76+
%Todo{todo | title: title}
77+
end
78+
79+
def apply(%Todo{} = todo, %TodoOrderUpdated{order: order}) do
80+
%Todo{todo | order: order}
81+
end
82+
4083
def apply(%Todo{uuid: uuid}, %TodoDeleted{uuid: uuid}) do
4184
nil
4285
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
defmodule TodoBackend.Todos.Commands.UpdateTodo do
2+
defstruct [
3+
:uuid,
4+
:title,
5+
:completed,
6+
:order
7+
]
8+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
defmodule TodoBackend.Todos.Events.TodoCompleted do
2+
@derive Jason.Encoder
3+
defstruct [
4+
:uuid
5+
]
6+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
defmodule TodoBackend.Todos.Events.TodoOrderUpdated do
2+
@derive Jason.Encoder
3+
defstruct [
4+
:uuid,
5+
:order
6+
]
7+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
defmodule TodoBackend.Todos.Events.TodoTitleUpdated do
2+
@derive Jason.Encoder
3+
defstruct [
4+
:uuid,
5+
:title
6+
]
7+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
defmodule TodoBackend.Todos.Events.TodoUncompleted do
2+
@derive Jason.Encoder
3+
defstruct [
4+
:uuid
5+
]
6+
end

0 commit comments

Comments
 (0)