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

67 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
result = Maps::HexagonRequestHandler.new(
2025-09-16 14:41:53 -04:00
params: params,
current_api_user: current_api_user
).call
2025-09-12 02:33:51 -04:00
render json: result
rescue ActionController::ParameterMissing => e
render json: { error: "Missing required parameter: #{e.param}" }, status: :bad_request
rescue ActionController::BadRequest => e
render json: { error: e.message }, status: :bad_request
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
rescue 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-18 12:29:46 -04:00
result = Maps::BoundsCalculator.new(
2025-09-16 14:41:53 -04:00
target_user: context[:target_user],
start_date: context[:start_date],
end_date: context[:end_date]
2025-09-18 12:29:46 -04:00
).call
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
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