move helpers to shops.ex, update controller

controller now uses helpers from shops.ex (same as live),
and no longer has edit/delete/create functionality
This commit is contained in:
Lili (Tlapka) 2021-10-07 17:26:45 +02:00
parent 9e1118868a
commit c30f6068db
7 changed files with 84 additions and 132 deletions

View File

@ -127,4 +127,50 @@ defmodule PokemonCouture.Shops do
|> Ecto.Changeset.put_assoc(:users, List.delete(clothes.users, user)) |> Ecto.Changeset.put_assoc(:users, List.delete(clothes.users, user))
|> Repo.update!() |> Repo.update!()
end end
@doc """
Creates a a shop map.
%{location -> %{type -> [clothes_of_loc_and_type]}}
"""
def create_shop_map() do
Enum.reduce(list_clothes_with_owners(), %{}, &sort_clothes_to_map/2)
end
defp sort_clothes_to_map(clothes, map) do
case map[clothes.location] do
nil ->
Map.put(map, clothes.location, %{clothes.type => [clothes]})
map_of_clothes when is_map(map_of_clothes) ->
map_of_clothes = case map_of_clothes[clothes.type] do
nil ->
Map.put(map_of_clothes, clothes.type, [clothes])
list_of_clothes when is_list(list_of_clothes) ->
Map.put(map_of_clothes, clothes.type, [clothes | list_of_clothes])
end
Map.put(map, clothes.location, map_of_clothes)
end
end
defp assign_clothespiece_to_map(clothes, map) do
case map[clothes.name] do
nil ->
Map.put(map, clothes.name, [clothes])
list_of_clothes when is_list(list_of_clothes) ->
Map.put(map, clothes.name, [clothes | list_of_clothes])
end
end
@doc """
Sorts inner maps created with create_shop_map(),
putting each in a different map by clothes name, then sorting by game name.
Resulting big map: %{location -> %{type -> %{name -> sorted_list_of_clothes}}}
"""
def inner_map_creator({type, list}, acc) do
clothes_map = Enum.reduce(list, %{}, &assign_clothespiece_to_map/2)
clothes_map = for {shop, map} <- clothes_map
do {shop, Enum.sort_by(map, fn %Clothes{game: game} -> game end)}
end
Map.put(acc, type, clothes_map)
end
end end

View File

@ -3,71 +3,16 @@ defmodule PokemonCoutureWeb.ClothesController do
alias PokemonCouture.Shops alias PokemonCouture.Shops
def create_shop_map(clothes, map) do # helper function for index
case map[clothes.location] do
nil ->
Map.put(map, clothes.location, [clothes])
list_of_clothes when is_list(list_of_clothes) ->
Map.put(map, clothes.location, list_of_clothes ++ [clothes])
end
end
def index(conn, _params) do def index(conn, _params) do
clothes = Shops.list_clothes() clothes_map = Shops.create_shop_map()
clothes_map = clothes_map = for {shop, map} <- clothes_map
clothes do {shop, Enum.reduce(map, %{}, &Shops.inner_map_creator/2)}
|> Enum.reduce(%{}, &create_shop_map/2) end
render(conn, "index.html", clothes: clothes, clothes_map: clothes_map) render(conn, "index.html", clothes_map: clothes_map)
end end
# def new(conn, _params) do
# changeset = Shops.change_clothes(%Clothes{})
# render(conn, "new.html", changeset: changeset)
# end
# def create(conn, %{"clothes" => clothes_params}) do
# case Shops.create_clothes(clothes_params) do
# {:ok, clothes} ->
# conn
# |> put_flash(:info, "Clothes created successfully.")
# |> redirect(to: Routes.clothes_path(conn, :show, clothes))
# {:error, %Ecto.Changeset{} = changeset} ->
# render(conn, "new.html", changeset: changeset)
# end
# end
def show(conn, %{"id" => id}) do def show(conn, %{"id" => id}) do
clothes = Shops.get_clothes!(id) clothes = Shops.get_clothes!(id)
render(conn, "show.html", clothes: clothes) render(conn, "show.html", clothes: clothes)
end end
def edit(conn, %{"id" => id}) do
clothes = Shops.get_clothes!(id)
changeset = Shops.change_clothes(clothes)
render(conn, "edit.html", clothes: clothes, changeset: changeset)
end
def update(conn, %{"id" => id, "clothes" => clothes_params}) do
clothes = Shops.get_clothes!(id)
case Shops.update_clothes(clothes, clothes_params) do
{:ok, clothes} ->
conn
|> put_flash(:info, "Clothes updated successfully.")
|> redirect(to: Routes.clothes_path(conn, :show, clothes))
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "edit.html", clothes: clothes, changeset: changeset)
end
end
def delete(conn, %{"id" => id}) do
clothes = Shops.get_clothes!(id)
{:ok, _clothes} = Shops.delete_clothes(clothes)
conn
|> put_flash(:info, "Clothes deleted successfully.")
|> redirect(to: Routes.clothes_path(conn, :index))
end
end end

View File

