move json to api, change tile loading api
This commit is contained in:
@@ -63,6 +63,66 @@ defmodule MoundHuntersWeb.ApiController do
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Get tile metadata by ID.
|
||||
GET /api/meta/tile/:id
|
||||
Returns: tile metadata including bounds, status, availability flags
|
||||
"""
|
||||
def get_tile(conn) do
|
||||
tile_id = conn.path_params["id"]
|
||||
|
||||
case MoundHunters.Repo.get_tile(tile_id) do
|
||||
{:ok, tile} ->
|
||||
conn
|
||||
|> put_resp_content_type("application/json")
|
||||
|> put_resp_header("cache-control", "public, max-age=60")
|
||||
|> send_resp(200, Jason.encode!(tile))
|
||||
|
||||
{:error, :not_found} ->
|
||||
send_error(conn, 404, "Tile not found")
|
||||
|
||||
{:error, reason} ->
|
||||
Logger.error("Failed to get tile: #{inspect(reason)}")
|
||||
send_error(conn, 500, "Failed to retrieve tile")
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Get tile metadata by coordinates.
|
||||
GET /api/meta/tile?lat=40.0&lng=-82.5
|
||||
Returns: tile metadata for the tile containing the given coordinates
|
||||
"""
|
||||
def get_tile_by_coords(conn) do
|
||||
with lat_str when is_binary(lat_str) <- conn.query_params["lat"],
|
||||
lng_str when is_binary(lng_str) <- conn.query_params["lng"],
|
||||
{lat, _} <- Float.parse(lat_str),
|
||||
{lng, _} <- Float.parse(lng_str) do
|
||||
case MoundHunters.Repo.get_tile_at_coords(lat, lng) do
|
||||
{:ok, tile} ->
|
||||
conn
|
||||
|> put_resp_content_type("application/json")
|
||||
|> put_resp_header("cache-control", "public, max-age=60")
|
||||
|> send_resp(200, Jason.encode!(tile))
|
||||
|
||||
{:error, :not_found} ->
|
||||
send_error(conn, 404, "No tile found at coordinates")
|
||||
|
||||
{:error, reason} ->
|
||||
Logger.error("Failed to get tile by coords: #{inspect(reason)}")
|
||||
send_error(conn, 500, "Failed to retrieve tile")
|
||||
end
|
||||
else
|
||||
nil ->
|
||||
send_error(conn, 400, "Missing lat or lng parameter")
|
||||
|
||||
:error ->
|
||||
send_error(conn, 400, "Invalid lat or lng format")
|
||||
|
||||
_ ->
|
||||
send_error(conn, 400, "Invalid request")
|
||||
end
|
||||
end
|
||||
|
||||
defp send_error(conn, status, message) do
|
||||
conn
|
||||
|> put_private(:error_message, message)
|
||||
|
||||
Reference in New Issue
Block a user