dawarich/app/controllers/api/v1/visits_controller.rb

120 lines
3 KiB
Ruby
Raw Normal View History

2024-08-12 16:18:11 -04:00
# frozen_string_literal: true
2024-08-25 14:19:02 -04:00
class Api::V1::VisitsController < ApiController
2025-03-02 15:24:57 -05:00
def index
visits = Visits::Finder.new(current_api_user, params).call
2025-03-02 15:24:57 -05:00
serialized_visits = visits.map do |visit|
Api::VisitSerializer.new(visit).call
end
render json: serialized_visits
end
2025-08-21 12:42:45 -04:00
def create
2025-08-21 12:58:59 -04:00
service = Visits::Create.new(current_api_user, visit_params)
2025-08-22 13:10:40 -04:00
result = service.call
if result
2025-08-21 12:58:59 -04:00
render json: Api::VisitSerializer.new(service.visit).call
else
2025-08-22 13:10:40 -04:00
error_message = service.errors || 'Failed to create visit'
render json: { error: error_message }, status: :unprocessable_entity
2025-08-21 12:58:59 -04:00
end
2025-08-21 12:42:45 -04:00
end
2024-08-12 16:18:11 -04:00
def update
visit = current_api_user.visits.find(params[:id])
visit = update_visit(visit)
2025-03-03 14:11:21 -05:00
render json: Api::VisitSerializer.new(visit).call
2024-08-12 16:18:11 -04:00
end
2025-03-05 14:04:26 -05:00
def merge
# Validate that we have at least 2 visit IDs
visit_ids = params[:visit_ids]
if visit_ids.blank? || visit_ids.length < 2
return render json: { error: 'At least 2 visits must be selected for merging' }, status: :unprocessable_entity
end
# Find all visits that belong to the current user
visits = current_api_user.visits.where(id: visit_ids).order(started_at: :asc)
# Ensure we found all the visits
if visits.length != visit_ids.length
return render json: { error: 'One or more visits not found' }, status: :not_found
end
# Use the service to merge the visits
service = Visits::MergeService.new(visits)
merged_visit = service.call
if merged_visit&.persisted?
render json: Api::VisitSerializer.new(merged_visit).call, status: :ok
else
render json: { error: service.errors.join(', ') }, status: :unprocessable_entity
end
end
def bulk_update
service = Visits::BulkUpdate.new(
2025-03-05 14:04:26 -05:00
current_api_user,
params[:visit_ids],
params[:status]
)
result = service.call
if result
render json: {
message: "#{result[:count]} visits updated successfully",
updated_count: result[:count]
}, status: :ok
else
render json: { error: service.errors.join(', ') }, status: :unprocessable_entity
end
end
2025-08-21 14:41:53 -04:00
def destroy
visit = current_api_user.visits.find(params[:id])
2025-08-22 13:10:40 -04:00
2025-08-21 14:41:53 -04:00
if visit.destroy
head :no_content
else
2025-08-22 13:10:40 -04:00
render json: {
2025-08-21 14:41:53 -04:00
error: 'Failed to delete visit',
errors: visit.errors.full_messages
}, status: :unprocessable_entity
end
rescue ActiveRecord::RecordNotFound
render json: { error: 'Visit not found' }, status: :not_found
end
2024-08-12 16:18:11 -04:00
private
def visit_params
2025-08-21 12:42:45 -04:00
params.require(:visit).permit(:name, :place_id, :status, :latitude, :longitude, :started_at, :ended_at)
2024-08-12 16:18:11 -04:00
end
2025-03-05 14:04:26 -05:00
def merge_params
params.permit(visit_ids: [])
end
def bulk_update_params
params.permit(:status, visit_ids: [])
end
2024-08-12 16:18:11 -04:00
def update_visit(visit)
visit_params.each do |key, value|
2025-08-21 12:42:45 -04:00
next if %w[latitude longitude].include?(key.to_s)
2024-08-12 16:18:11 -04:00
visit[key] = value
visit.name = visit.place.name if visit_params[:place_id].present?
end
2024-08-25 14:19:02 -04:00
visit.save!
2024-08-12 16:18:11 -04:00
visit
end
end