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-03-18 21:06:00 -04:00
|
|
|
ammo_types = Ammo.list_ammo_types(current_user)
|
|
|
|
used_counts = ammo_types |> ActivityLog.get_used_count_for_ammo_types(current_user)
|
|
|
|
round_counts = ammo_types |> Ammo.get_round_count_for_ammo_types(current_user)
|
|
|
|
ammo_group_counts = ammo_types |> Ammo.get_ammo_groups_count_for_types(current_user)
|
|
|
|
|
|
|
|
total_ammo_group_counts =
|
|
|
|
ammo_types |> Ammo.get_ammo_groups_count_for_types(current_user, true)
|
|
|
|
|
|
|
|
average_costs = ammo_types |> Ammo.get_average_cost_for_ammo_types(current_user)
|
2022-11-09 23:33:50 -05:00
|
|
|
|
2023-03-18 21:06:00 -04:00
|
|
|
ammo_types =
|
|
|
|
ammo_types
|
|
|
|
|> Enum.map(fn %{id: ammo_type_id} = ammo_type ->
|
2022-11-09 23:33:50 -05:00
|
|
|
ammo_type
|
|
|
|
|> Jason.encode!()
|
|
|
|
|> Jason.decode!()
|
|
|
|
|> Map.merge(%{
|
2023-03-18 21:06:00 -04:00
|
|
|
"average_cost" => Map.get(average_costs, ammo_type_id),
|
|
|
|
"round_count" => Map.get(round_counts, ammo_type_id, 0),
|
|
|
|
"used_count" => Map.get(used_counts, ammo_type_id, 0),
|
|
|
|
"ammo_group_count" => Map.get(ammo_group_counts, ammo_type_id, 0),
|
|
|
|
"total_ammo_group_count" => Map.get(total_ammo_group_counts, ammo_type_id, 0)
|
2022-11-09 23:33:50 -05:00
|
|
|
})
|
|
|
|
end)
|
|
|
|
|
2023-03-18 21:06:00 -04:00
|
|
|
ammo_groups = Ammo.list_ammo_groups(nil, true, current_user)
|
|
|
|
used_counts = ammo_groups |> ActivityLog.get_used_counts(current_user)
|
|
|
|
original_counts = ammo_groups |> Ammo.get_original_counts(current_user)
|
|
|
|
cprs = ammo_groups |> Ammo.get_cprs(current_user)
|
|
|
|
|
2022-11-09 23:33:50 -05:00
|
|
|
ammo_groups =
|
2023-03-18 21:06:00 -04:00
|
|
|
ammo_groups
|
|
|
|
|> Enum.map(fn %{id: ammo_group_id} = ammo_group ->
|
|
|
|
percentage_remaining = ammo_group |> Ammo.get_percentage_remaining(current_user)
|
2022-11-09 23:33:50 -05:00
|
|
|
|
|
|
|
ammo_group
|
|
|
|
|> Jason.encode!()
|
|
|
|
|> Jason.decode!()
|
|
|
|
|> Map.merge(%{
|
2023-03-18 21:06:00 -04:00
|
|
|
"used_count" => Map.get(used_counts, ammo_group_id),
|
2022-11-12 14:37:55 -05:00
|
|
|
"percentage_remaining" => percentage_remaining,
|
2023-03-18 21:06:00 -04:00
|
|
|
"original_count" => Map.get(original_counts, ammo_group_id),
|
|
|
|
"cpr" => Map.get(cprs, ammo_group_id)
|
2022-11-09 23:33:50 -05:00
|
|
|
})
|
|
|
|
end)
|
|
|
|
|
|
|
|
shot_groups = ActivityLog.list_shot_groups(current_user)
|
|
|
|
|
|
|
|
containers =
|
|
|
|
Containers.list_containers(current_user)
|
|
|
|
|> Enum.map(fn container ->
|
2023-03-18 21:06:00 -04:00
|
|
|
ammo_group_count = container |> Ammo.get_ammo_groups_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(%{
|
|
|
|
"ammo_group_count" => ammo_group_count,
|
|
|
|
"round_count" => round_count
|
|
|
|
})
|
|
|
|
end)
|
|
|
|
|
|
|
|
json(conn, %{
|
|
|
|
user: current_user,
|
|
|
|
ammo_types: ammo_types,
|
|
|
|
ammo_groups: ammo_groups,
|
|
|
|
shot_groups: shot_groups,
|
|
|
|
containers: containers
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|