From 3232e985782a234aaef56c7ad9d1cc666d527761 Mon Sep 17 00:00:00 2001 From: eemdeeks Date: Sat, 20 Jan 2024 20:08:16 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20repositery=20=EC=84=A0=EC=88=98?= =?UTF-8?q?=EC=B6=94=EA=B0=80=20=EA=B8=B0=EB=8A=A5(#128)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implementation/TeamDBRepository.swift | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/Projects/Data/Sources/Repositories/Implementation/TeamDBRepository.swift b/Projects/Data/Sources/Repositories/Implementation/TeamDBRepository.swift index 1f1dde9..aabd0b6 100644 --- a/Projects/Data/Sources/Repositories/Implementation/TeamDBRepository.swift +++ b/Projects/Data/Sources/Repositories/Implementation/TeamDBRepository.swift @@ -27,8 +27,12 @@ public struct TeamDBRepository: TeamDBRepositoryInterface { team.sort { pre, _ in pre.isSelected } - - return team.map { Team(id: $0.id, name: $0.teamName) } + + return team.map { + Team(id: $0.id, name: $0.teamName, members: $0.teamPlayers.map{ + Player(id: $0.id,name: $0.name, backNumber: $0.backNumber) + }) + } } catch { fatalError(error.localizedDescription) } @@ -45,6 +49,24 @@ public struct TeamDBRepository: TeamDBRepositoryInterface { } } + public func addPlayer(player: Player) { + do { + var fetchDescriptor = FetchDescriptor() + fetchDescriptor.sortBy = [SortDescriptor(\.updatedAt, order: .reverse)] + + var team = try modelContext.fetch(fetchDescriptor) + team.sort { pre, _ in + pre.isSelected + } + + var human = SchemaV1.TeamPlayer(id: player.id ,name: player.name, backNumber: player.backNumber) + team[0].teamPlayers.insert(human, at: 0) + try modelContext.save() + } catch { + fatalError(error.localizedDescription) + } + } + // func loadUniform(index: Int) { // do { // var fetchDescriptor = FetchDescriptor()