-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRoutineRepository.swift
More file actions
93 lines (77 loc) · 3.98 KB
/
Copy pathRoutineRepository.swift
File metadata and controls
93 lines (77 loc) · 3.98 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//
// RoutineRepository.swift
// DataSource
//
// Created by 최정인 on 7/30/25.
//
import Domain
final class RoutineRepository: RoutineRepositoryProtocol {
private let networkService = NetworkService.shared
func createRoutine(routine: RoutineCreationEntity) async throws {
let routineCreationDTO = RoutineCreationDTO(
routineId: nil,
updateApplyDate: routine.applyDateType?.rawValue,
routineName: routine.name,
repeatDay: routine.repeatDay.map { $0.rawValue },
routineStartDate: routine.startDate,
routineEndDate: routine.endDate,
executionTime: routine.executionTime,
subRoutineName: routine.subroutines,
recommendedRoutineType: routine.recommendedRoutineType?.rawValue)
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: RoutineDTO.self) else { return nil }
return response.toRoutineEntity()
}
func fetchRoutines(from startDate: String, to endDate: String) async throws -> [String: (routines: [RoutineEntity], allCompleted: Bool)] {
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], Bool)] = [:]
for (date, routineDTO) in response.routines {
let allCompleted = routineDTO.allCompleted
let routines = routineDTO.routineList.compactMap({ $0.toRoutineEntity() })
result[date] = (routines, allCompleted)
}
return result
}
func updateRoutine(routine: RoutineCreationEntity) async throws {
let routineUpdateDTO = RoutineCreationDTO(
routineId: routine.id,
updateApplyDate: routine.applyDateType?.rawValue,
routineName: routine.name,
repeatDay: routine.repeatDay.map { $0.rawValue },
routineStartDate: routine.startDate,
routineEndDate: routine.endDate,
executionTime: routine.executionTime,
subRoutineName: routine.subroutines,
recommendedRoutineType: routine.recommendedRoutineType?.rawValue)
let endpoint = RoutineEndpoint.updateRoutine(routine: routineUpdateDTO)
_ = try await networkService.request(endpoint: endpoint, type: EmptyResponseDTO.self)
}
func deleteAllRoutine(routineId: String) async throws {
let endpoint = RoutineEndpoint.deleteAllRoutine(routineId: routineId)
_ = try await networkService.request(endpoint: endpoint, type: EmptyResponseDTO.self)
}
func deleteDailyRoutine(routineId: String) async throws {
let endpoint = RoutineEndpoint.deleteDailyRoutine(routineId: routineId)
_ = try await networkService.request(endpoint: endpoint, type: EmptyResponseDTO.self)
}
func updateRoutineCompletions(routines: [RoutineCompletionEntity]) async throws {
guard let routine = routines.first else { return }
let performedDate = routine.performedDate
let completionDTO = routines.map({ RoutineCompletionDTO(
routineId: $0.routineId,
completeYn: $0.completeYn,
historySeq: $0.historySeq,
routineType: $0.routineType) })
let completionListDTO = RoutineCompletionListDTO(
performedDate: performedDate,
routineCompletionInfos: completionDTO)
let endpoint = RoutineEndpoint.updateRoutineCompletion(routines: completionListDTO)
_ = try await networkService.request(endpoint: endpoint, type: EmptyResponseDTO.self)
}
}