Skip to content

Commit 615216e

Browse files
committed
feat(DB): implement function to update user saved place address
1 parent abad32f commit 615216e

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

lib/utilities/firebase_calls.dart

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import 'package:firebase_auth/firebase_auth.dart';
2+
import 'package:cloud_firestore/cloud_firestore.dart';
3+
4+
FirebaseAuth auth = FirebaseAuth.instance;
5+
CollectionReference usersCollection =
6+
FirebaseFirestore.instance.collection('users');
7+
CollectionReference congestionCollection =
8+
FirebaseFirestore.instance.collection('congestions');
9+
bool newUser = true;
10+
11+
class FirebaseCalls {
12+
Future<void> updateUser(String username, List<Map<String, dynamic>> addresses) async {
13+
// Fetch the currently authenticated user
14+
User? currentUser = auth.currentUser;
15+
16+
// Ensure that the user is logged in
17+
if (currentUser == null) {
18+
throw Exception("No user is logged in.");
19+
}
20+
21+
// Check if there is an existing record of user
22+
QuerySnapshot querySnap = await usersCollection.where('userid', isEqualTo: currentUser.uid).get();
23+
24+
if (querySnap.docs.isNotEmpty) {
25+
// Existing user
26+
QueryDocumentSnapshot doc = querySnap.docs[0];
27+
await doc.reference.update({
28+
'addresses': addresses,
29+
});
30+
} else {
31+
// New user
32+
await usersCollection.add({
33+
'addresses': addresses,
34+
'username': username,
35+
'userid': currentUser.uid,
36+
});
37+
}
38+
}
39+
}
40+

0 commit comments

Comments
 (0)