Implemented the rest of CRUD operations for the task model
This commit is contained in:
parent
f164a2a8f7
commit
c95939f5df
2 changed files with 83 additions and 24 deletions
|
|
@ -5,7 +5,7 @@ import './models/task_model.dart';
|
|||
import './models/user_model.dart';
|
||||
import './resources/firestore_provider.dart';
|
||||
|
||||
class App extends StatelessWidget {
|
||||
/* class App extends StatelessWidget {
|
||||
Widget build(BuildContext context) {
|
||||
final fire = FirestoreProvider();
|
||||
return MaterialApp(
|
||||
|
|
@ -19,9 +19,8 @@ class App extends StatelessWidget {
|
|||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
} */
|
||||
|
||||
/*
|
||||
class App extends StatelessWidget {
|
||||
Widget build(BuildContext context) {
|
||||
final fire = FirestoreProvider();
|
||||
|
|
@ -43,7 +42,22 @@ class App extends StatelessWidget {
|
|||
}
|
||||
final children = <Widget>[
|
||||
MaterialButton(
|
||||
onPressed: () {},
|
||||
onPressed: () {
|
||||
final task = TaskModel(
|
||||
ownerUsername: 'mariano159357',
|
||||
text: 'I dont know what to put',
|
||||
priority: 2,
|
||||
done: false,
|
||||
event: 'Math',
|
||||
);
|
||||
|
||||
fire.updateTask(
|
||||
'-LZRNhS9mX-SO0XgfQIM',
|
||||
done: true,
|
||||
text: 'Hellloooooo',
|
||||
priority: 1,
|
||||
);
|
||||
},
|
||||
child: Text('Add task'),
|
||||
),
|
||||
];
|
||||
|
|
@ -62,4 +76,3 @@ class App extends StatelessWidget {
|
|||
);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -5,6 +5,12 @@ import '../models/event_model.dart';
|
|||
import '../models/user_model.dart';
|
||||
import '../models/task_model.dart';
|
||||
|
||||
/**
|
||||
* TODO: the cloud firestore plugin currently throws an error when calling
|
||||
* methods that modify documents. Wait for a fix.
|
||||
* https://github.com/flutter/flutter/issues/28103
|
||||
*/
|
||||
|
||||
/// A connection to the Cloud Firestore database
|
||||
///
|
||||
/// Implempents CRUD operations for users, tasks and events.
|
||||
|
|
@ -14,6 +20,7 @@ class FirestoreProvider {
|
|||
FirestoreProvider() {
|
||||
firestore.settings(timestampsInSnapshotsEnabled: true);
|
||||
}
|
||||
//-----------------------User related operations------------------------------
|
||||
|
||||
/// Returns a stream of [UserModel].
|
||||
Observable<UserModel> getUser(String username) {
|
||||
|
|
@ -38,13 +45,66 @@ class FirestoreProvider {
|
|||
|
||||
//-------------------------Task related operations----------------------------
|
||||
|
||||
/// Adds a task to the tasks collection in firestore.
|
||||
/// Adds a task to firestore.
|
||||
Future<void> addTask(TaskModel task) async {
|
||||
final dataMap = task.toFirestoreMap();
|
||||
await firestore.collection('tasks').add(dataMap);
|
||||
try {
|
||||
final dataMap = task.toFirestoreMap();
|
||||
await firestore.collection('tasks').add(dataMap);
|
||||
} catch (e) {
|
||||
print('Error adding task to firestore: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a stream of [List<Task>]
|
||||
/// Returns a Stream of a single task from an id.
|
||||
Observable<TaskModel> getTask(String id) {
|
||||
final mappedStream =
|
||||
firestore.collection('tasks').document(id).snapshots().map(
|
||||
(DocumentSnapshot snapshot) {
|
||||
return TaskModel.fromFirestore(
|
||||
snapshot.data,
|
||||
id: snapshot.documentID,
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
return Observable(mappedStream);
|
||||
}
|
||||
|
||||
/// Deletes a task from firestore.
|
||||
Future<void> deleteTask(String id) async {
|
||||
try {
|
||||
final documentReference = firestore.collection('tasks').document(id);
|
||||
await documentReference.delete();
|
||||
} catch (e) {
|
||||
print('Error deleting task from firestore: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Updates a task in firestore.
|
||||
///
|
||||
/// Only the [text], [priority] and [done] attributes can be update.
|
||||
/// Provide at least one of these values.
|
||||
Future<void> updateTask(
|
||||
String id, {
|
||||
String text,
|
||||
int priority,
|
||||
bool done,
|
||||
}) async {
|
||||
final newData = <String, dynamic>{
|
||||
'text': text,
|
||||
'priority': priority,
|
||||
'done': done,
|
||||
};
|
||||
newData.removeWhere((key, value) => value == null);
|
||||
try {
|
||||
final documentReference = firestore.collection('tasks').document(id);
|
||||
await documentReference.setData(newData, merge: true);
|
||||
} catch (e) {
|
||||
print('Error updating task in firestore: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a stream of [List<Task>] that correspond to a particular user.
|
||||
///
|
||||
/// The [event] parameter is used to query tasks that are part of a certain
|
||||
/// event.
|
||||
|
|
@ -74,23 +134,9 @@ class FirestoreProvider {
|
|||
return Observable(mappedStream);
|
||||
}
|
||||
|
||||
Observable<TaskModel> getTask(String id) {
|
||||
final mappedStream =
|
||||
firestore.collection('tasks').document(id).snapshots().map(
|
||||
(DocumentSnapshot snapshot) {
|
||||
return TaskModel.fromFirestore(
|
||||
snapshot.data,
|
||||
id: snapshot.documentID,
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
return Observable(mappedStream);
|
||||
}
|
||||
|
||||
//-----------------------Event related operations-----------------------------
|
||||
|
||||
// TODO: Change the Events collction name to 'events'
|
||||
// TODO: Change the Events collction name to 'events' in forestore.
|
||||
Observable<List<EventModel>> getUserEvents(String userDocumentId) {
|
||||
final mappedStream = firestore
|
||||
.collection('users')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue