2024-07-21 10:45:29 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2024-08-25 14:19:02 -04:00
|
|
|
class Api::V1::PointsController < ApiController
|
2025-11-04 15:21:20 -05:00
|
|
|
before_action :authenticate_active_api_user!, only: %i[create update destroy bulk_destroy]
|
2025-05-16 12:51:48 -04:00
|
|
|
before_action :validate_points_limit, only: %i[create]
|
2025-02-19 15:23:11 -05:00
|
|
|
|
2024-07-31 13:35:35 -04:00
|
|
|
def index
|
|
|
|
|
start_at = params[:start_at]&.to_datetime&.to_i
|
2024-10-02 15:29:56 -04:00
|
|
|
end_at = params[:end_at]&.to_datetime&.to_i || Time.zone.now.to_i
|
|
|
|
|
order = params[:order] || 'desc'
|
2024-07-31 13:35:35 -04:00
|
|
|
|
2024-08-21 13:20:04 -04:00
|
|
|
points = current_api_user
|
2025-08-21 16:32:29 -04:00
|
|
|
.points
|
2024-08-21 13:20:04 -04:00
|
|
|
.where(timestamp: start_at..end_at)
|
2024-10-02 15:29:56 -04:00
|
|
|
.order(timestamp: order)
|
2024-08-21 13:20:04 -04:00
|
|
|
.page(params[:page])
|
|
|
|
|
.per(params[:per_page] || 100)
|
2024-09-30 17:38:32 -04:00
|
|
|
|
2024-09-23 18:10:39 -04:00
|
|
|
serialized_points = points.map { |point| point_serializer.new(point).call }
|
2024-07-31 13:35:35 -04:00
|
|
|
|
2024-09-15 06:07:46 -04:00
|
|
|
response.set_header('X-Current-Page', points.current_page.to_s)
|
|
|
|
|
response.set_header('X-Total-Pages', points.total_pages.to_s)
|
2024-09-14 16:52:25 -04:00
|
|
|
|
2024-09-23 18:10:39 -04:00
|
|
|
render json: serialized_points
|
2024-07-31 13:35:35 -04:00
|
|
|
end
|
2024-07-21 10:45:29 -04:00
|
|
|
|
2025-01-20 11:59:13 -05:00
|
|
|
def create
|
2025-05-03 16:40:44 -04:00
|
|
|
points = Points::Create.new(current_api_user, batch_params).call
|
2025-01-20 14:07:46 -05:00
|
|
|
|
2025-05-03 16:40:44 -04:00
|
|
|
render json: { data: points }
|
2025-01-20 11:59:13 -05:00
|
|
|
end
|
|
|
|
|
|
2025-01-19 06:59:12 -05:00
|
|
|
def update
|
2025-08-21 16:32:29 -04:00
|
|
|
point = current_api_user.points.find(params[:id])
|
2025-01-19 06:59:12 -05:00
|
|
|
|
2025-03-23 19:01:18 -04:00
|
|
|
point.update(lonlat: "POINT(#{point_params[:longitude]} #{point_params[:latitude]})")
|
2025-01-19 06:59:12 -05:00
|
|
|
|
|
|
|
|
render json: point_serializer.new(point).call
|
|
|
|
|
end
|
|
|
|
|
|
2024-07-21 10:45:29 -04:00
|
|
|
def destroy
|
2025-08-21 16:32:29 -04:00
|
|
|
point = current_api_user.points.find(params[:id])
|
2024-07-21 10:45:29 -04:00
|
|
|
point.destroy
|
|
|
|
|
|
|
|
|
|
render json: { message: 'Point deleted successfully' }
|
|
|
|
|
end
|
2024-09-23 18:10:39 -04:00
|
|
|
|
2025-11-04 15:21:20 -05:00
|
|
|
def bulk_destroy
|
|
|
|
|
point_ids = bulk_destroy_params[:point_ids]
|
|
|
|
|
|
2025-11-07 04:03:15 -05:00
|
|
|
render json: { error: 'No points selected' }, status: :unprocessable_entity and return if point_ids.blank?
|
2025-11-04 15:21:20 -05:00
|
|
|
|
|
|
|
|
deleted_count = current_api_user.points.where(id: point_ids).destroy_all.count
|
|
|
|
|
|
|
|
|
|
render json: { message: 'Points were successfully destroyed', count: deleted_count }, status: :ok
|
|
|
|
|
end
|
|
|
|
|
|
2024-09-23 18:10:39 -04:00
|
|
|
private
|
|
|
|
|
|
2025-01-19 06:59:12 -05:00
|
|
|
def point_params
|
|
|
|
|
params.require(:point).permit(:latitude, :longitude)
|
|
|
|
|
end
|
|
|
|
|
|
2025-01-20 14:07:46 -05:00
|
|
|
def batch_params
|
|
|
|
|
params.permit(locations: [:type, { geometry: {}, properties: {} }], batch: {})
|
|
|
|
|
end
|
|
|
|
|
|
2025-11-04 15:21:20 -05:00
|
|
|
def bulk_destroy_params
|
|
|
|
|
params.permit(point_ids: [])
|
|
|
|
|
end
|
|
|
|
|
|
2024-09-23 18:10:39 -04:00
|
|
|
def point_serializer
|
2024-10-02 15:58:19 -04:00
|
|
|
params[:slim] == 'true' ? Api::SlimPointSerializer : Api::PointSerializer
|
2024-09-23 18:10:39 -04:00
|
|
|
end
|
2024-07-21 10:45:29 -04:00
|
|
|
end
|