Added method to update user data in the firestore provider
This commit is contained in:
parent
b009cb7462
commit
79a5196aff
1 changed files with 33 additions and 0 deletions
|
|
@ -5,6 +5,7 @@ import 'package:rxdart/rxdart.dart';
|
||||||
|
|
||||||
import '../models/event_model.dart';
|
import '../models/event_model.dart';
|
||||||
import '../models/user_model.dart';
|
import '../models/user_model.dart';
|
||||||
|
import '../models/summary_model.dart';
|
||||||
import '../models/task_model.dart';
|
import '../models/task_model.dart';
|
||||||
|
|
||||||
/// A connection to the Cloud Firestore database
|
/// A connection to the Cloud Firestore database
|
||||||
|
|
@ -41,6 +42,7 @@ class FirestoreProvider {
|
||||||
return Observable(mappedStream);
|
return Observable(mappedStream);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Creates a new instance of a user in Firestore.
|
||||||
Future<void> createUser(UserModel user) async {
|
Future<void> createUser(UserModel user) async {
|
||||||
try {
|
try {
|
||||||
final dataMap = user.toFirestoreMap();
|
final dataMap = user.toFirestoreMap();
|
||||||
|
|
@ -50,6 +52,7 @@ class FirestoreProvider {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Verifies if a user with the given username is already in the database.
|
||||||
Future<bool> userExists(String username) async {
|
Future<bool> userExists(String username) async {
|
||||||
final querySnapshot = await _firestore
|
final querySnapshot = await _firestore
|
||||||
.collection('users')
|
.collection('users')
|
||||||
|
|
@ -58,6 +61,36 @@ class FirestoreProvider {
|
||||||
return querySnapshot.documents.length > 0;
|
return querySnapshot.documents.length > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> updateUser(
|
||||||
|
String id, {
|
||||||
|
List<String> tasks,
|
||||||
|
SummaryModel summary,
|
||||||
|
int pendingHigh,
|
||||||
|
int pendingMedium,
|
||||||
|
int pendingLow,
|
||||||
|
}) async {
|
||||||
|
final newData = <String, dynamic>{
|
||||||
|
'id': id,
|
||||||
|
'tasks': tasks,
|
||||||
|
'summary': summary,
|
||||||
|
'pendingHigh': pendingHigh,
|
||||||
|
'pendingMedium': pendingMedium,
|
||||||
|
'pendingLow': pendingLow,
|
||||||
|
};
|
||||||
|
newData.removeWhere((key, value) => value == null);
|
||||||
|
|
||||||
|
if (newData.isEmpty) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
final documentReference = _firestore.collection('users').document(id);
|
||||||
|
await documentReference.setData(newData, merge: true);
|
||||||
|
} catch (e) {
|
||||||
|
print('Error trying to update user data: $e');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//-------------------------Task related operations----------------------------
|
//-------------------------Task related operations----------------------------
|
||||||
|
|
||||||
/// Adds a task to firestore.
|
/// Adds a task to firestore.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue