Skip to content

Commit

Permalink
replace TODO model with TODO projection
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianAlexander committed May 9, 2022
1 parent 14a2814 commit c188df2
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 11 deletions.
8 changes: 4 additions & 4 deletions lib/todo_backend/todos.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule TodoBackend.Todos do
import Ecto.Query, warn: false
alias TodoBackend.Repo

alias TodoBackend.Todos.Todo
alias TodoBackend.Todos.Projections.Todo

@doc """
Returns the list of todos.
Expand All @@ -28,14 +28,14 @@ defmodule TodoBackend.Todos do
## Examples
iex> get_todo!(123)
iex> get_todo!("51004ff5-5a73-4681-87bb-1b1ffbf03fe0")
%Todo{}
iex> get_todo!(456)
iex> get_todo!("00000000-0000-0000-0000-000000000000")
** (Ecto.NoResultsError)
"""
def get_todo!(id), do: Repo.get!(Todo, id)
def get_todo!(uuid), do: Repo.get_by!(Todo, uuid: uuid)

@doc """
Creates a todo.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
defmodule TodoBackend.Todos.Todo do
defmodule TodoBackend.Todos.Projections.Todo do
use Ecto.Schema
import Ecto.Changeset

@primary_key {:uuid, :binary_id, autogenerate: false}
@derive {Phoenix.Param, key: :uuid}

schema "todos" do
field :completed, :boolean, default: false
field :title, :string
Expand All @@ -10,10 +13,8 @@ defmodule TodoBackend.Todos.Todo do
timestamps()
end

@doc false
def changeset(todo, attrs) do
def update_changeset(todo, attrs \\ %{}) do
todo
|> cast(attrs, [:title, :completed, :order])
|> validate_required([:title, :completed])
end
end
2 changes: 1 addition & 1 deletion lib/todo_backend_web/controllers/todo_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule TodoBackendWeb.TodoController do
use TodoBackendWeb, :controller

alias TodoBackend.Todos
alias TodoBackend.Todos.Todo
alias TodoBackend.Todos.Projections.Todo

action_fallback TodoBackendWeb.FallbackController

Expand Down
4 changes: 2 additions & 2 deletions lib/todo_backend_web/views/todo_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ defmodule TodoBackendWeb.TodoView do

def render("todo.json", %{todo: todo}) do
%{
id: todo.id,
id: todo.uuid,
title: todo.title,
completed: todo.completed,
order: todo.order,
url: Routes.todo_url(Endpoint, :show, todo.id)
url: Routes.todo_url(Endpoint, :show, todo.uuid)
}
end
end
16 changes: 16 additions & 0 deletions priv/repo/migrations/20220508025205_migrate_todo_to_uuid.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
defmodule TodoBackend.Repo.Migrations.MigrateTodoToUuid do
use Ecto.Migration

def change do
drop table(:todos)

create table(:todos, primary_key: false) do
add :uuid, :uuid, primary_key: true
add :title, :string
add :completed, :boolean, default: false, null: false
add :order, :integer, default: 0

timestamps()
end
end
end

0 comments on commit c188df2

Please sign in to comment.