mirror of
https://github.com/jcwimer/wrestlingApp
synced 2026-03-25 01:14:43 +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
|
||||
@@ -1,11 +1,11 @@
|
||||
class WrestlingdevImporter
|
||||
|
||||
##### Note, the json contains id's for each row in the tables as well as it's associations
|
||||
##### Note, the json contains id's for each row in the tables as well as its associations
|
||||
##### this ignores those ids and uses this tournament id and then looks up associations based on name
|
||||
##### and this tournament id
|
||||
def initialize(tournament, import_json)
|
||||
def initialize(tournament, backup)
|
||||
@tournament = tournament
|
||||
@import_data = JSON.parse(import_json)
|
||||
@import_data = JSON.parse(Base64.decode64(backup.backup_data))
|
||||
end
|
||||
|
||||
def import
|
||||
@@ -26,6 +26,7 @@ class WrestlingdevImporter
|
||||
end
|
||||
|
||||
def destroy_all
|
||||
@tournament.mat_assignment_rules.destroy_all
|
||||
@tournament.mats.destroy_all
|
||||
@tournament.matches.destroy_all
|
||||
@tournament.schools.each do |school|
|
||||
@@ -42,6 +43,8 @@ class WrestlingdevImporter
|
||||
parse_mats(@import_data["tournament"]["mats"])
|
||||
parse_wrestlers(@import_data["tournament"]["wrestlers"])
|
||||
parse_matches(@import_data["tournament"]["matches"])
|
||||
puts "Parsing mat assignment rules"
|
||||
parse_mat_assignment_rules(@import_data["tournament"]["mat_assignment_rules"])
|
||||
end
|
||||
|
||||
def parse_tournament(attributes)
|
||||
@@ -70,6 +73,34 @@ class WrestlingdevImporter
|
||||
end
|
||||
end
|
||||
|
||||
def parse_mat_assignment_rules(mat_assignment_rules)
|
||||
mat_assignment_rules.each do |rule_attributes|
|
||||
mat_name = rule_attributes.dig("mat", "name")
|
||||
mat = Mat.find_by(name: mat_name, tournament_id: @tournament.id)
|
||||
|
||||
# Map max values of weight_classes to their new IDs
|
||||
new_weight_classes = rule_attributes["weight_classes"].map do |max_value|
|
||||
Weight.find_by(max: max_value, tournament_id: @tournament.id)&.id
|
||||
end.compact
|
||||
|
||||
# Extract bracket_positions and rounds
|
||||
bracket_positions = rule_attributes["bracket_positions"]
|
||||
rounds = rule_attributes["rounds"]
|
||||
|
||||
rule_attributes.except!("id", "mat", "tournament_id", "weight_classes")
|
||||
|
||||
MatAssignmentRule.create(
|
||||
rule_attributes.merge(
|
||||
tournament_id: @tournament.id,
|
||||
mat_id: mat&.id,
|
||||
weight_classes: new_weight_classes,
|
||||
bracket_positions: bracket_positions,
|
||||
rounds: rounds
|
||||
)
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
def parse_wrestlers(wrestlers)
|
||||
wrestlers.each do |wrestler_attributes|
|
||||
school = School.find_by(name: wrestler_attributes["school"]["name"], tournament_id: @tournament.id)
|
||||
|
||||
Reference in New Issue
Block a user