-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEmotionRepository.swift
More file actions
75 lines (64 loc) · 2.55 KB
/
EmotionRepository.swift
File metadata and controls
75 lines (64 loc) · 2.55 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
//
// EmotionRepository.swift
// DataSource
//
// Created by 최정인 on 7/28/25.
//
import Domain
final class EmotionRepository: EmotionRepositoryProtocol {
private let networkService = NetworkService.shared
func fetchEmotions() async throws -> [EmotionEntity] {
let endpoint = EmotionEndpoint.fetchEmotions
do {
guard let response = try await networkService.request(endpoint: endpoint, type: [EmotionResponseDTO].self)
else { return [] }
let emotionEntities = response.compactMap({ $0.toEmotionEntity() })
return emotionEntities
} 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 loadEmotion(date: String) async throws -> EmotionEntity? {
let endpoint = EmotionEndpoint.loadEmotion(date: date)
do {
guard let response = try await networkService.request(endpoint: endpoint, type: EmotionResponseDTO.self)
else { throw NetworkError.unknown(description: "Emotion Reponse를 받아오지 못했습니다.") }
let emotionEntity = response.toEmotionEntity()
return emotionEntity
} 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 registerEmotion(emotion: String) async throws -> [RecommendedRoutineEntity] {
let endpoint = EmotionEndpoint.registerEmotion(emotion: emotion)
do {
guard let response = try await networkService.request(endpoint: endpoint, type: RecommendedRoutineListResponseDTO.self)
else { return [] }
let recommendedRoutineEntity = response.recommendedRoutines.compactMap({ $0.toRecommendedRoutineEntity() })
return recommendedRoutineEntity
} catch let error as NetworkError {
switch error {
case .needRetry, .invalidURL, .emptyData:
throw DomainError.requireRetry
default:
throw DomainError.business(error.description)
}
} catch {
throw DomainError.unknown
}
}
}