Skip to content

Commit de8b873

Browse files
committed
[Style] Change func naming from isFieldDuplicated to isDocumentExists
1 parent 621a7a1 commit de8b873

File tree

3 files changed

+66
-56
lines changed

3 files changed

+66
-56
lines changed

Sources/SHFirestoreService/FirestoreDocumentSupportable.swift

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,32 @@
88
import Combine
99
import Foundation
1010

11+
#if os(iOS) && canImport(FirebaseFirestore)
12+
import FirebaseFirestore
1113
public protocol FirestoreDocumentSupportable {
12-
func isDocumentExists(endpoint: any FirestoreEndopintable) -> AnyPublisher<Bool, FirestoreServiceError>
14+
15+
/// When implementing the endpoint's document instance to be returned from the request Type, it is returned whether the document exists.
16+
func isDocumentExists(
17+
endpoint: any FirestoreEndopintable
18+
) -> AnyPublisher<Bool, FirestoreServiceError>
19+
20+
/// Checks if a document is duplicated or not from the endpoint requestType is a collectionRef using firestore's query.
21+
///
22+
/// Notes:
23+
/// 1. This method executes a Firestore query to check if the specified field in the document is duplicated from a collection reference.
24+
/// 2. endpoint's a documentRef computed property of requestType property should return nil.
25+
/// 3. The query is executed using the provided endpoint and query handler.
26+
/// 4. The requestType of endpoint is not used.
27+
///
28+
/// - Parameters:
29+
/// - endpoint: The Firestore endpoint to query, which must conform to `FirestoreEndopintable`.
30+
/// - query: The Firestore query handler used to execute the query.
31+
/// The query should be designed to check for field duplication.
32+
/// - Returns: A publisher that emits a `Bool` indicating whether the field is duplicated (`true`) or not (`false`).
33+
/// - Throws: `FirestoreServiceError` if the query execution fails.
34+
func isDocumentExists(
35+
endpoint: any FirestoreEndopintable,
36+
makeQuery: @escaping FirestoreQueryable.FirestoreQueryHandler
37+
) -> AnyPublisher<Bool, FirestoreServiceError>
1338
}
39+
#endif

Sources/SHFirestoreService/FirestoreQueryable.swift

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,5 @@ public protocol FirestoreQueryable {
3232
where D: Decodable,
3333
E: FirestoreEndopintable,
3434
D == E.ResponseDTO
35-
36-
/// Checks if a document is duplicated or not from the endpoint requestType is a collectionRef using firestore's query.
37-
///
38-
/// Notes:
39-
/// 1. This method executes a Firestore query to check if the specified field in the document is duplicated from a collection reference.
40-
/// 2. endpoint's a documentRef computed property of requestType property should return nil.
41-
/// 3. The query is executed using the provided endpoint and query handler.
42-
/// 4. The requestType of endpoint is not used.
43-
///
44-
/// - Parameters:
45-
/// - endpoint: The Firestore endpoint to query, which must conform to `FirestoreEndopintable`.
46-
/// - query: The Firestore query handler used to execute the query.
47-
/// The query should be designed to check for field duplication.
48-
/// - Returns: A publisher that emits a `Bool` indicating whether the field is duplicated (`true`) or not (`false`).
49-
/// - Throws: `FirestoreServiceError` if the query execution fails.
50-
func isFieldDuplicated(
51-
endpoint: any FirestoreEndopintable,
52-
makeQuery: @escaping FirestoreQueryHandler
53-
) -> AnyPublisher<Bool, FirestoreServiceError>
5435
}
5536
#endif

