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:
@@ -127,4 +127,50 @@ defmodule PokemonCouture.Shops do
|
||||
|> Ecto.Changeset.put_assoc(:users, List.delete(clothes.users, user))
|
||||
|> Repo.update!()
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user