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

94 lines
2.8 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
context = resolve_hexagon_context
result = Maps::HexagonRequestHandler.new(
2025-09-16 14:41:53 -04:00
params: params,
2025-09-19 13:55:27 -04:00
user: context[:user] || current_api_user,
stat: context[:stat],
start_date: context[:start_date],
end_date: context[:end_date]
).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
rescue ActiveRecord::RecordNotFound => e
render json: { error: 'Shared stats not found or no longer available' }, status: :not_found
rescue Stats::CalculateMonth::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
context = resolve_hexagon_context
2025-09-12 02:33:51 -04:00
2025-09-18 12:29:46 -04:00
result = Maps::BoundsCalculator.new(
user: context[:user] || context[:target_user],
2025-09-16 14:41:53 -04:00
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
rescue ActiveRecord::RecordNotFound => e
render json: { error: 'Shared stats not found or no longer available' }, status: :not_found
rescue ArgumentError => e
render json: { error: e.message }, status: :bad_request
2025-09-16 14:41:53 -04:00
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
2025-09-12 02:33:51 -04:00
end
private
def resolve_hexagon_context
return resolve_public_sharing_context if public_sharing_request?
resolve_authenticated_context
end
def resolve_public_sharing_context
stat = Stat.find_by(sharing_uuid: params[:uuid])
raise ActiveRecord::RecordNotFound unless stat&.public_accessible?
{
user: stat.user,
start_date: Date.new(stat.year, stat.month, 1).beginning_of_day.iso8601,
end_date: Date.new(stat.year, stat.month, 1).end_of_month.end_of_day.iso8601,
stat: stat
}
end
def resolve_authenticated_context
{
user: current_api_user,
start_date: params[:start_date],
end_date: params[:end_date],
stat: nil
}
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