dawarich/app/controllers/api/v1/maps/hexagons_controller.rb

69 lines
2.1 KiB
Ruby
Raw Normal View History

2025-09-12 02:33:51 -04:00
# frozen_string_literal: true
class Api::V1::Maps::HexagonsController < ApiController
skip_before_action :authenticate_api_key, if: :public_sharing_request?
def index
2025-09-16 19:55:42 -04:00
result = Maps::H3HexagonRenderer.call(
2025-09-16 14:41:53 -04:00
params: params,
current_api_user: current_api_user
)
2025-09-12 02:33:51 -04:00
render json: result
2025-09-16 14:41:53 -04:00
rescue Maps::HexagonContextResolver::SharedStatsNotFoundError => e
render json: { error: e.message }, status: :not_found
rescue Maps::DateParameterCoercer::InvalidDateFormatError => e
render json: { error: e.message }, status: :bad_request
2025-09-16 19:55:42 -04:00
rescue Maps::H3HexagonCenters::TooManyHexagonsError,
Maps::H3HexagonCenters::InvalidCoordinatesError,
Maps::H3HexagonCenters::PostGISError => e
2025-09-12 02:33:51 -04:00
render json: { error: e.message }, status: :bad_request
rescue StandardError => _e
handle_service_error
2025-09-12 02:33:51 -04:00
end
def bounds
2025-09-16 14:41:53 -04:00
context = Maps::HexagonContextResolver.call(
params: params,
current_api_user: current_api_user
)
2025-09-12 02:33:51 -04:00
2025-09-16 14:41:53 -04:00
result = Maps::BoundsCalculator.call(
target_user: context[:target_user],
start_date: context[:start_date],
end_date: context[:end_date]
)
2025-09-12 02:33:51 -04:00
2025-09-16 14:41:53 -04:00
if result[:success]
render json: result[:data]
2025-09-12 02:33:51 -04:00
else
render json: {
2025-09-16 14:41:53 -04:00
error: result[:error],
point_count: result[:point_count]
2025-09-12 02:33:51 -04:00
}, status: :not_found
end
2025-09-16 14:41:53 -04:00
rescue Maps::HexagonContextResolver::SharedStatsNotFoundError => e
render json: { error: e.message }, status: :not_found
rescue Maps::BoundsCalculator::NoUserFoundError => e
render json: { error: e.message }, status: :not_found
rescue Maps::BoundsCalculator::NoDateRangeError => e
render json: { error: e.message }, status: :bad_request
rescue Maps::DateParameterCoercer::InvalidDateFormatError => e
render json: { error: e.message }, status: :bad_request
2025-09-12 02:33:51 -04:00
end
private
2025-09-16 19:55:42 -04:00
def hexagon_params
params.permit(:h3_resolution, :uuid, :start_date, :end_date)
2025-09-12 02:33:51 -04:00
end
def handle_service_error
2025-09-12 02:33:51 -04:00
render json: { error: 'Failed to generate hexagon grid' }, status: :internal_server_error
end
def public_sharing_request?
params[:uuid].present?
end
end