Files
wanderer/lib/wanderer_app_web/controllers/util_api_controller.ex
guarzo f74c20142c Add api for visible system kill information (#133)
* feat: api for zkill information
2025-02-02 23:53:40 +04:00

42 lines
1.0 KiB
Elixir

defmodule WandererAppWeb.UtilAPIController do
@moduledoc """
Utility functions for parameter handling, fetch helpers, etc.
"""
alias WandererApp.Api
def fetch_map_id(%{"map_id" => mid}) when is_binary(mid) and mid != "" do
{:ok, mid}
end
def fetch_map_id(%{"slug" => slug}) when is_binary(slug) and slug != "" do
case Api.Map.get_map_by_slug(slug) do
{:ok, map} ->
{:ok, map.id}
{:error, _reason} ->
{:error, "No map found for slug=#{slug}"}
end
end
def fetch_map_id(_),
do: {:error, "Must provide either ?map_id=UUID or ?slug=SLUG"}
# Require a given param to be present and non-empty
def require_param(params, key) do
case params[key] do
nil -> {:error, "Missing required param: #{key}"}
"" -> {:error, "Param #{key} cannot be empty"}
val -> {:ok, val}
end
end
# Parse a string into an integer
def parse_int(str) do
case Integer.parse(str) do
{num, ""} -> {:ok, num}
_ -> {:error, "Invalid integer for param id=#{str}"}
end
end
end