Skip to content

Commit

Permalink
[Feat] Add logic to return an empty string in FirestoreService.reques…
Browse files Browse the repository at this point in the history
…t(endpoint:) if the document in the collection is empty.
  • Loading branch information
SHcommit committed May 1, 2024
1 parent 7092d2a commit 15901ee
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions Sources/SHFirestoreService/FirestoreService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public final class FirestoreService: FirestoreServiceProtocol {

/// Reqeust responseDTOs from endpoint's specific CollectionReference
/// when endpoint's **FirestoreMethod** is get type.
/// If specific collection has no any document, it return empty array.
public func request<D, E>(
endpoint: E
) -> AnyPublisher<[D], FirestoreServiceError>
Expand All @@ -29,7 +30,10 @@ public final class FirestoreService: FirestoreServiceProtocol {
if case .get = endpoint.method {
return collectionRef.getDocuments()
.tryMap { snapshots in
try snapshots.documents.map { snapshot in
if snapshots.isEmpty {
return []
}
return try snapshots.documents.map { snapshot in
try snapshot.data(as: D.self)
}
}
Expand Down Expand Up @@ -73,7 +77,7 @@ public final class FirestoreService: FirestoreServiceProtocol {
return Fail(error: FirestoreServiceError.collectionNotFound).eraseToAnyPublisher()
}

///
/// If FirestoreMethod save's associated value is exist, create a document with the document ID and save the requestDTO.
if let documentId {
return collectionRef.document(documentId)
.setData(from: requestDTO)
Expand Down

0 comments on commit 15901ee

Please sign in to comment.