Skip to content

Commit

Permalink
feat: Saving person_email and fetching items according to auth info. #37
Browse files Browse the repository at this point in the history
  • Loading branch information
LuchoTurtle committed Dec 9, 2022
1 parent 782cc8a commit d32c90d
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 127 deletions.
5 changes: 3 additions & 2 deletions lib/app/todo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ defmodule App.Todo do
## Examples
iex> list_items()
iex> list_items("test@email.com")
[%Item{}, ...]
"""
def list_items do
def list_items(person_email) do
query =
from(
i in Item,
select: i,
where: i.person_email == ^person_email,
order_by: [asc: i.id]
)

Expand Down
6 changes: 3 additions & 3 deletions lib/app/todo/item.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule App.Todo.Item do
import Ecto.Changeset

schema "items" do
field :person_id, :integer, default: 0
field :person_email, :string, default: ""
field :status, :integer, default: 0
field :text, :string

Expand All @@ -13,7 +13,7 @@ defmodule App.Todo.Item do
@doc false
def changeset(item, attrs) do
item
|> cast(attrs, [:text, :person_id, :status])
|> validate_required([:text])
|> cast(attrs, [:person_email, :status, :text])
|> validate_required([:person_email, :text])
end
end
50 changes: 37 additions & 13 deletions lib/app_web/controllers/item_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,40 @@ defmodule AppWeb.ItemController do
else
%Item{}
end
items = Todo.list_items()
changeset = Todo.change_item(item)

render(conn, "index.html",
items: items,
changeset: changeset,
editing: item,
loggedin: Map.get(conn.assigns, :loggedin),
person: Map.get(conn.assigns, :person),
filter: Map.get(params, "filter", "all")
)

person_email = ""
if Map.has_key?(conn.assigns, :person) do
person_email = Map.get(conn.assigns.person, :email)
end

case Map.has_key?(conn.assigns, :person) do
false ->
items = []
changeset = Todo.change_item(item)

render(conn, "index.html",
items: items,
changeset: changeset,
editing: item,
loggedin: Map.get(conn.assigns, :loggedin),
filter: Map.get(params, "filter", "all")
)

true ->
person_email = Map.get(conn.assigns.person, :email)

items = Todo.list_items(person_email)
changeset = Todo.change_item(item)

render(conn, "index.html",
items: items,
changeset: changeset,
editing: item,
loggedin: Map.get(conn.assigns, :loggedin),
erson: Map.get(conn.assigns, :person),
filter: Map.get(params, "filter", "all")
)
end
end

def new(conn, _params) do
Expand All @@ -31,6 +54,7 @@ defmodule AppWeb.ItemController do
end

def create(conn, %{"item" => item_params}) do
item_params = Map.put(item_params, "person_email", conn.assigns.person.email)
case Todo.create_item(item_params) do
{:ok, _item} ->
conn
Expand Down Expand Up @@ -88,8 +112,8 @@ defmodule AppWeb.ItemController do
end

def clear_completed(conn, _param) do
person_id = 0
query = from(i in Item, where: i.person_id == ^person_id, where: i.status == 1)
person_email = Map.get(conn.assigns.person, :email)
query = from(i in Item, where: i.person_email == ^person_email, where: i.status == 1)
Repo.update_all(query, set: [status: 2])
# render the main template:
index(conn, %{filter: "items"})
Expand Down
212 changes: 107 additions & 105 deletions lib/app_web/controllers/item_html/index.html.heex
Original file line number Diff line number Diff line change
@@ -1,125 +1,127 @@
<div>
<section class="todoapp">
<header class="header">
<h1>todos</h1>
<%= if @editing.id do %>
<.link href={~p"/items"}
<%= if @loggedin do %>
<section class="todoapp">
<header class="header">
<h1>todos</h1>
<%= if @editing.id do %>
<.link href={~p"/items"}
method="get"
class="new-todo">
Click here to create a new item!
</.link>
<% else %>
<%= new(Map.put(assigns, :action, ~p"/items/new")) %>
<% end %>
</header>
<section class="main" style="display: block;">
<input id="toggle-all" class="toggle-all" type="checkbox" />
<label for="toggle-all">Mark all as complete</label>
<ul class="todo-list">
<%= for item <- filter(@items, @filter) do %>
<li data-id={item.id} class={complete(item)}>
<%= if item.status == 1 do %>
<.link href={~p"/items/toggle/#{item.id}"}
Click here to create a new item!
</.link>
<% else %>
<%= new(Map.put(assigns, :action, ~p"/items/new")) %>
<% end %>
</header>
<section class="main" style="display: block;">
<input id="toggle-all" class="toggle-all" type="checkbox" />
<label for="toggle-all">Mark all as complete</label>
<ul class="todo-list">
<%= for item <- filter(@items, @filter) do %>
<li data-id={item.id} class={complete(item)}>
<%= if item.status == 1 do %>
<.link href={~p"/items/toggle/#{item.id}"}
class="toggle checked">
type="checkbox"
</.link>
<% else %>
<.link href={~p"/items/toggle/#{item.id}"}
type="checkbox"
</.link>
<% else %>
<.link href={~p"/items/toggle/#{item.id}"}
type="checkbox"
class="toggle">
</.link>
<% end %>
<div class="view">
<%= if item.id == @editing.id do %>
<%= edit(
</.link>
<% end %>
<div class="view">
<%= if item.id == @editing.id do %>
<%= edit(
Map.put(assigns, :action, ~p"/items/#{item.id}/edit")
|> Map.put(:item, item)
) %>
<% else %>
<.link href={~p"/items/#{item}/edit"} class="dblclick">
<label><%= item.text %></label>
</.link>
<span></span> <!-- used for CSS Double Click -->
<% end %>
<.link
<% else %>
<.link href={~p"/items/#{item}/edit"} class="dblclick">
<label><%= item.text %></label>
</.link>
<span></span> <!-- used for CSS Double Click -->
<% end %>
<.link
class="destroy"
href={~p"/items/#{item}"}
method="delete"
>
</.link>
</div>
</li>
<% end %>
</ul>
</section>
<%= if got_items?(@items) do %>
<footer class="footer" style="display: block;">
<span class="todo-count">
<%= remaining_items(@items) %> <%= pluralise(@items) %> left
</span>
<ul class="filters">
<li>
<%= if @filter == "items" do %>
<a href="/items" class='selected'>
All
</a>
<% else %>
<a href="/items">
All
</a>
<% end %>
</li>
<li>
<%= if @filter == "active" do %>
<a href="/items/active" class='selected'>
Active
[<%= Enum.count(filter(@items, "active")) %>]
</a>
<% else %>
<a href="/items/active">
Active
[<%= Enum.count(filter(@items, "active")) %>]
</a>
<% end %>
</li>
<li>
<%= if @filter == "completed" do %>
<a href="/items/completed" class='selected'>
Completed
[<%= Enum.count(filter(@items, "completed")) %>]
</a>
<% else %>
<a href="/items/completed">
Completed
[<%= Enum.count(filter(@items, "completed")) %>]
</a>
<% end %>
</li>
</.link>
</div>
</li>
<% end %>
</ul>
<a class="clear-completed" href="/items/clear">
Clear completed
[<%= Enum.count(filter(@items, "completed")) %>]
</a>
</footer>
<% end %>
</section>
<div class="fixed right-0 mr-10">
<%= if @loggedin == true do %>
<button
</section>
<%= if got_items?(@items) do %>
<footer class="footer" style="display: block;">
<span class="todo-count">
<%= remaining_items(@items) %> <%= pluralise(@items) %> left
</span>
<ul class="filters">
<li>
<%= if @filter == "items" do %>
<a href="/items" class='selected'>
All
</a>
<% else %>
<a href="/items">
All
</a>
<% end %>
</li>
<li>
<%= if @filter == "active" do %>
<a href="/items/active" class='selected'>
Active
[<%= Enum.count(filter(@items, "active")) %>]
</a>
<% else %>
<a href="/items/active">
Active
[<%= Enum.count(filter(@items, "active")) %>]
</a>
<% end %>
</li>
<li>
<%= if @filter == "completed" do %>
<a href="/items/completed" class='selected'>
Completed
[<%= Enum.count(filter(@items, "completed")) %>]
</a>
<% else %>
<a href="/items/completed">
Completed
[<%= Enum.count(filter(@items, "completed")) %>]
</a>
<% end %>
</li>
</ul>
<a class="clear-completed" href="/items/clear">
Clear completed
[<%= Enum.count(filter(@items, "completed")) %>]
</a>
</footer>
<% end %>
</section>
<div class="fixed right-0 mr-10">
<button
class="inline-block px-6 py-2.5 bg-[#fea5a5] text-white font-medium text-xs leading-tight uppercase rounded shadow-md hover:bg-[#f76e6e] hover:shadow-lg focus:bg-[#f76e6e] focus:shadow-lg focus:outline-none focus:ring-0 active:bg-[#a23434] active:shadow-lg transition duration-150 ease-in-out"
>
<a href="/logout">
Logout
</a>
</button>
<a href="/logout">
Logout
</a>
</button>
</div>
<% else %>
<button
class="inline-block px-6 py-2.5 bg-blue-300 text-white font-medium text-xs leading-tight uppercase rounded shadow-md hover:bg-blue-700 hover:shadow-lg focus:bg-blue-700 focus:shadow-lg focus:outline-none focus:ring-0 active:bg-blue-800 active:shadow-lg transition duration-150 ease-in-out"
<div class="fixed h-screen ml-[25%] flex justify-center items-center">
<button
class="max-h-[35px] inline-block px-6 py-2.5 bg-blue-300 text-white font-medium text-xs leading-tight uppercase rounded shadow-md hover:bg-blue-700 hover:shadow-lg focus:bg-blue-700 focus:shadow-lg focus:outline-none focus:ring-0 active:bg-blue-800 active:shadow-lg transition duration-150 ease-in-out"
>
<a href="/login">
Login
</a>
</button>
<a href="/login">
Login
</a>
</button>
</div>
<% end %>
</div>
</div>
2 changes: 1 addition & 1 deletion priv/repo/migrations/20221205102303_create_items.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule App.Repo.Migrations.CreateItems do
def change do
create table(:items) do
add :text, :string
add :person_id, :integer
add :person_email, :string
add :status, :integer

timestamps()
Expand Down
2 changes: 1 addition & 1 deletion test/app/todo_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defmodule App.TodoTest do
@invalid_attrs %{person_id: nil, status: nil, text: nil}

test "list_items/0 returns all items" do
item = item_fixture()
item = item_fixture("test@email.com")
assert Todo.list_items() == [item]
end

Expand Down
4 changes: 2 additions & 2 deletions test/support/fixtures/todo_fixtures.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ defmodule App.TodoFixtures do
{:ok, item} =
attrs
|> Enum.into(%{
person_id: 42,
status: 42,
person_email: "test@email.com",
status: 0,
text: "some text"
})
|> App.Todo.create_item()
Expand Down

0 comments on commit d32c90d

Please sign in to comment.