mirror of
https://github.com/jcwimer/wrestlingApp
synced 2026-05-12 16:59:00 +00:00
Added tournament backups to the database and added pages to restore and create backups
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
class TournamentBackupService
|
||||
def initialize(tournament, reason)
|
||||
@tournament = tournament
|
||||
@reason = reason
|
||||
end
|
||||
|
||||
def create_backup
|
||||
if Rails.env.production?
|
||||
self.delay(:job_owner_id => @tournament.id, :job_owner_type => "Create a backup").create_backup_raw
|
||||
else
|
||||
self.create_backup_raw
|
||||
end
|
||||
end
|
||||
|
||||
def create_backup_raw
|
||||
# Generate the JSON directly in Ruby and encode it
|
||||
backup_data = Base64.encode64(generate_json.to_json)
|
||||
|
||||
begin
|
||||
# Save the backup with encoded data
|
||||
TournamentBackup.create!(tournament: @tournament, backup_data: backup_data, backup_reason: @reason)
|
||||
Rails.logger.info("Backup created successfully for tournament ##{@tournament.id}")
|
||||
rescue ActiveRecord::RecordInvalid => e
|
||||
Rails.logger.error("Failed to save backup: #{e.message}")
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def generate_json
|
||||
{
|
||||
tournament: {
|
||||
attributes: @tournament.attributes,
|
||||
schools: @tournament.schools.map(&:attributes),
|
||||
weights: @tournament.weights.map(&:attributes),
|
||||
mats: @tournament.mats.map(&:attributes),
|
||||
mat_assignment_rules: @tournament.mat_assignment_rules.map do |rule|
|
||||
rule.attributes.merge(
|
||||
mat: Mat.find_by(id: rule.mat_id)&.attributes.slice("name"),
|
||||
weight_classes: rule.weight_classes.map do |weight_id|
|
||||
Weight.find_by(id: weight_id)&.max
|
||||
end
|
||||
)
|
||||
end,
|
||||
wrestlers: @tournament.wrestlers.map do |wrestler|
|
||||
wrestler.attributes.merge(
|
||||
school: wrestler.school&.attributes,
|
||||
weight: wrestler.weight&.attributes
|
||||
)
|
||||
end,
|
||||
matches: @tournament.matches.sort_by(&:bout_number).map do |match|
|
||||
match.attributes.merge(
|
||||
w1_name: Wrestler.find_by(id: match.w1)&.name,
|
||||
w2_name: Wrestler.find_by(id: match.w2)&.name,
|
||||
winner_name: Wrestler.find_by(id: match.winner_id)&.name,
|
||||
weight: Weight.find_by(id: match.weight_id)&.attributes,
|
||||
mat: Mat.find_by(id: match.mat_id)&.attributes
|
||||
)
|
||||
end
|
||||
}
|
||||
}
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user