Skip to content

Commit c188df2

Browse files
replace TODO model with TODO projection
1 parent 14a2814 commit c188df2

5 files changed

Lines changed: 28 additions & 11 deletions

File tree

lib/todo_backend/todos.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ defmodule TodoBackend.Todos do
66
import Ecto.Query, warn: false
77
alias TodoBackend.Repo
88

9-
alias TodoBackend.Todos.Todo
9+
alias TodoBackend.Todos.Projections.Todo
1010

1111
@doc """
1212
Returns the list of todos.
@@ -28,14 +28,14 @@ defmodule TodoBackend.Todos do
2828
2929
## Examples
3030
31-
iex> get_todo!(123)
31+
iex> get_todo!("51004ff5-5a73-4681-87bb-1b1ffbf03fe0")
3232
%Todo{}
3333
34-
iex> get_todo!(456)
34+
iex> get_todo!("00000000-0000-0000-0000-000000000000")
3535
** (Ecto.NoResultsError)
3636
3737
"""
38-
def get_todo!(id), do: Repo.get!(Todo, id)
38+
def get_todo!(uuid), do: Repo.get_by!(Todo, uuid: uuid)
3939

4040
@doc """
4141
Creates a todo.
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
defmodule TodoBackend.Todos.Todo do
1+
defmodule TodoBackend.Todos.Projections.Todo do
22
use Ecto.Schema
33
import Ecto.Changeset
44

5+
@primary_key {:uuid, :binary_id, autogenerate: false}
6+
@derive {Phoenix.Param, key: :uuid}
7+
58
schema "todos" do
69
field :completed, :boolean, default: false
710
field :title, :string
@@ -10,10 +13,8 @@ defmodule TodoBackend.Todos.Todo do
1013
timestamps()
1114
end
1215

13-
@doc false
14-
def changeset(todo, attrs) do
16+
def update_changeset(todo, attrs \\ %{}) do
1517
todo
1618
|> cast(attrs, [:title, :completed, :order])
17-
|> validate_required([:title, :completed])
1819
end
1920
end

lib/todo_backend_web/controllers/todo_controller.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ defmodule TodoBackendWeb.TodoController do
22
use TodoBackendWeb, :controller
33

44
alias TodoBackend.Todos
5-
alias TodoBackend.Todos.Todo
5+
alias TodoBackend.Todos.Projections.Todo
66

77
action_fallback TodoBackendWeb.FallbackController
88

lib/todo_backend_web/views/todo_view.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ defmodule TodoBackendWeb.TodoView do
1313

1414
def render("todo.json", %{todo: todo}) do
1515
%{
16-
id: todo.id,
16+
id: todo.uuid,
1717
title: todo.title,
1818
completed: todo.completed,
1919
order: todo.order,
20-
url: Routes.todo_url(Endpoint, :show, todo.id)
20+
url: Routes.todo_url(Endpoint, :show, todo.uuid)
2121
}
2222
end
2323
end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
defmodule TodoBackend.Repo.Migrations.MigrateTodoToUuid do
2+
use Ecto.Migration
3+
4+
def change do
5+
drop table(:todos)
6+
7+
create table(:todos, primary_key: false) do
8+
add :uuid, :uuid, primary_key: true
9+
add :title, :string
10+
add :completed, :boolean, default: false, null: false
11+
add :order, :integer, default: 0
12+
13+
timestamps()
14+
end
15+
end
16+
end

0 commit comments

Comments
 (0)