-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRecommendedRoutineRepository.swift
More file actions
58 lines (49 loc) · 2.03 KB
/
RecommendedRoutineRepository.swift
File metadata and controls
58 lines (49 loc) · 2.03 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
//
// RecommendedRoutineRepository.swift
// DataSource
//
// Created by 최정인 on 7/27/25.
//
import Domain
final class RecommendedRoutineRepository: RecommendedRoutineRepositoryProtocol {
private let networkService = NetworkService.shared
func fetchRecommendedRoutine(id: Int) async throws -> RecommendedRoutineEntity? {
let endpoint = RecommendedRoutineEndpoint.fetchRecommendedRoutine(id: id)
do {
guard let recommendedRoutineDTO = try await networkService.request(endpoint: endpoint, type: RecommendedRoutineDTO.self)
else { return nil }
return recommendedRoutineDTO.toRecommendedRoutineEntity()
} catch let error as NetworkError {
switch error {
case .needRetry, .invalidURL, .emptyData:
throw DomainError.requireRetry
default:
throw DomainError.business(error.description)
}
} catch {
throw DomainError.unknown
}
}
func fetchRecommendedRoutines() async throws -> [RecommendedRoutineEntity] {
let endpoint = RecommendedRoutineEndpoint.fetchRecommendedRoutines
do {
guard let response = try await networkService.request(endpoint: endpoint, type: RecommendedRoutineDictionaryResponseDTO.self)
else { return [] }
var entities: [RecommendedRoutineEntity] = []
for (category, recommendedRoutines) in response.recommendedRoutines {
let recommendedRoutineEntity = recommendedRoutines.compactMap({ $0.toRecommendedRoutineEntity(category: category) })
entities.append(contentsOf: recommendedRoutineEntity)
}
return entities
} catch let error as NetworkError {
switch error {
case .needRetry, .invalidURL, .emptyData:
throw DomainError.requireRetry
default:
throw DomainError.business(error.description)
}
} catch {
throw DomainError.unknown
}
}
}