Skip to content

Commit a94af62

Browse files
authored
πŸ”— :: (#199) μ•Œλ¦Ό api 연동
2 parents 248e430 + 8f9da5a commit a94af62

File tree

16 files changed

+335
-56
lines changed

16 files changed

+335
-56
lines changed

β€ŽProjects/Data/Sources/DI/DataSourceAssembly.swiftβ€Ž

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,9 @@ public final class DataSourceAssembly: Assembly {
6969
container.register(RemoteNoticesDataSource.self) { resolver in
7070
RemoteNoticesDataSourceDataSourceImpl(keychain: self.keychain(resolver))
7171
}
72+
73+
container.register(RemoteNotificationsDataSource.self) { resolver in
74+
RemoteNotificationsDataSourceImpl(keychain: self.keychain(resolver))
75+
}
7276
}
7377
}

β€ŽProjects/Data/Sources/DI/RepositoryAssembly.swiftβ€Ž

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ public final class RepositoryAssembly: Assembly {
8888
remoteNoticesDataSource: resolver.resolve(RemoteNoticesDataSource.self)!
8989
)
9090
}
91+
92+
container.register(NotificationsRepository.self) { resolver in
93+
NotificationsRepositoryImpl(
94+
remoteNotificationsDataSource: resolver.resolve(RemoteNotificationsDataSource.self)!
95+
)
96+
}
9197
}
9298
// swiftlint:enable function_body_length
9399
}