Sources/SHFirestoreService/FirestoreService.swift

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,44 @@ extension FirestoreService: FirestoreQueryable {
371371
}
372372
}.eraseToAnyPublisher()
373373
}
374+
}
375+
376+
// MARK: - FirestoreTransactional
377+
extension FirestoreService: FirestoreTransactional {
378+
/// Notes:
379+
/// 1. Executes a transaction and emits FiresotreServiceError if execution fails.
380+
public func performTransaction(
381+
_ updateBlock: @escaping (Transaction) throws -> Any?
382+
) -> AnyPublisher<Any?, any Error> {
383+
return Firestore.firestore()
384+
.runTransaction(updateBlock)
385+
.subscribeAndReceive(on: backgroundQueue)
386+
.mapError { error in
387+
return FirestoreServiceError.failedTransaction(error)
388+
}.eraseToAnyPublisher()
389+
}
390+
}
391+
392+
// MARK: - FirestoreDocumentSupportable
393+
extension FirestoreService: FirestoreDocumentSupportable {
394+
/// Determine whether a document exists
395+
///
396+
/// Notes:
397+
/// 1. Do not need to specify FirestoreMethod's specific type when using this function.
398+
/// 2. Set up requestType of FirestoreAccessible in endpoint to access firestore's a specific document before call this function.
399+
/// 3. When a document path is provided from requestType, this function determines whether the document exists by querying the snapshot.
400+
public func isDocumentExists(
401+
endpoint: any FirestoreEndopintable
402+
) -> AnyPublisher<Bool, FirestoreServiceError> {
403+
guard let documentRef = endpoint.reference.asDocumentRef else {
404+
return Fail(error: FirestoreServiceError.documentNotFound).eraseToAnyPublisher()
405+
}
406+
return documentRef
407+
.getDocument()
408+
.convertFirestoreServiceError()
409+
.map { snapshot -> Bool in return snapshot.exists }.eraseToAnyPublisher()
410+
.eraseToAnyPublisher()
411+
}
374412

375413
/// Checks if a document is duplicated or not from the endpoint requestType is a collectionRef using firestore's query.
376414
///
@@ -400,7 +438,7 @@ extension FirestoreService: FirestoreQueryable {
400438
/// // TODO: - You can check whether the query has any matching documents in this downstream
401439
/// }
402440
/// ```
403-
public func isFieldDuplicated(
441+
public func isDocumentExists(
404442
endpoint: any FirestoreEndopintable,
405443
makeQuery: @escaping FirestoreQueryHandler
406444
) -> AnyPublisher<Bool, FirestoreServiceError> {
@@ -428,39 +466,4 @@ extension FirestoreService: FirestoreQueryable {
428466
}.eraseToAnyPublisher()
429467
}
430468
}
431-
432-
// MARK: - FirestoreTransactional
433-
extension FirestoreService: FirestoreTransactional {
434-
/// Notes:
435-
/// 1. Executes a transaction and emits FiresotreServiceError if execution fails.
436-
public func performTransaction(
437-
_ updateBlock: @escaping (Transaction) throws -> Any?
438-
) -> AnyPublisher<Any?, any Error> {
439-
return Firestore.firestore()
440-
.runTransaction(updateBlock)
441-
.subscribeAndReceive(on: backgroundQueue)
442-
.mapError { error in
443-
return FirestoreServiceError.failedTransaction(error)
444-
}.eraseToAnyPublisher()
445-
}
446-
}
447-
448-
// MARK: - FirestoreDocumentSupportable
449-
extension FirestoreService: FirestoreDocumentSupportable {
450-
/// Determine whether a document exists
451-
///
452-
/// Notes:
453-
/// 1. Do not need to specify FirestoreMethod's specific type when using this function.
454-
/// 2. Set up requestType of FirestoreAccessible in endpoint to access firestore's a specific document before call this function.
455-
public func isDocumentExists(endpoint: any FirestoreEndopintable) -> AnyPublisher<Bool, FirestoreServiceError> {
456-
guard let documentRef = endpoint.reference.asDocumentRef else {
457-
return Fail(error: FirestoreServiceError.documentNotFound).eraseToAnyPublisher()
458-
}
459-
return documentRef
460-
.getDocument()
461-
.convertFirestoreServiceError()
462-
.map { snapshot -> Bool in return snapshot.exists }.eraseToAnyPublisher()
463-
.eraseToAnyPublisher()
464-
}
465-
}
466469
#endif

0 commit comments

Comments
 (0)