Skip to content

Commit b16e857

Browse files
fix: get expense by id (#65)
1 parent 0670251 commit b16e857

File tree

2 files changed

+25
-20
lines changed

2 files changed

+25
-20
lines changed

GoMoney/Service/DataService.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,14 @@ class DataService {
160160
}
161161

162162
/// get Transaction by id.
163-
func getTransaction(by id: String, completion: ((Expense?) -> Void)? = nil) {
164-
let transaction = realm.objects(Expense.self)
165-
.first(where: { $0._id.stringValue == id })
166-
completion?(transaction)
163+
func getTransaction(by id: String) -> Expense? {
164+
guard let objectId = try? ObjectId(string: id) else {
165+
return nil
166+
}
167+
return realm.object(
168+
ofType: Expense.self,
169+
forPrimaryKey: objectId
170+
)
167171
}
168172

169173
func dropAllTable() {

GoMoney/Service/RemoteService.swift

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,23 @@ class RemoteService {
5454
return
5555
}
5656

57-
local.getTransaction(by: id) { transaction in
58-
guard let transaction = transaction else {
59-
print("Transaction \(id) not found at local.")
60-
return
61-
}
62-
do {
63-
try self.db.collection("transactions")
64-
.document(userId)
65-
.collection("transactions")
66-
.document(id)
67-
.setData(from: transaction)
68-
69-
completion(nil)
70-
} catch {
71-
completion(error)
72-
}
57+
let transaction = local.getTransaction(by: id)
58+
59+
guard let transaction = transaction else {
60+
print("Transaction \(id) not found at local.")
61+
return
62+
}
63+
64+
do {
65+
try db.collection("transactions")
66+
.document(userId)
67+
.collection("transactions")
68+
.document(id)
69+
.setData(from: transaction)
70+
71+
completion(nil)
72+
} catch {
73+
completion(error)
7374
}
7475
}
7576

0 commit comments

Comments
 (0)