Added the remaining CRUD operations for events
This commit is contained in:
parent
e7fa1e7afa
commit
b86d88785c
3 changed files with 112 additions and 16 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,9 @@ import './resources/firestore_provider.dart';
|
|||
),
|
||||
);
|
||||
}
|
||||
} */
|
||||
}
|
||||
|
||||
class App extends StatelessWidget {
|
||||
/* class App extends StatelessWidget {
|
||||
Widget build(BuildContext context) {
|
||||
final fire = FirestoreProvider();
|
||||
return MaterialApp(
|
||||
|
|
@ -32,9 +32,9 @@ class App extends StatelessWidget {
|
|||
title: Text('DO>'),
|
||||
),
|
||||
body: StreamBuilder(
|
||||
stream: fire.getUserEvents('vBOvtmTeC8iPg8L4Hixh'),
|
||||
builder: (BuildContext context,
|
||||
AsyncSnapshot<List<EventModel>> userSnapshot) {
|
||||
stream: fire.getEvent('vBOvtmTeC8iPg8L4Hixh', '-LZReccofbHpw9UfOTMk'),
|
||||
builder:
|
||||
(BuildContext context, AsyncSnapshot<EventModel> userSnapshot) {
|
||||
if (!userSnapshot.hasData) {
|
||||
return Center(
|
||||
child: CircularProgressIndicator(),
|
||||
|
|
@ -51,11 +51,21 @@ class App extends StatelessWidget {
|
|||
event: 'Math',
|
||||
);
|
||||
|
||||
fire.updateTask(
|
||||
'-LZRNhS9mX-SO0XgfQIM',
|
||||
done: true,
|
||||
text: 'Hellloooooo',
|
||||
priority: 1,
|
||||
final event = EventModel(
|
||||
name: 'Langs and trans',
|
||||
tasks: <String>[],
|
||||
highPriority: 0,
|
||||
mediumPriority: 0,
|
||||
lowPriority: 0,
|
||||
media: <String>[],
|
||||
when: <bool>[],
|
||||
pendigTasks: 0,
|
||||
);
|
||||
|
||||
fire.updateEvent(
|
||||
'vBOvtmTeC8iPg8L4Hixh',
|
||||
'-LZReccofbHpw9UfOTMk',
|
||||
name: 'kjhahslkdjhf',
|
||||
);
|
||||
},
|
||||
child: Text('Add task'),
|
||||
|
|
@ -63,9 +73,11 @@ class App extends StatelessWidget {
|
|||
];
|
||||
//children.add(Text(userSnapshot.data.text));
|
||||
|
||||
userSnapshot.data.forEach((EventModel task) {
|
||||
children.add(Text(task.name));
|
||||
});
|
||||
//userSnapshot.data.forEach((EventModel task) {
|
||||
// children.add(Text(task.name));
|
||||
//});
|
||||
|
||||
children.add(Text(userSnapshot.data.name));
|
||||
|
||||
return Column(
|
||||
children: children,
|
||||
|
|
@ -75,4 +87,4 @@ class App extends StatelessWidget {
|
|||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
} */
|
||||
|
|
|
|||
|
|
@ -33,4 +33,17 @@ class EventModel {
|
|||
highPriority = firestoreMap["highPriority"],
|
||||
mediumPriority = firestoreMap["mediumPriority"],
|
||||
lowPriority = firestoreMap["lowPriority"];
|
||||
|
||||
Map<String, dynamic> toFirestoreMap() {
|
||||
return <String, dynamic>{
|
||||
"name": name,
|
||||
"pendingTasks": pendigTasks,
|
||||
"when": when,
|
||||
"media": media,
|
||||
"tasks": tasks,
|
||||
"highPriority": highPriority,
|
||||
"mediumPriority": mediumPriority,
|
||||
"lowPriority": lowPriority,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -136,8 +136,79 @@ class FirestoreProvider {
|
|||
}
|
||||
|
||||
//-----------------------Event related operations-----------------------------
|
||||
// TODO: Change the Events collection name to 'events' in forestore.
|
||||
|
||||
// TODO: Change the Events collction name to 'events' in forestore.
|
||||
/// Adds an event to firestore.
|
||||
Future<void> addEvent(String userId, EventModel event) async {
|
||||
try {
|
||||
final dataMap = event.toFirestoreMap();
|
||||
await firestore.collection('users/$userId/Events').add(dataMap);
|
||||
} catch (e) {
|
||||
print('Error adding Event to firestore: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a Stream of a single event.
|
||||
Observable<EventModel> getEvent(String userId, String eventId) {
|
||||
final mappedStream = firestore
|
||||
.collection('users/$userId/Events')
|
||||
.document(eventId)
|
||||
.snapshots()
|
||||
.map(
|
||||
(DocumentSnapshot snapshot) {
|
||||
return EventModel.fromFirestore(
|
||||
snapshot.data,
|
||||
id: snapshot.documentID,
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
return Observable(mappedStream);
|
||||
}
|
||||
|
||||
Future<void> deleteEvent(String userId, String eventId) async {
|
||||
try {
|
||||
final documentReference =
|
||||
firestore.document('users/$userId/Events/$eventId');
|
||||
await documentReference.delete();
|
||||
} catch (e) {
|
||||
print('Error deleting event in firestore: $e');
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> updateEvent(
|
||||
String userId,
|
||||
String eventId, {
|
||||
String name,
|
||||
int pendingtasks,
|
||||
List<int> media,
|
||||
List<String> tasks,
|
||||
int highPriority,
|
||||
int mediumPriority,
|
||||
int lowPriority,
|
||||
}) async {
|
||||
final newData = <String, dynamic>{
|
||||
'name': name,
|
||||
'pendingtasks': pendingtasks,
|
||||
'media': media,
|
||||
'tasks': tasks,
|
||||
'highPriority': highPriority,
|
||||
'mediumPriority': mediumPriority,
|
||||
'lowPriority': lowPriority,
|
||||
};
|
||||
newData.removeWhere((_, value) => value == null);
|
||||
|
||||
try {
|
||||
final documentReference =
|
||||
firestore.document('users/$userId/Events/$eventId');
|
||||
await documentReference.setData(newData, merge: true);
|
||||
} catch (e) {
|
||||
print('Error while updating Event in Firestore: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a stream of [List<EventModel] that correspond to
|
||||
/// a particular user.
|
||||
Observable<List<EventModel>> getUserEvents(String userDocumentId) {
|
||||
final mappedStream = firestore
|
||||
.collection('users')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue