dawarich/app/controllers/export_controller.rb

33 lines
868 B
Ruby
Raw Normal View History

2024-04-26 12:59:58 -04:00
# frozen_string_literal: true
2024-03-23 16:46:18 -04:00
class ExportController < ApplicationController
before_action :authenticate_user!
def download
2024-05-23 14:12:23 -04:00
export = current_user.export_data(start_at:, end_at:)
2024-05-23 14:12:23 -04:00
send_data export, filename:, type: 'applocation/json', disposition: 'attachment'
2024-04-26 12:59:58 -04:00
end
private
def filename
2024-05-23 14:12:23 -04:00
first_point_datetime = Time.zone.at(start_at).to_s
last_point_datetime = Time.zone.at(end_at).to_s
2024-04-26 12:59:58 -04:00
"dawarich-export-#{first_point_datetime}-#{last_point_datetime}.json".gsub(' ', '_')
end
2024-05-23 14:12:23 -04:00
def start_at
2024-05-25 07:26:56 -04:00
first_point_timestamp = current_user.tracked_points.order(timestamp: :asc)&.first&.timestamp
2024-05-23 14:12:23 -04:00
@start_at ||= first_point_timestamp || 1.month.ago.to_i
2024-05-23 14:12:23 -04:00
end
def end_at
last_point_timestamp = current_user.tracked_points.order(timestamp: :asc)&.last&.timestamp
@end_at ||= last_point_timestamp || Time.current.to_i
2024-05-23 14:12:23 -04:00
end
2024-03-23 16:46:18 -04:00
end