Skip to content

Commit a6f9a79

Browse files
authored
[#85] PushNotification 구조체를 레이어에 맞게 개선한다 (#151)
* refactor: PushNotification 도메인화 * refacot: Presentation, UI 레이어 용 모델인 PushNotificationItem로 대체 * refactor: 데이터, 인프라 레이어 모델 대체 * refactor: 불필요 private init 제거
1 parent 4319097 commit a6f9a79

12 files changed

Lines changed: 134 additions & 100 deletions
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//
2+
// PushNotificationMapping.swift
3+
// DevLog
4+
//
5+
// Created by 최윤진 on 2/27/26.
6+
//
7+
8+
import FirebaseFirestore
9+
10+
extension PushNotificationResponse {
11+
func toDomain() throws -> PushNotification {
12+
guard let id = self.id else {
13+
throw DataError.invalidData("PushNotificationResponse.id is nil")
14+
}
15+
guard let todoKind = TodoKind(rawValue: self.todoKind) else {
16+
throw DataError.invalidData("PushNotificationResponse.todoKind is invalid: \(self.todoKind)")
17+
}
18+
19+
return PushNotification(
20+
id: id,
21+
title: self.title,
22+
body: self.body,
23+
receivedAt: self.receivedAt.dateValue(),
24+
isRead: self.isRead,
25+
todoID: self.todoID,
26+
todoKind: todoKind
27+
)
28+
}
29+
}
30+
31+
extension PushNotificationCursorDTO {
32+
func toDomain() -> PushNotificationCursor {
33+
PushNotificationCursor(
34+
receivedAt: self.receivedAt.dateValue(),
35+
documentID: self.documentID
36+
)
37+
}
38+
39+
static func fromDomain(_ cursor: PushNotificationCursor) -> Self {
40+
PushNotificationCursorDTO(
41+
receivedAt: Timestamp(date: cursor.receivedAt),
42+
documentID: cursor.documentID
43+
)
44+
}
45+
}
46+
47+
extension PushNotificationPageResponse {
48+
func toDomain() throws -> PushNotificationPage {
49+
let items = try self.items.map { try $0.toDomain() }
50+
let nextCursor = self.nextCursor?.toDomain()
51+
return PushNotificationPage(items: items, nextCursor: nextCursor)
52+
}
53+
}

DevLog/Data/Repository/PushNotificationRepositoryImpl.swift

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,33 +36,9 @@ final class PushNotificationRepositoryImpl: PushNotificationRepository {
3636
_ query: PushNotificationQuery,
3737
cursor: PushNotificationCursor?
3838
) async throws -> PushNotificationPage {
39-
let response = try await service.requestNotifications(query, cursor: cursor)
40-
41-
let items: [PushNotification] = response.items.compactMap { dto in
42-
guard
43-
let id = dto.id,
44-
let todoKind = TodoKind(rawValue: dto.todoKind)
45-
else { return nil }
46-
47-
return PushNotification(
48-
id: id,
49-
title: dto.title,
50-
body: dto.body,
51-
receivedAt: dto.receivedAt.dateValue(),
52-
isRead: dto.isRead,
53-
todoID: dto.todoID,
54-
todoKind: todoKind
55-
)
56-
}
57-
58-
let nextCursor = response.nextCursor.map { cursor in
59-
PushNotificationCursor(
60-
receivedAt: cursor.receivedAt.dateValue(),
61-
documentID: cursor.documentID
62-
)
63-
}
64-
65-
return PushNotificationPage(items: items, nextCursor: nextCursor)
39+
let cursorDTO = cursor.map { PushNotificationCursorDTO.fromDomain($0) }
40+
let response = try await service.requestNotifications(query, cursor: cursorDTO)
41+
return try response.toDomain()
6642
}
6743

6844
// 푸시 알림 기록 삭제

DevLog/Infra/DTO/PushNotification.swift renamed to DevLog/Domain/Entity/PushNotification.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import Foundation
99

10-
struct PushNotification: Identifiable {
10+
struct PushNotification {
1111
let id: String
1212
let title: String
1313
let body: String
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// PushNotificationCursorDTO.swift
3+
// DevLog
4+
//
5+
// Created by 최윤진 on 2/27/26.
6+
//
7+
8+
import FirebaseFirestore
9+
10+
struct PushNotificationCursorDTO {
11+
let receivedAt: Timestamp
12+
let documentID: String
13+
}

DevLog/Infra/DTO/PushNotificationCursorResponse.swift

Lines changed: 0 additions & 13 deletions
This file was deleted.

DevLog/Infra/DTO/PushNotificationPageResponse.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77

88
struct PushNotificationPageResponse {
99
let items: [PushNotificationResponse]
10-
let nextCursor: PushNotificationCursorResponse?
10+
let nextCursor: PushNotificationCursorDTO?
1111
}

DevLog/Infra/Service/PushNotificationService.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ final class PushNotificationService {
9191
/// 푸시 알림 기록 요청
9292
func requestNotifications(
9393
_ query: PushNotificationQuery,
94-
cursor: PushNotificationCursor?
94+
cursor: PushNotificationCursorDTO?
9595
) async throws -> PushNotificationPageResponse {
9696
guard let uid = Auth.auth().currentUser?.uid else { throw AuthError.notAuthenticated }
9797

@@ -115,7 +115,7 @@ final class PushNotificationService {
115115

116116
if let cursor {
117117
firestoreQuery = firestoreQuery.start(after: [
118-
Timestamp(date: cursor.receivedAt),
118+
cursor.receivedAt,
119119
cursor.documentID
120120
])
121121
}
@@ -128,12 +128,12 @@ final class PushNotificationService {
128128
try document.data(as: PushNotificationResponse.self)
129129
}
130130

131-
let nextCursor: PushNotificationCursorResponse? = snapshot.documents.last.map { document in
131+
let nextCursor: PushNotificationCursorDTO? = snapshot.documents.last.map { document in
132132
guard let receivedAt = document.data()["receivedAt"] as? Timestamp else {
133133
return nil
134134
}
135135

136-
return PushNotificationCursorResponse(
136+
return PushNotificationCursorDTO(
137137
receivedAt: receivedAt,
138138
documentID: document.documentID
139139
)

DevLog/Presentation/Structure/PinnedTodoItem.swift

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,10 @@ struct PinnedTodoItem: Identifiable, Hashable {
1313
let dueDate: Date?
1414
let kind: TodoKind
1515

16-
private init(
17-
id: String,
18-
title: String,
19-
dueDate: Date?,
20-
kind: TodoKind
21-
) {
22-
self.id = id
23-
self.title = title
24-
self.dueDate = dueDate
25-
self.kind = kind
26-
}
27-
2816
init(from todo: Todo) {
29-
self.init(
30-
id: todo.id,
31-
title: todo.title,
32-
dueDate: todo.dueDate,
33-
kind: todo.kind
34-
)
17+
self.id = todo.id
18+
self.title = todo.title
19+
self.dueDate = todo.dueDate
20+
self.kind = todo.kind
3521
}
3622
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// PushNotificationItem.swift
3+
// DevLog
4+
//
5+
// Created by 최윤진 on 2/27/26.
6+
//
7+
8+
import Foundation
9+
10+
struct PushNotificationItem: Identifiable, Hashable {
11+
let id: String
12+
let title: String
13+
let body: String
14+
let receivedAt: Date
15+
var isRead: Bool
16+
let todoID: String
17+
let todoKind: TodoKind
18+
19+
init(from notification: PushNotification) {
20+
self.id = notification.id
21+
self.title = notification.title
22+
self.body = notification.body
23+
self.receivedAt = notification.receivedAt
24+
self.isRead = notification.isRead
25+
self.todoID = notification.todoID
26+
self.todoKind = notification.todoKind
27+
}
28+
}

DevLog/Presentation/Structure/TodoListItem.swift

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,10 @@ struct TodoListItem: Identifiable, Hashable {
1313
let tags: [String]
1414
let isPinned: Bool
1515

16-
private init(
17-
id: String,
18-
title: String,
19-
tags: [String],
20-
isPinned: Bool
21-
) {
22-
self.id = id
23-
self.title = title
24-
self.tags = tags
25-
self.isPinned = isPinned
26-
}
27-
2816
init(from todo: Todo) {
29-
self.init(
30-
id: todo.id,
31-
title: todo.title,
32-
tags: todo.tags,
33-
isPinned: todo.isPinned
34-
)
17+
self.id = todo.id
18+
self.title = todo.title
19+
self.tags = todo.tags
20+
self.isPinned = todo.isPinned
3521
}
3622
}

0 commit comments

Comments
 (0)