Skip to content

Commit

Permalink
[Feat] Add associatedValue in FirestoreMethod.save case. Now directly…
Browse files Browse the repository at this point in the history
… specific document create is avaliable or not:)
  • Loading branch information
SHcommit committed Apr 26, 2024
1 parent 3075538 commit c0d8824
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 3 additions & 1 deletion Sources/SHFirestoreService/FirestoreMethod.swift
Original file line number Diff line number Diff line change
@@ -9,8 +9,10 @@ import Foundation
import FirebaseFirestore

@frozen public enum FirestoreMethod {
public typealias DocumentId = String
case get
case save
/// If you want to specify the document name directly, just enter the associate value.
case save(DocumentId?)
case delete
case update
case query
11 changes: 10 additions & 1 deletion Sources/SHFirestoreService/FirestoreService.swift
Original file line number Diff line number Diff line change
@@ -62,14 +62,23 @@ public final class FirestoreService: FirestoreServiceProtocol {
public func request(
endpoint: any FirestoreEndopintable
) -> AnyPublisher<Void, FirestoreServiceError> {
if case .save = endpoint.method {
if case .save(let documentId) = endpoint.method {
guard let requestDTO = endpoint.requestDTO else {
return Fail(error: FirestoreServiceError.invalidRequestDTO).eraseToAnyPublisher()
}
guard let collectionRef = endpoint.reference as? CollectionReference else {
return Fail(error: FirestoreServiceError.collectionNotFound).eraseToAnyPublisher()
}

///
if let documentId {
return collectionRef.document(documentId)
.setData(from: requestDTO)
.convertFirestoreServiceError()
.map { _ -> () in return }
.eraseToAnyPublisher()
}

return collectionRef.addDocument(from: requestDTO)
.convertFirestoreServiceError()
.map { _ -> () in return }

0 comments on commit c0d8824

Please sign in to comment.