diff --git a/PadelClub/Data/DataStore.swift b/PadelClub/Data/DataStore.swift index 64a2995..996c619 100644 --- a/PadelClub/Data/DataStore.swift +++ b/PadelClub/Data/DataStore.swift @@ -189,4 +189,73 @@ class DataStore: ObservableObject { } + func copyToLocalServer(tournament: Tournament) { + + Task { + do { + + if let url = PListReader.readString(plist: "local", key: "local_server"), + let login = PListReader.readString(plist: "local", key: "username"), + let pass = PListReader.readString(plist: "local", key: "password") { + let service = Services(url: url) + let _: User = try await service.login(username: login, password: pass) + + tournament.event = nil + _ = try await service.post(tournament) + + for groupStage in tournament.groupStages() { + _ = try await service.post(groupStage) +// for match in groupStage._matches() { +// try await self._insertMatch(match: match, service: service) +// } + } + for round in tournament.rounds() { + try await self._insertRoundAndChildren(round: round, service: service) +// _ = try await service.post(round) +// for match in round._matches() { +// try await self._insertMatch(match: match, service: service) +// } + } + for teamRegistration in tournament.unsortedTeams() { + _ = try await service.post(teamRegistration) + for playerRegistration in teamRegistration.unsortedPlayers() { + _ = try await service.post(playerRegistration) + } + } + for groupStage in tournament.groupStages() { +// _ = try await service.post(groupStage) + for match in groupStage._matches() { + try await self._insertMatch(match: match, service: service) + } + } + for round in tournament.allRounds() { +// _ = try await service.post(round) + for match in round._matches() { + try await self._insertMatch(match: match, service: service) + } + } + + } + } catch { + Logger.error(error) + } + } + + } + + fileprivate func _insertRoundAndChildren(round: Round, service: Services) async throws { + _ = try await service.post(round) + for loserRound in round.loserRounds() { + try await self._insertRoundAndChildren(round: loserRound, service: service) + } + } + + fileprivate func _insertMatch(match: Match, service: Services) async throws { + _ = try await service.post(match) + for teamScore in match.teamScores { + _ = try await service.post(teamScore) + } + + } + } diff --git a/PadelClub/Views/Tournament/TournamentView.swift b/PadelClub/Views/Tournament/TournamentView.swift index 3dd8477..48c8fbf 100644 --- a/PadelClub/Views/Tournament/TournamentView.swift +++ b/PadelClub/Views/Tournament/TournamentView.swift @@ -133,6 +133,15 @@ struct TournamentView: View { if (presentationContext == .agenda || tournament.state() == .running) && tournament.isCanceled == false { ToolbarItem(placement: .topBarTrailing) { Menu { + + #if DEBUG + Button { + DataStore.shared.copyToLocalServer(tournament: self.tournament) + } label: { + Label("Copier sur serveur local", systemImage: "rectangle.portrait.and.arrow.right") + } + #endif + if presentationContext == .agenda { Button { navigation.openTournamentInOrganizer(tournament)