-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathReportRepository.swift
More file actions
98 lines (85 loc) · 2.99 KB
/
ReportRepository.swift
File metadata and controls
98 lines (85 loc) · 2.99 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
94
95
96
97
98
//
// ReportRepository.swift
// DataSource
//
// Created by 이동현 on 11/9/25.
//
import Domain
import Foundation
final class ReportRepository: ReportRepositoryProtocol {
private let networkService = NetworkService.shared
func report(
title: String,
content: String?,
category: ReportType,
location: LocationEntity?,
photoURLs: [String]
) async throws -> Int? {
let reportDTO = ReportDTO(
reportId: nil,
reportDate: nil,
reportTitle: title,
reportContent: content,
reportLocation: location?.address ?? "",
reportStatus: nil,
reportCategory: category.description,
reportImageUrl: nil,
reportImageUrls: photoURLs,
latitude: location?.latitude,
longitude: location?.longitude
)
let endpoint = ReportEndpoint.register(report: reportDTO)
do {
guard let id = try await networkService.request(endpoint: endpoint, type: Int.self) else { return nil }
return id
} 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 fetchReports() async throws -> [ReportEntity] {
let endpoint = ReportEndpoint.fetchReports
do {
guard let response = try await networkService.request(endpoint: endpoint, type: ReportDictonaryDTO.self)
else { return [] }
var reportEntities: [ReportEntity] = []
for (date, reports) in response.reportInfos {
let reportHistories = reports.compactMap({ try? $0.toReportEntity(date: date) })
reportEntities += reportHistories
}
return reportEntities
} 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 fetchReportDetail(reportId: Int) async throws -> ReportEntity? {
let endpoint = ReportEndpoint.fetchReportDetail(reportId: reportId)
do {
guard let response = try await networkService.request(endpoint: endpoint, type: ReportDTO.self)
else { return nil }
return try response.toReportEntity()
} catch let error as NetworkError {
switch error {
case .needRetry, .invalidURL, .emptyData:
throw DomainError.requireRetry
default:
throw DomainError.business(error.description)
}
} catch {
throw DomainError.unknown
}
}
}