Skip to content

Commit 463a908

Browse files
committed
[Feat] Add requestDTODictionary and apply to request and saveDocument
1 parent 5fea329 commit 463a908

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

Sources/SHFirestoreService/FirestoreEndopintable.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ import FirebaseFirestore
1111
public protocol FirestoreEndopintable {
1212
associatedtype ResponseDTO: Decodable
1313

14+
/// It is possible to update or save using Encodable form.
1415
var requestDTO: (any Encodable)? { get }
16+
17+
/// It is possible to update or save using dictionary form.
18+
/// E.g. FieldValue is not supported for encoding when encodable object's inner property's type is FieldValue.
19+
/// In this case, you should use this type.
20+
var requestDTODictionary: [String: Any]? { get }
1521

1622
/// Firestore's service accesses and works on a collection, a docuemnt or docuemnts based on the FirestoreMethod.
1723
/// So, you need to assign the desired request from FirestoreService to what is required by FirestoreMethod.

Sources/SHFirestoreService/FirestoreService.swift

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ extension FirestoreService: FirestoreServiceProtocol {
9494

9595
if case .update = endpoint.method {
9696
guard let requestDTO = endpoint.requestDTO else {
97+
if let requestDTODictionary = endpoint.requestDTODictionary {
98+
return documentRef
99+
.updateData(requestDTODictionary)
100+
.subscribe(on: backgroundQueue)
101+
.convertFirestoreServiceError()
102+
.eraseToAnyPublisher()
103+
}
97104
return Fail(error: FirestoreServiceError.invalidRequestDTO).eraseToAnyPublisher()
98105
}
99106
do {
@@ -113,9 +120,11 @@ extension FirestoreService: FirestoreServiceProtocol {
113120
/// Save a document using endpoint. And the ID of the created document A is returned.
114121
///
115122
/// Notes:
116-
/// 1. If requestDTO is nil, only the document is created without any fields.
123+
/// 1. If requestDTO and requestDTODictionary are nil, only the document is created without any fields.
117124
/// The same applies if you set the document ID or give the document ID through firesotre's automatic document.
118-
/// 2. If there is a response DTO at the endpoint,
125+
/// 2. If a respose dto dictionary is exist at the endpoint,
126+
/// It is saved in the document If documentId exists, the ID of the document is specified, otherwise it is automatically generated.
127+
/// 3. If there is a response DTO at the endpoint,
119128
/// fields and values ​​are formed through the key values ​​defined in CodingKeys through encode(to:) of the encodable.
120129
///
121130
/// Depending on whether there is a specific ID or not, a document with a specified documentId or an automatic ID is created.
@@ -130,7 +139,26 @@ extension FirestoreService: FirestoreServiceProtocol {
130139
return Fail(error: FirestoreServiceError.collectionNotFound).eraseToAnyPublisher()
131140
}
132141

133-
/// If request DTO is nil, it is assumed that only a document with no fields is created.
142+
if let requestDTODictionary = endpoint.requestDTODictionary {
143+
if let documentId {
144+
return collectionRef
145+
.document(documentId)
146+
.setData(requestDTODictionary)
147+
.subscribe(on: backgroundQueue)
148+
.convertFirestoreServiceError()
149+
.map { _ in return documentId }
150+
.eraseToAnyPublisher()
151+
} else {
152+
return collectionRef
153+
.addDocument(data: requestDTODictionary)
154+
.subscribe(on: backgroundQueue)
155+
.map { $0.documentID }
156+
.convertFirestoreServiceError()
157+
.eraseToAnyPublisher()
158+
}
159+
}
160+
161+
/// If request DTO and requestDTODictionary are nil it is assumed that only a document with no fields is created.
134162
guard let requestDTO = endpoint.requestDTO else {
135163
if let documentId {
136164
return collectionRef

0 commit comments

Comments
 (0)