-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFirestoreService.swift
327 lines (274 loc) · 12 KB
/
FirestoreService.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
//
// FirestoreService.swift
// Firebase
//
// Created by xqsadness on 11/01/23.
//
import Foundation
import FirebaseFirestore
import FirebaseStorage
protocol FirestoreObject {
func toDictionary() -> [String: Any]
var id: String { get }
}
/**
Adds an object of a specific type conforming to `FirestoreObject` to the Firestore database.
- Parameters:
- object: The object to be added to Firestore.
- collectionName: The collection name where the object will be stored in Firestore.
**/
func writeObjectToFirestore<T : FirestoreObject>(object: T, collectionName: NameObject, completion: @escaping (Error?) -> Void) {
let db = Firestore.firestore()
db.collection(collectionName.rawValue).document(object.id).setData(object.toDictionary()) { error in
completion(error)
}
}
/**
Deletes an object of a specific type conforming to `FirestoreObject` from the Firestore database.
- Parameters:
- object: The object to be deleted from Firestore.
- collectionName: The collection name from which the object will be deleted in Firestore.
- completion: A closure that is called after the deletion operation is completed, providing an optional error if an error occurred during the operation.
*/
func deleteObjectFromFirestore<T: FirestoreObject>(object: T, collectionName: NameObject, completion: @escaping (Error?) -> Void) {
let db = Firestore.firestore()
db.collection(collectionName.rawValue).document(object.id).delete { error in
completion(error)
}
}
/**
Deletes multiple objects of a specific type conforming to `FirestoreObject` from the Firestore database in a single batch operation.
- Parameters:
- objects: An array of objects to be deleted from Firestore.
- collectionName: The collection name from which the objects will be deleted in Firestore.
- completion: A closure that is called after the deletion operation is completed, providing an optional error if an error occurred during the operation.
*/
func deleteMultipleObjectsFromFirestore<T: FirestoreObject>(objects: [T], collectionName: NameObject, completion: @escaping (Error?) -> Void) {
let db = Firestore.firestore()
let batch = db.batch()
for object in objects {
let documentRef = db.collection(collectionName.rawValue).document(object.id)
batch.deleteDocument(documentRef)
}
// Commit the batch operation to delete multiple objects
batch.commit { error in
completion(error)
}
}
/**
Deletes an image from Firebase Storage.
- Parameters:
- imageURL: The name or URL of the image file to be deleted.
- completion: A closure that will be called when the image deletion is complete, with an optional `Error` parameter indicating any errors that occurred during the deletion. If the deletion is successful, the `Error` parameter will be `nil`.
*/
func deleteImageFromStorage(imageURL: String, completion: @escaping (Error?) -> Void) {
// Create a reference to the Firebase Storage
let storage = Storage.storage()
// Get a reference to the image file based on the imageURL
let imageRef = storage.reference(forURL: imageURL)
// Delete the image file
imageRef.delete { error in
if let error = error {
// An error occurred while deleting the image
completion(error)
} else {
// Image deleted successfully
completion(nil)
}
}
}
/**
Fetches all objects from a Firestore collection of a specified`.
- Parameters:
- collectionName: The collection name from which the objects will be fetched in Firestore.
- completion: A closure that is called after the fetch operation is completed, providing a `QuerySnapshot` containing the fetched documents and an optional error if an error occurred during the operation.
*/
func fetchAllObjects(collectionName: NameObject, completion: @escaping (QuerySnapshot?, Error?) -> Void) {
let db = Firestore.firestore()
db.collection(collectionName.rawValue).getDocuments() { (querySnapshot, error) in
completion(querySnapshot, error)
}
}
/**
Fetches a single document from a Firestore collection based on its `id`.
- Parameters:
- collectionName: The collection name from which the objects will be fetched in Firestore.
- id: The unique identifier (id) of the document to fetch.
- completion: A closure that is called when the document is successfully fetched or if an error occurs.
- documentSnapshot: A `DocumentSnapshot` containing the fetched document, if successful; otherwise, `nil`.
- error: An `Error` object describing any errors that occurred during the operation, if any.
*/
func fetchSingleObject(collectionName: NameObject, id: String, completion: @escaping (DocumentSnapshot?, Error?) -> Void) {
let db = Firestore.firestore()
let documentReference = db.collection(collectionName.rawValue).document(id)
documentReference.getDocument { (documentSnapshot, error) in
completion(documentSnapshot, error)
}
}
/**
Fetches objects from a Firestore collection based on specified conditions.
- Parameters:
- collectionName: The collection name from which the objects will be fetched in Firestore.
- conditions: An array of tuples, each containing the field name, operator, and value for a query condition.
- completion: A closure that is called after the fetch operation is completed, providing a `QuerySnapshot` containing the fetched documents and an optional error if an error occurred during the operation.
- Use conditions like this
let conditions: [(fieldName: String, `operator`: Operator, value: Any)] = [
("name", .isEqualTo, "C")
]
*/
func fetchObjectsWithConditions(collectionName: NameObject, conditions: [(fieldName: String, `operator`: Operator, value: Any)], completion: @escaping (QuerySnapshot?, Error?) -> Void) {
let db = Firestore.firestore()
let collectionRef = db.collection(collectionName.rawValue)
var query: Query = collectionRef
for condition in conditions {
let (fieldName, `operator`, value) = condition
// If `operator` is `.none`, then the `query.whereField` operation is not performed
if `operator` != .none{
switch `operator` {
case .isEqualTo:
query = query.whereField(fieldName, isEqualTo: value)
case .isLessThan:
query = query.whereField(fieldName, isLessThan: value)
case .isGreaterThan:
query = query.whereField(fieldName, isGreaterThan: value)
case .arrayContains:
query = query.whereField(fieldName, arrayContains: value)
case .isLessThanOrEqualTo:
query = query.whereField(fieldName, isLessThanOrEqualTo: value)
case .isGreaterThanOrEqualTo:
query = query.whereField(fieldName, isGreaterThanOrEqualTo: value)
case .isNotEqualTo:
query = query.whereField(fieldName, isNotEqualTo: value)
case .none:
break
}
}
}
query.getDocuments() { (querySnapshot, error) in
completion(querySnapshot, error)
}
}
/**
Enum defining names of Firestore collections to be used for different types of objects. Each case corresponds to a specific Firestore collection name.
*/
enum NameObject: String{
case product = "products"
case coupon = "coupons"
case order = "orders"
case payment = "payments"
case cart = "carts"
case address = "address"
case category = "categorys"
case discount = "discounts"
case productOption = "productOptions"
case user = "users"
case notification = "notification"
}
// Add more conditions as needed
enum Operator: String{
case none = ""
case isEqualTo = "isEqualTo"
case isLessThan = "isLessThan"
case isGreaterThan = "isGreaterThan"
case arrayContains = "arrayContains"
case isLessThanOrEqualTo = "isLessThanOrEqualTo"
case isGreaterThanOrEqualTo = "isGreaterThanOrEqualTo"
case isNotEqualTo = "isNotEqualTo"
}
/*
Usage example for fetching objects from a Firestore collection and handling the fetched data.
Example usage includes fetching a list of `CategoryModel` objects from the Firestore `categorys` collection.
- Note: In this example, I define a `CategoryModel` conforming to the `FirestoreObject` protocol and implement the required functions `toDictionary` and `id`.
- Note: The function `fetchAllCategory` is used to fetch all documents from the Firestore `categorys` collection, convert them into `CategoryModel` objects, and update the `dataCategorys` property, which can be used to display the data in the user interface.
Example usage:
-- Model
struct CategoryModel: FirestoreObject{
var categoryId: String = ""
var name: String = ""
var thumbnailCategory: String = ""
var id:String{
return categoryId
}
func toDictionary() -> [String: Any] {
let data: [String: Any] = [
"categoryId" : self.categoryId,
"name": self.name,
"thumbnailCategory": self.thumbnailCategory,
]
return data
}
}
-- VM
struct CategoryModel: FirestoreObject{
var categoryId: String = ""
var name: String = ""
var thumbnailCategory: String = ""
var id:String{
//return -> categoryId
return categoryId
}
func toDictionary() -> [String: Any] {
let data: [String: Any] = [
"categoryId" : self.categoryId,
"name": self.name,
"thumbnailCategory": self.thumbnailCategory,
]
return data
}
}
-- fetch
func fetchAllCategory() {
fetchAllObjects(collectionName: .category) { querySnapshot, error in
if let error = error {
print("Error getting documents: \(error)")
} else {
var data: [CategoryModel] = []
for document in querySnapshot!.documents {
let categoryData = document.data()
if let category = parseCategory(from: categoryData) {
data.append(category)
}
}
withAnimation {
self.dataCategorys = data
}
}
}
}
func parseCategory(from categoryData: [String: Any]) -> CategoryModel? {
guard let name = categoryData["name"] as? String ,
let categoryId = categoryData["categoryId"] as? String,
let thumbnailCategory = categoryData["thumbnailCategory"] as? String else {
return nil
}
return CategoryModel(categoryId: categoryId, name: name, thumbnailCategory: thumbnailCategory)
}
-- Write
func addCategory() {
let categoryId = UUID().uuidString
let storageRef = Storage.storage().reference().child("Category_Images").child(categoryId)
guard let thumbnailCategory = thumbnailCategory else {
LocalNotification.shared.message("Default profile image not found", .warning)
return
}
// convert type image to png
let metaData = StorageMetadata()
metaData.contentType = "image/png"
// Convert the image into JPEG and compress the quality to reduce its size
let data = thumbnailCategory.jpegData(compressionQuality: 0.2)
// Upload the image
if let data = data {
storageRef.putData(data, metadata: metaData) { (metadata, error) in
if let error = error {
LocalNotification.shared.message("Error while uploading file: \(error.localizedDescription)", .error)
}else{
Task{
let urlCategory = try await storageRef.downloadURL()
self.updateCategoryWithImage(categoryId: categoryId, urlImage: urlCategory.absoluteString)
//add category with downloadURL from storage and fetch category
}
}
}
}
}
*/