-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRoutineRepository.swift
More file actions
65 lines (52 loc) · 2.66 KB
/
RoutineRepository.swift
File metadata and controls
65 lines (52 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//
// RoutineRepository.swift
// DataSource
//
// Created by 최정인 on 7/30/25.
//
import Domain
final class RoutineRepository: RoutineRepositoryProtocol {
private let networkService = NetworkService.shared
func createRoutine(routineSummary: RoutineSummaryEntity, subRoutineSummaries: [SubRoutineSummaryEntity]) async throws {
let subRoutineNames = subRoutineSummaries.compactMap { $0.subRoutineName }
let routineCreationDTO = RoutineCreationDTO(
routineName: routineSummary.routineName,
repeatDay: routineSummary.repeatDay.map { $0.rawValue },
executionTime: routineSummary.executionTime,
subRoutineName: subRoutineNames)
let endpoint = RoutineEndpoint.createRoutine(routine: routineCreationDTO)
_ = try await networkService.request(endpoint: endpoint, type: EmptyResponseDTO.self)
}
func fetchRoutine(routineId: String) async throws -> RoutineEntity? {
let endpoint = RoutineEndpoint.fetchRoutine(routineId: routineId)
guard let response = try await networkService.request(endpoint: endpoint, type: RoutineResponseDTO.self) else { return nil }
return response.toRoutineEntity()
}
func fetchRoutines(from startDate: String, to endDate: String) async throws -> [String: [RoutineEntity]] {
let endpoint = RoutineEndpoint.fetchRoutines(startDate: startDate, endDate: endDate)
guard let response = try await networkService.request(endpoint: endpoint, type: RoutineDictionaryDTO.self)
else { return [:] }
var result: [String: [RoutineEntity]] = [:]
for (date, routineDTO) in response.routines {
result[date] = routineDTO.compactMap({ $0.toRoutineEntity() })
}
return result
}
func updateRoutine(routineSummary: RoutineSummaryEntity, subRoutineSummaries: [SubRoutineSummaryEntity]) async throws {
guard let routineId = routineSummary.routineId else { return }
let subRoutineDTO = subRoutineSummaries.map {
SubRoutineUpdateDTO(
subRoutineId: $0.subRoutineId,
subRoutineName: $0.subRoutineName,
sortOrder: $0.sortOrder)
}
let routineUpdateDTO = RoutineUpdateDTO(
routineId: routineId,
routineName: routineSummary.routineName,
repeatDay: routineSummary.repeatDay.map { $0.rawValue },
executionTime: routineSummary.executionTime,
subRoutineInfos: subRoutineDTO)
let endpoint = RoutineEndpoint.updateRoutine(routine: routineUpdateDTO)
_ = try await networkService.request(endpoint: endpoint, type: EmptyResponseDTO.self)
}
}