server/lib/boardwise_web/controllers/coach_controller.ex

45 lines
1.4 KiB
Elixir
Raw Permalink Normal View History

2023-12-04 14:39:27 +00:00
defmodule BoardWiseWeb.CoachController do
use BoardWiseWeb, :controller
alias BoardWise.Coaches
alias BoardWise.Coaches.QueryParams
2023-12-06 02:46:27 +00:00
def index(conn, params) do
query_params =
%QueryParams{}
|> override_param(:rapid_gte, params, :integer)
|> override_param(:rapid_lte, params, :integer)
|> override_param(:blitz_gte, params, :integer)
|> override_param(:blitz_lte, params, :integer)
|> override_param(:bullet_gte, params, :integer)
|> override_param(:bullet_lte, params, :integer)
|> override_param(:languages, params, :strlist)
2023-12-07 12:45:40 +00:00
|> override_param(:titles, params, :strlist)
2023-12-07 14:44:59 +00:00
|> override_param(:sites, params, :strlist)
|> override_param(:page_no, params, :integer)
|> override_param(:page_size, params, :integer)
# Ensure we never attempt to query too large of a response all at once.
query_params = %{query_params | page_size: Enum.min([query_params.page_size, 25])}
coaches = Coaches.list_coaches(query_params)
2023-12-04 14:39:27 +00:00
render(conn, :index, coaches: coaches)
end
2023-12-06 02:46:27 +00:00
defp override_param(query_params, key, params, type) do
case Map.get(params, Atom.to_string(key)) do
nil ->
query_params
val when type == :strlist ->
%{query_params | key => String.split(val, ",")}
2023-12-06 02:46:27 +00:00
val when type == :integer ->
case Integer.parse(val) do
{parsed, ""} -> %{query_params | key => parsed}
_ -> query_params
end
2023-12-06 02:46:27 +00:00
end
end
2023-12-04 14:39:27 +00:00
end