-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
90aac1b
commit 0af6414
Showing
11 changed files
with
208 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// | ||
// ExpenseStore.swift | ||
// Data | ||
// | ||
// Created by Amisha Italiya on 29/03/24. | ||
// | ||
|
||
import Combine | ||
import FirebaseFirestoreInternal | ||
|
||
public class ExpenseStore: ObservableObject { | ||
|
||
@Inject private var database: Firestore | ||
|
||
private let DATABASE_NAME: String = "expenses" | ||
|
||
public func addExpense(expense: Expense, completion: @escaping (String?) -> Void) { | ||
do { | ||
let code = try database.collection(DATABASE_NAME).addDocument(from: expense) | ||
completion(code.documentID) | ||
return | ||
} catch { | ||
LogE("ExpenseRepository :: \(#function) error: \(error.localizedDescription)") | ||
} | ||
completion(nil) | ||
} | ||
|
||
public func fetchExpensesBy(groupId: String) -> AnyPublisher<[Expense], ServiceError> { | ||
return Future { [weak self] promise in | ||
guard let self = self else { | ||
promise(.failure(.unexpectedError)) | ||
return | ||
} | ||
|
||
self.database.collection(DATABASE_NAME).whereField("group_id", isEqualTo: groupId).getDocuments { snapshot, error in | ||
if let error = error { | ||
LogE("ExpenseRepository :: \(#function) error: \(error.localizedDescription)") | ||
promise(.failure(.networkError)) | ||
return | ||
} | ||
|
||
guard let snapshot, !snapshot.documents.isEmpty else { | ||
LogD("ExpenseRepository :: \(#function) The document is not available.") | ||
promise(.success([])) | ||
return | ||
} | ||
|
||
do { | ||
let expenses = try snapshot.documents.compactMap { document in | ||
try document.data(as: Expense.self) | ||
} | ||
promise(.success(expenses)) | ||
} catch { | ||
LogE("ExpenseRepository :: \(#function) Decode error: \(error.localizedDescription)") | ||
promise(.failure(.decodingError)) | ||
} | ||
} | ||
} | ||
.eraseToAnyPublisher() | ||
} | ||
|
||
public func deleteExpense(id: String) -> AnyPublisher<Void, ServiceError> { | ||
Future { [weak self] promise in | ||
guard let self else { | ||
promise(.failure(.unexpectedError)) | ||
return | ||
} | ||
|
||
self.database.collection(self.DATABASE_NAME).document(id).delete { error in | ||
if let error { | ||
LogE("ExpenseRepository :: \(#function): Deleting collection failed with error: \(error.localizedDescription).") | ||
promise(.failure(.databaseError)) | ||
} else { | ||
LogD("ExpenseRepository :: \(#function): expense deleted successfully.") | ||
promise(.success(())) | ||
} | ||
} | ||
}.eraseToAnyPublisher() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.