cannery/lib/cannery_web/controllers/export_controller.ex

76 lines
2.5 KiB
Elixir
Raw Normal View History

2022-11-09 23:33:50 -05:00
defmodule CanneryWeb.ExportController do
use CanneryWeb, :controller
alias Cannery.{ActivityLog, Ammo, Containers}
def export(%{assigns: %{current_user: current_user}} = conn, %{"mode" => "json"}) do
2023-06-03 20:14:20 -04:00
types = Ammo.list_types(current_user)
2023-03-30 21:53:52 -04:00
used_counts = types |> ActivityLog.get_used_count_for_types(current_user)
round_counts = types |> Ammo.get_round_count_for_types(current_user)
pack_counts = types |> Ammo.get_packs_count_for_types(current_user)
2023-03-30 21:53:52 -04:00
total_pack_counts = types |> Ammo.get_packs_count_for_types(current_user, true)
2023-03-30 21:53:52 -04:00
average_costs = types |> Ammo.get_average_cost_for_types(current_user)
2022-11-09 23:33:50 -05:00
2023-03-30 21:53:52 -04:00
types =
types
|> Enum.map(fn %{id: type_id} = type ->
type
2022-11-09 23:33:50 -05:00
|> Jason.encode!()
|> Jason.decode!()
|> Map.merge(%{
2023-03-30 21:53:52 -04:00
"average_cost" => Map.get(average_costs, type_id),
"round_count" => Map.get(round_counts, type_id, 0),
"used_count" => Map.get(used_counts, type_id, 0),
"pack_count" => Map.get(pack_counts, type_id, 0),
"total_pack_count" => Map.get(total_pack_counts, type_id, 0)
2022-11-09 23:33:50 -05:00
})
end)
2023-03-29 22:54:55 -04:00
packs = Ammo.list_packs(nil, :all, current_user, true)
used_counts = packs |> ActivityLog.get_used_counts(current_user)
original_counts = packs |> Ammo.get_original_counts(current_user)
cprs = packs |> Ammo.get_cprs(current_user)
percentages_remaining = packs |> Ammo.get_percentages_remaining(current_user)
2023-03-29 22:54:55 -04:00
packs =
packs
|> Enum.map(fn %{id: pack_id} = pack ->
pack
2022-11-09 23:33:50 -05:00
|> Jason.encode!()
|> Jason.decode!()
|> Map.merge(%{
2023-03-29 22:54:55 -04:00
"used_count" => Map.get(used_counts, pack_id),
"percentage_remaining" => Map.fetch!(percentages_remaining, pack_id),
"original_count" => Map.get(original_counts, pack_id),
"cpr" => Map.get(cprs, pack_id)
2022-11-09 23:33:50 -05:00
})
end)
2023-03-30 20:43:30 -04:00
shot_records = ActivityLog.list_shot_records(:all, current_user)
2022-11-09 23:33:50 -05:00
containers =
Containers.list_containers(current_user)
|> Enum.map(fn container ->
2023-03-29 22:54:55 -04:00
pack_count = container |> Ammo.get_packs_count_for_container!(current_user)
round_count = container |> Ammo.get_round_count_for_container!(current_user)
2022-11-09 23:33:50 -05:00
container
|> Jason.encode!()
|> Jason.decode!()
|> Map.merge(%{
2023-03-29 22:54:55 -04:00
"pack_count" => pack_count,
2022-11-09 23:33:50 -05:00
"round_count" => round_count
})
end)
json(conn, %{
user: current_user,
2023-03-30 21:53:52 -04:00
types: types,
2023-03-29 22:54:55 -04:00
packs: packs,
2023-03-30 20:43:30 -04:00
shot_records: shot_records,
2022-11-09 23:33:50 -05:00
containers: containers
})
end
end