β€ŽProjects/Data/Sources/DI/UseCaseAssembly.swiftβ€Ž

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,18 @@ public final class UseCaseAssembly: Assembly {
217217
noticesRepository: resolver.resolve(NoticesRepository.self)!
218218
)
219219
}
220+
221+
// Notifications
222+
container.register(FetchNotificationListUseCase.self) { resolver in
223+
FetchNotificationListUseCase(
224+
notificationsRepository: resolver.resolve(NotificationsRepository.self)!
225+
)
226+
}
227+
container.register(ReadNotificationUseCase.self) { resolver in
228+
ReadNotificationUseCase(
229+
notificationsRepository: resolver.resolve(NotificationsRepository.self)!
230+
)
231+
}
220232
}
221233
// swiftlint:enable function_body_length
222234
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import Foundation
2+
import Domain
3+
4+
struct NotificationListResponseDTO: Codable {
5+
let notifications: [NotificationResponseDTO]
6+
}
7+
8+
public struct NotificationResponseDTO: Codable {
9+
public let notificationID: Int
10+
public let title, content, topic: String
11+
public let detailID: Int
12+
public let createdAt: String
13+
public let new: Bool
14+
15+
enum CodingKeys: String, CodingKey {
16+
case notificationID = "notification_id"
17+
case title, content, topic
18+
case detailID = "detail_id"
19+
case createdAt = "created_at"
20+
case new
21+
}
22+
}
23+
24+
extension NotificationListResponseDTO {
25+
func toDomain() -> [NotificationEntity] {
26+
notifications.map {
27+
NotificationEntity(
28+
notificationID: $0.notificationID,
29+
title: $0.title,
30+
content: $0.content,
31+
topic: $0.topic,
32+
detailID: $0.detailID,
33+
createdAt: $0.createdAt.toJobisDate().toStringFormat("yyyy.MM.dd"),
34+
new: $0.new
35+
)
36+
}
37+
}
38+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import Moya
2+
import AppNetwork
3+
import Domain
4+
5+
public enum NotificationsAPI {
6+
case fetchNotificationList
7+
case patchReadNotification(id: Int)
8+
}
9+
10+
extension NotificationsAPI: JobisAPI {
11+
public typealias ErrorType = JobisError
12+
13+
public var domain: JobisDomain {
14+
return .notifications
15+
}
16+
17+
public var urlPath: String {
18+
switch self {
19+
case .fetchNotificationList:
20+
return ""
21+
22+
case let .patchReadNotification(id):
23+
return "/\(id)"
24+
}
25+
}
26+
27+
public var method: Method {
28+
switch self {
29+
case .fetchNotificationList:
30+
return .get
31+
32+
case .patchReadNotification:
33+
return .patch
34+
}
35+
}
36+
37+
public var task: Task {
38+
switch self {
39+
case .fetchNotificationList:
40+
return .requestParameters(
41+
parameters:
42+
// // TODO: μΆ”ν›„ μ½μŒμ™€ μ•ˆμ½μŒ λΆ„κΈ°μ²˜λ¦¬ ν•„μš”
43+
["is_new": ""],
44+
encoding: URLEncoding.queryString)
45+
default:
46+
return .requestPlain
47+
}
48+
}
49+
50+
public var jwtTokenType: JwtTokenType {
51+
switch self {
52+
default:
53+
return .accessToken
54+
}
55+
}
56+
57+
public var errorMap: [Int: ErrorType]? {
58+
return [:]
59+
}
60+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import RxSwift
2+
import RxCocoa
3+
import Domain
4+
5+
public protocol RemoteNotificationsDataSource {
6+
func fetchNotificationList() -> Single<[NotificationEntity]>
7+
func patchReadNotification(id: Int) -> Completable
8+
}
9+
10+
final class RemoteNotificationsDataSourceImpl: RemoteBaseDataSource<NotificationsAPI>, RemoteNotificationsDataSource {
11+
public func fetchNotificationList() -> Single<[NotificationEntity]> {
12+
request(.fetchNotificationList)
13+
.map(NotificationListResponseDTO.self)
14+
.map { $0.toDomain() }
15+
}
16+
17+
public func patchReadNotification(id: Int) -> Completable {
18+
request(.patchReadNotification(id: id))
19+
.asCompletable()
20+
}
21+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import RxSwift
2+
import Domain
3+
4+
struct NotificationsRepositoryImpl: NotificationsRepository {
5+
private let remoteNotificationsDataSource: any RemoteNotificationsDataSource
6+
7+
init(
8+
remoteNotificationsDataSource: any RemoteNotificationsDataSource
9+
) {
10+
self.remoteNotificationsDataSource = remoteNotificationsDataSource
11+
}
12+
13+
func fetchNotificationsList() -> Single<[NotificationEntity]> {
14+
remoteNotificationsDataSource.fetchNotificationList()
15+
}
16+
17+
func patchReadNotification(id: Int) -> Completable {
18+
remoteNotificationsDataSource.patchReadNotification(id: id)
19+
}
20+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import Foundation
2+
3+
public struct NotificationEntity: Equatable, Hashable {
4+
public let notificationID: Int
5+
public let title, content, topic: String
6+
public let detailID: Int
7+
public let createdAt: String
8+
public let new: Bool
9+
10+
public init(
11+
notificationID: Int,
12+
title: String,
13+
content: String,
14+
topic: String,
15+
detailID: Int,
16+
createdAt: String,
17+
new: Bool
18+
) {
19+
self.notificationID = notificationID
20+
self.title = title
21+
self.content = content
22+
self.topic = topic
23+
self.detailID = detailID
24+
self.createdAt = createdAt
25+
self.new = new
26+
}
27+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import RxSwift
2+
3+
public protocol NotificationsRepository {
4+
func fetchNotificationsList() -> Single<[NotificationEntity]>
5+
func patchReadNotification(id: Int) -> Completable
6+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import RxSwift
2+
3+
public struct FetchNotificationListUseCase {
4+
private let notificationsRepository: any NotificationsRepository
5+
6+
public init(notificationsRepository: any NotificationsRepository) {
7+
self.notificationsRepository = notificationsRepository
8+
}
9+
10+
public func execute() -> Single<[NotificationEntity]> {
11+
notificationsRepository.fetchNotificationsList()
12+
}
13+
}

0 commit comments

Comments
Β (0)