@ -3,45 +3,12 @@ defmodule PokemonCoutureWeb.ClothesTrackerLive do
alias PokemonCouture.Shops alias PokemonCouture.Shops
alias PokemonCouture.Accounts alias PokemonCouture.Accounts
alias PokemonCouture.Shops.Clothes
def create_shop_map(clothes, map) do
case map[clothes.location] do
nil ->
Map.put(map, clothes.location, %{clothes.type => [clothes]})
map_of_clothes when is_map(map_of_clothes) ->
map_of_clothes = case map_of_clothes[clothes.type] do
nil ->
Map.put(map_of_clothes, clothes.type, [clothes])
list_of_clothes when is_list(list_of_clothes) ->
Map.put(map_of_clothes, clothes.type, [clothes | list_of_clothes])
end
Map.put(map, clothes.location, map_of_clothes)
end
end
def assign_clothespiece_to_map(clothes, map) do
case map[clothes.name] do
nil ->
Map.put(map, clothes.name, [clothes])
list_of_clothes when is_list(list_of_clothes) ->
Map.put(map, clothes.name, [clothes | list_of_clothes])
end
end
def inner_map_creator({type, list}, acc) do
clothes_map = Enum.reduce(list, %{}, &assign_clothespiece_to_map/2)
clothes_map = for {shop, map} <- clothes_map
do {shop, Enum.sort_by(map, fn %Clothes{game: game} -> game end)}
end
Map.put(acc, type, clothes_map)
end
def mount(_params, %{"user_token" => user_token} = _session, socket) do def mount(_params, %{"user_token" => user_token} = _session, socket) do
user = Accounts.get_user_by_session_token(user_token) user = Accounts.get_user_by_session_token(user_token)
clothes_map = Enum.reduce(Shops.list_clothes_with_owners(), %{}, &create_shop_map/2) clothes_map = Shops.create_shop_map()
clothes_map = for {shop, map} <- clothes_map clothes_map = for {shop, map} <- clothes_map
do {shop, Enum.reduce(map, %{}, &inner_map_creator/2)} do {shop, Enum.reduce(map, %{}, &Shops.inner_map_creator/2)}
end end
socket = socket =
socket socket

View File

@ -1,5 +0,0 @@
<h1>Edit Clothes</h1>
<%= render "form.html", Map.put(assigns, :action, Routes.clothes_path(@conn, :update, @clothes)) %>
<span><%= link "Back", to: Routes.clothes_path(@conn, :index) %></span>

View File

@ -1,6 +1,8 @@
<h1>Listing Clothes</h1> <h1>Listing Clothes</h1>
<%= for {shop, list_of_clothes} <- @clothes_map do %> <%= for {shop, map_of_clothes_by_type} <- @clothes_map do %>
<h2> <%= shop %></h2> <h2> <%= shop %></h2>
<%= for {type, map_of_clothes_by_name} <- map_of_clothes_by_type do %>
<h3> <%= type %></h3>
<table> <table>
<thead> <thead>
<tr> <tr>
@ -11,6 +13,7 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<%= for {_name, list_of_clothes} <- map_of_clothes_by_name do %>
<%= for clothes <- list_of_clothes do %> <%= for clothes <- list_of_clothes do %>
<tr> <tr>
<td><%= clothes.name %></td> <td><%= clothes.name %></td>
@ -19,11 +22,11 @@
<td> <td>
<span><%= link "Show", to: Routes.clothes_path(@conn, :show, clothes) %></span> <span><%= link "Show", to: Routes.clothes_path(@conn, :show, clothes) %></span>
<span><%= link "Edit", to: Routes.clothes_path(@conn, :edit, clothes) %></span>
<span><%= link "Delete", to: Routes.clothes_path(@conn, :delete, clothes), method: :delete, data: [confirm: "Are you sure?"] %></span>
</td> </td>
</tr> </tr>
<% end %> <% end %>
<% end %>
</tbody> </tbody>
</table> </table>
<% end %> <% end %>
<% end %>

View File

@ -1,5 +0,0 @@
<h1>New Clothes</h1>
<%= render "form.html", Map.put(assigns, :action, Routes.clothes_path(@conn, :create)) %>
<span><%= link "Back", to: Routes.clothes_path(@conn, :index) %></span>

View File

@ -7,12 +7,15 @@
<%= @clothes.name %> <%= @clothes.name %>
</li> </li>
<li> <li>
<strong>Color:</strong> <strong>Color:</strong>
<%= @clothes.color %> <%= @clothes.color %>
</li> </li>
<li>
<strong>Type:</strong>
<%= @clothes.type %>
</li>
<li> <li>
<strong>Location:</strong> <strong>Location:</strong>
@ -25,6 +28,4 @@
</li> </li>
</ul> </ul>
<span><%= link "Edit", to: Routes.clothes_path(@conn, :edit, @clothes) %></span>
<span><%= link "Back", to: Routes.clothes_path(@conn, :index) %></span> <span><%= link "Back", to: Routes.clothes_path(@conn, :index) %></span>