Fix import from dev

multistore
Laurent 1 year ago
parent c2ad10e297
commit 6ad220bc35
  1. 76
      PadelClub/Utils/Patcher.swift

@ -61,60 +61,62 @@ class Patcher {
return
}
return // TODO review
// return // TODO review
let services = Services(url: "https://xlr.alwaysdata.net/roads/")
if services.hasToken() {
Task {
let clubs: [Club] = try await Store.main.getItems()
let events: [Event] = try await Store.main.getItems()
let tournaments: [Tournament] = try await Store.main.getItems()
// let rounds: [Round] = try await services.get()
// let groupStages: [GroupStage] = try await services.get()
// let matches: [Match] = try await services.get()
// let teamRegistrations: [TeamRegistration] = try await services.get()
// let teamScores: [TeamScore] = try await services.get()
// let playerRegistrations: [PlayerRegistration] = try await services.get()
let courts: [Court] = try await Store.main.getItems()
let dateIntervals: [DateInterval] = try await Store.main.getItems()
// for club in clubs {
// club.creator = userId
// }
for event in events {
event.creator = userId
if let clubId = event.club, DataStore.shared.clubs.findById(clubId) == nil {
if let club = clubs.first(where: { $0.id == clubId }) {
club.creator = userId
try? DataStore.shared.clubs.addOrUpdate(instance: club)
}
}
let clubs: [Club] = try await services.get()
let events: [Event] = try await services.get()
let tournaments: [Tournament] = try await services.get()
let courts: [Court] = try await services.get()
let dateIntervals: [DateInterval] = try await services.get()
let clubIds = Set(events.compactMap { $0.club })
let myClubs: [Club] = clubIds.compactMap { clubId in
clubs.first { $0.id == clubId }
}
myClubs.forEach { $0.creator = userId }
try DataStore.shared.clubs.addOrUpdate(contentOfs: myClubs)
for club in myClubs {
let courts = courts.filter { $0.club == club.id }
try DataStore.shared.courts.addOrUpdate(contentOfs: courts)
}
events.forEach { $0.creator = userId }
// try DataStore.shared.clubs.addOrUpdate(contentOfs: clubs)
try DataStore.shared.events.addOrUpdate(contentOfs: events)
try DataStore.shared.tournaments.addOrUpdate(contentOfs: tournaments)
try DataStore.shared.courts.addOrUpdate(contentOfs: courts)
try DataStore.shared.dateIntervals.addOrUpdate(contentOfs: dateIntervals)
// for tournament in DataStore.shared.tournaments {
// let store = tournament.tournamentStore
// try store.rounds.addOrUpdate(contentOfs: rounds)
// try store.groupStages.addOrUpdate(contentOfs: groupStages)
// try store.matches.addOrUpdate(contentOfs: matches)
// try store.teamRegistrations.addOrUpdate(contentOfs: teamRegistrations)
// try store.teamScores.addOrUpdate(contentOfs: teamScores)
// try store.playerRegistrations.addOrUpdate(contentOfs: playerRegistrations)
// }
for tournament in DataStore.shared.tournaments {
let store = tournament.tournamentStore
let identifier = StoreIdentifier(value: tournament.id, parameterName: "tournament")
let rounds: [Round] = try await services.get(identifier: identifier)
let groupStages: [GroupStage] = try await services.get(identifier: identifier)
let matches: [Match] = try await services.get(identifier: identifier)
let teamRegistrations: [TeamRegistration] = try await services.get(identifier: identifier)
let teamScores: [TeamScore] = try await services.get(identifier: identifier)
let playerRegistrations: [PlayerRegistration] = try await services.get(identifier: identifier)
try store.teamRegistrations.addOrUpdate(contentOfs: teamRegistrations)
try store.rounds.addOrUpdate(contentOfs: rounds)
try store.groupStages.addOrUpdate(contentOfs: groupStages)
try store.matches.addOrUpdate(contentOfs: matches)
try store.playerRegistrations.addOrUpdate(contentOfs: playerRegistrations)
try store.teamScores.addOrUpdate(contentOfs: teamScores)
}
}
} else {
Logger.log("no token for import")
}
}
}

Loading…
Cancel
Save