Adds a way to import a tournament to the local server

multistore
Laurent 1 year ago
parent a37ac62968
commit 7daac875f2
  1. 69
      PadelClub/Data/DataStore.swift
  2. 9
      PadelClub/Views/Tournament/TournamentView.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)
}
}
}

@ -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)

Loading…
Cancel
Save