2021-06-28 16:32:25 +02:00
|
|
|
defmodule PokemonCoutureWeb.ClothesController do
|
|
|
|
use PokemonCoutureWeb, :controller
|
|
|
|
|
|
|
|
alias PokemonCouture.Shops
|
|
|
|
|
2021-09-20 18:00:04 +02:00
|
|
|
def create_shop_map(clothes, map) do # helper function for index
|
2021-07-07 19:08:29 +02:00
|
|
|
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
|
|
|
|
|
2021-06-28 16:32:25 +02:00
|
|
|
def index(conn, _params) do
|
|
|
|
clothes = Shops.list_clothes()
|
2021-07-07 19:08:29 +02:00
|
|
|
clothes_map =
|
|
|
|
clothes
|
|
|
|
|> Enum.reduce(%{}, &create_shop_map/2)
|
|
|
|
render(conn, "index.html", clothes: clothes, clothes_map: clothes_map)
|
2021-06-28 16:32:25 +02:00
|
|
|
end
|
|
|
|
|
2021-09-20 18:00:04 +02:00
|
|
|
# def new(conn, _params) do
|
|
|
|
# changeset = Shops.change_clothes(%Clothes{})
|
|
|
|
# render(conn, "new.html", changeset: changeset)
|
|
|
|
# end
|
2021-06-28 16:32:25 +02:00
|
|
|
|
2021-09-20 18:00:04 +02:00
|
|
|
# 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))
|
2021-06-28 16:32:25 +02:00
|
|
|
|
2021-09-20 18:00:04 +02:00
|
|
|
# {:error, %Ecto.Changeset{} = changeset} ->
|
|
|
|
# render(conn, "new.html", changeset: changeset)
|
|
|
|
# end
|
|
|
|
# end
|
2021-06-28 16:32:25 +02:00
|
|
|
|
|
|
|
def show(conn, %{"id" => id}) do
|
|
|
|
clothes = Shops.get_clothes!(id)
|
|
|
|
render(conn, "show.html", clothes: clothes)
|
|
|
|
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
|