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-02-19 15:23:11 -05:00
|
|
|
before_action :authenticate_active_api_user!, only: %i[create update destroy]
|
|
|
|
|
|
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
|
|
|
|
|
.tracked_points
|
|
|
|
|
.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-01-20 14:07:46 -05:00
|
|
|
Points::CreateJob.perform_later(batch_params, current_api_user.id)
|
|
|
|
|
|
|
|
|
|
render json: { message: 'Points are being processed' }
|
2025-01-20 11:59:13 -05:00
|
|
|
end
|
|
|
|
|
|
2025-01-19 06:59:12 -05:00
|
|
|
def update
|
|
|
|
|
point = current_api_user.tracked_points.find(params[:id])
|
|
|
|
|
|
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
|
2024-07-31 13:35:35 -04:00
|
|
|
point = current_api_user.tracked_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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
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
|