Skip to content

Commit acdb5fd

Browse files
authored
[#313] Firestore 경로를 enum으로 정리한다 (#316)
* style: Firestore 관리를 enum으로 처리 * fix: 기존 경로와 다른 값이 들어가는 현상 해결
1 parent 495e03d commit acdb5fd

9 files changed

Lines changed: 95 additions & 31 deletions
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//
2+
// FirestorePath.swift
3+
// DevLog
4+
//
5+
// Created by opfic on 3/26/26.
6+
//
7+
8+
enum FirestorePath {
9+
private enum Collection: String {
10+
case users
11+
case userData
12+
case counters
13+
case todoLists
14+
case notifications
15+
case webPages
16+
}
17+
18+
enum UserData: String {
19+
case info
20+
case tokens
21+
case settings
22+
}
23+
24+
enum Counter: String {
25+
case todo
26+
}
27+
28+
static func user(_ uid: String) -> String {
29+
"\(Collection.users.rawValue)/\(uid)"
30+
}
31+
32+
static func userData(_ uid: String, document: UserData? = nil) -> String {
33+
let path = "\(user(uid))/\(Collection.userData.rawValue)"
34+
guard let document else { return path }
35+
return "\(path)/\(document.rawValue)"
36+
}
37+
38+
static func counter(_ uid: String, document: Counter) -> String {
39+
"\(user(uid))/\(Collection.counters.rawValue)/\(document.rawValue)"
40+
}
41+
42+
static func todos(_ uid: String) -> String {
43+
"\(user(uid))/\(Collection.todoLists.rawValue)"
44+
}
45+
46+
static func todo(_ uid: String, todoId: String) -> String {
47+
"\(todos(uid))/\(todoId)"
48+
}
49+
50+
static func notifications(_ uid: String) -> String {
51+
"\(user(uid))/\(Collection.notifications.rawValue)"
52+
}
53+
54+
static func webPages(_ uid: String) -> String {
55+
"\(user(uid))/\(Collection.webPages.rawValue)"
56+
}
57+
58+
static func webPage(_ uid: String, documentId: String) -> String {
59+
"\(webPages(uid))/\(documentId)"
60+
}
61+
}

DevLog/Infra/Service/AuthService.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ final class AuthService {
5252

5353
do {
5454
let document = try await store
55-
.collection("users/\(uid)/userData")
56-
.document("info")
55+
.document(FirestorePath.userData(uid, document: .info))
5756
.getDocument()
5857

5958
let providerID = document.data()?["currentProvider"] as? String

DevLog/Infra/Service/PushNotificationService.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ final class PushNotificationService {
3030
}
3131

3232
do {
33-
let settingsRef = store.document("users/\(uid)/userData/settings")
33+
let settingsRef = store.document(FirestorePath.userData(uid, document: .settings))
3434
let doc = try await settingsRef.getDocument()
3535

3636
if let allowPush = doc.data()?["allowPushNotification"] as? Bool {
@@ -56,7 +56,7 @@ final class PushNotificationService {
5656
}
5757

5858
do {
59-
let settingsRef = store.document("users/\(uid)/userData/settings")
59+
let settingsRef = store.document(FirestorePath.userData(uid, document: .settings))
6060
let doc = try await settingsRef.getDocument()
6161

6262
guard let hour = doc.data()?["pushNotificationHour"] as? Int else {
@@ -84,7 +84,7 @@ final class PushNotificationService {
8484
}
8585

8686
do {
87-
let settingsRef = store.document("users/\(uid)/userData/settings")
87+
let settingsRef = store.document(FirestorePath.userData(uid, document: .settings))
8888

8989
var dict: [String: Any] = ["allowPushNotification": isEnabled]
9090

@@ -182,7 +182,7 @@ final class PushNotificationService {
182182
guard let uid = Auth.auth().currentUser?.uid else { throw AuthError.notAuthenticated }
183183

184184
let subject = PassthroughSubject<Int, Error>()
185-
let listener = store.collection("users/\(uid)/notifications")
185+
let listener = store.collection(FirestorePath.notifications(uid))
186186
.whereField("isRead", isEqualTo: false)
187187
.addSnapshotListener { snapshot, error in
188188
if let error {
@@ -237,7 +237,7 @@ final class PushNotificationService {
237237
throw AuthError.notAuthenticated
238238
}
239239

240-
let collection = store.collection("users/\(uid)/notifications")
240+
let collection = store.collection(FirestorePath.notifications(uid))
241241
let snapshot = try await collection.whereField("todoId", isEqualTo: todoId).getDocuments()
242242

243243
guard let document = snapshot.documents.first else {
@@ -264,7 +264,7 @@ private extension PushNotificationService {
264264
uid: String,
265265
query: PushNotificationQuery
266266
) -> Query {
267-
var firestoreQuery: Query = store.collection("users/\(uid)/notifications")
267+
var firestoreQuery: Query = store.collection(FirestorePath.notifications(uid))
268268

269269
if let thresholdDate = query.timeFilter.thresholdDate {
270270
firestoreQuery = firestoreQuery.whereField(

DevLog/Infra/Service/SocialLogin/AppleAuthenticationService.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ final class AppleAuthenticationService: AuthenticationService {
6666

6767
// 이미 가입된 사용자일 경우 Firestore에서 사용자 이름 가져오기
6868
if displayName == nil {
69-
let doc = try await store.document("users/\(result.user.uid)/userData/info").getDocument()
69+
let doc = try await store
70+
.document(FirestorePath.userData(result.user.uid, document: .info))
71+
.getDocument()
7072
displayName = doc.data()?["appleName"] as? String
7173
}
7274

@@ -97,7 +99,7 @@ final class AppleAuthenticationService: AuthenticationService {
9799

98100
func signOut(_ uid: String) async throws {
99101
do {
100-
let infoRef = store.document("users/\(uid)/userData/tokens")
102+
let infoRef = store.document(FirestorePath.userData(uid, document: .tokens))
101103
let doc = try await infoRef.getDocument()
102104

103105
if doc.exists {
@@ -166,7 +168,7 @@ final class AppleAuthenticationService: AuthenticationService {
166168
logger.info("Starting Apple access token revocation for unlink. uid: \(uid)")
167169
try await revokeAppleAccessToken(token: accessToken)
168170

169-
let tokensRef = store.document("users/\(uid)/userData/tokens")
171+
let tokensRef = store.document(FirestorePath.userData(uid, document: .tokens))
170172

171173
logger.info("Starting Apple token document fetch for unlink. uid: \(uid)")
172174
let doc = try await tokensRef.getDocument()

DevLog/Infra/Service/SocialLogin/GithubAuthenticationService.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ final class GithubAuthenticationService: NSObject, AuthenticationService {
7474

7575
func signOut(_ uid: String) async throws {
7676
do {
77-
let infoRef = store.document("users/\(uid)/userData/tokens")
77+
let infoRef = store.document(FirestorePath.userData(uid, document: .tokens))
7878
let doc = try await infoRef.getDocument()
7979

8080
if doc.exists {
@@ -103,7 +103,7 @@ final class GithubAuthenticationService: NSObject, AuthenticationService {
103103
logger.info("Linking GitHub account for user: \(uid)")
104104

105105
do {
106-
let tokensRef = store.document("users/\(uid)/userData/tokens")
106+
let tokensRef = store.document(FirestorePath.userData(uid, document: .tokens))
107107
let authorizationCode = try await requestAuthorizationCode()
108108
let (accessToken, _) = try await requestTokens(authorizationCode: authorizationCode)
109109

@@ -138,7 +138,7 @@ final class GithubAuthenticationService: NSObject, AuthenticationService {
138138
logger.info("Starting GitHub access token revocation for unlink. uid: \(uid)")
139139
try await revokeAccessToken()
140140

141-
let tokensRef = store.document("users/\(uid)/userData/tokens")
141+
let tokensRef = store.document(FirestorePath.userData(uid, document: .tokens))
142142

143143
logger.info("Starting GitHub access token deletion from Firestore for unlink. uid: \(uid)")
144144
try await tokensRef.updateData(["githubAccessToken": FieldValue.delete()])

DevLog/Infra/Service/SocialLogin/GoogleAuthenticationService.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ final class GoogleAuthenticationService: AuthenticationService {
6161

6262
func signOut(_ uid: String) async throws {
6363
do {
64-
let infoRef = store.document("users/\(uid)/userData/tokens")
64+
let infoRef = store.document(FirestorePath.userData(uid, document: .tokens))
6565
let doc = try await infoRef.getDocument()
6666

6767
if doc.exists {

DevLog/Infra/Service/TodoService.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ final class TodoService {
158158
logger.info("Upserting todo")
159159

160160
do {
161-
let collection = store.collection("users/\(uid)/todoLists/")
161+
let collection = store.collection(FirestorePath.todos(uid))
162162
let docRef = collection.document(request.id)
163163
var data = try encoder.encode(request)
164164
data.removeValue(forKey: TodoFieldKey.id.rawValue)
@@ -174,7 +174,9 @@ final class TodoService {
174174
try await upsertTodoWithNumberOnCreate(
175175
data,
176176
for: docRef,
177-
counterRef: store.collection("users/\(uid)/counters/").document("todo")
177+
counterRef: store.document(
178+
FirestorePath.counter(uid, document: .todo)
179+
)
178180
)
179181

180182
logger.info("Successfully upserted todo")
@@ -222,7 +224,7 @@ final class TodoService {
222224
logger.info("Fetching todo")
223225

224226
do {
225-
let docRef = store.collection("users/\(uid)/todoLists/").document(todoId)
227+
let docRef = store.document(FirestorePath.todo(uid, todoId: todoId))
226228
let snapshot = try await docRef.getDocument()
227229
guard snapshot.exists, let todo = makeResponse(from: snapshot) else {
228230
throw FirestoreError.dataNotFound("Todo")
@@ -242,7 +244,7 @@ final class TodoService {
242244
let uniqueNumbers = Array(Set(numbers)).sorted()
243245
if uniqueNumbers.isEmpty { return [:] }
244246

245-
let collection = store.collection("users/\(uid)/todoLists/")
247+
let collection = store.collection(FirestorePath.todos(uid))
246248
let snapshots = try await withThrowingTaskGroup(of: [QueryDocumentSnapshot].self) { group in
247249
for chunk in uniqueNumbers.chunked(maxCount: 10) {
248250
group.addTask {
@@ -349,7 +351,7 @@ private extension TodoService {
349351
}
350352

351353
func makeQuery(uid: String, query: TodoQuery) -> Query {
352-
let collection = store.collection("users/\(uid)/todoLists/")
354+
let collection = store.collection(FirestorePath.todos(uid))
353355

354356
switch query.sortTarget {
355357
case .dueDate:

DevLog/Infra/Service/UserService.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ final class UserService {
2222
}
2323

2424
do {
25-
let userRef = store.document("users/\(user.uid)")
26-
let infoRef = store.document("users/\(user.uid)/userData/info")
27-
let tokensRef = store.document("users/\(user.uid)/userData/tokens")
28-
let settingsRef = store.document("users/\(user.uid)/userData/settings")
29-
let todoCounterRef = store.document("users/\(user.uid)/counters/todo")
25+
let userRef = store.document(FirestorePath.user(user.uid))
26+
let infoRef = store.document(FirestorePath.userData(user.uid, document: .info))
27+
let tokensRef = store.document(FirestorePath.userData(user.uid, document: .tokens))
28+
let settingsRef = store.document(FirestorePath.userData(user.uid, document: .settings))
29+
let todoCounterRef = store.document(FirestorePath.counter(user.uid, document: .todo))
3030

3131
// 사용자 기본 정보
3232
var userField: [String: Any] = [
@@ -115,7 +115,7 @@ final class UserService {
115115
}
116116

117117
do {
118-
let infoRef = store.document("users/\(uid)/userData/info")
118+
let infoRef = store.document(FirestorePath.userData(uid, document: .info))
119119
let data = try await infoRef.getDocument().data()
120120

121121
guard let provider = data?["currentProvider"] as? String,
@@ -149,7 +149,7 @@ final class UserService {
149149
}
150150

151151
do {
152-
let infoRef = store.document("users/\(uid)/userData/info")
152+
let infoRef = store.document(FirestorePath.userData(uid, document: .info))
153153
try await infoRef.setData(["statusMsg": message], merge: true)
154154
logger.info("Successfully upserted status message")
155155
} catch {
@@ -167,7 +167,7 @@ final class UserService {
167167
logger.info("Updating FCM token for user: \(userId)")
168168

169169
do {
170-
let tokensRef = store.document("users/\(userId)/userData/tokens")
170+
let tokensRef = store.document(FirestorePath.userData(userId, document: .tokens))
171171
try await tokensRef.setData(["fcmToken": fcmToken], merge: true)
172172
logger.info("Successfully updated FCM token")
173173
} catch {
@@ -185,7 +185,7 @@ final class UserService {
185185
logger.info("Updating timeZone for user: \(userId)")
186186

187187
do {
188-
let settingsRef = store.document("users/\(userId)/userData/settings")
188+
let settingsRef = store.document(FirestorePath.userData(userId, document: .settings))
189189
try await settingsRef.setData(
190190
["timeZone": TimeZone.autoupdatingCurrent.identifier],
191191
merge: true

DevLog/Infra/Service/WebPageService.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ final class WebPageService {
3030
}
3131

3232
do {
33-
let collectionRef = store.collection("users/\(uid)/webPages")
33+
let collectionRef = store.collection(FirestorePath.webPages(uid))
3434
let snapshot = try await collectionRef.getDocuments()
3535
let items: [WebPageResponse] = snapshot.documents.compactMap { makeResponse(from: $0) }
3636

@@ -63,7 +63,7 @@ final class WebPageService {
6363

6464
do {
6565
let documentID = documentID(for: request.url)
66-
let docRef = store.document("users/\(uid)/webPages/\(documentID)")
66+
let docRef = store.document(FirestorePath.webPage(uid, documentId: documentID))
6767
let data = try encoder.encode(request)
6868
try await docRef.setData(data, merge: true)
6969
logger.info("Successfully upserted web page")

0 commit comments

Comments
 (0)