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 './models/user_model.dart';
|
||||||
import './resources/firestore_provider.dart';
|
import './resources/firestore_provider.dart';
|
||||||
|
|
||||||
/* class App extends StatelessWidget {
|
class App extends StatelessWidget {
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final fire = FirestoreProvider();
|
final fire = FirestoreProvider();
|
||||||
return MaterialApp(
|
return MaterialApp(
|
||||||
|
|
@ -19,9 +19,9 @@ import './resources/firestore_provider.dart';
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} */
|
}
|
||||||
|
|
||||||
class App extends StatelessWidget {
|
/* class App extends StatelessWidget {
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final fire = FirestoreProvider();
|
final fire = FirestoreProvider();
|
||||||
return MaterialApp(
|
return MaterialApp(
|
||||||
|
|
@ -32,9 +32,9 @@ class App extends StatelessWidget {
|
||||||
title: Text('DO>'),
|
title: Text('DO>'),
|
||||||
),
|
),
|
||||||
body: StreamBuilder(
|
body: StreamBuilder(
|
||||||
stream: fire.getUserEvents('vBOvtmTeC8iPg8L4Hixh'),
|
stream: fire.getEvent('vBOvtmTeC8iPg8L4Hixh', '-LZReccofbHpw9UfOTMk'),
|
||||||
builder: (BuildContext context,
|
builder:
|
||||||
AsyncSnapshot<List<EventModel>> userSnapshot) {
|
(BuildContext context, AsyncSnapshot<EventModel> userSnapshot) {
|
||||||
if (!userSnapshot.hasData) {
|
if (!userSnapshot.hasData) {
|
||||||
return Center(
|
return Center(
|
||||||
child: CircularProgressIndicator(),
|
child: CircularProgressIndicator(),
|
||||||
|
|
@ -51,11 +51,21 @@ class App extends StatelessWidget {
|
||||||
event: 'Math',
|
event: 'Math',
|
||||||
);
|
);
|
||||||
|
|
||||||
fire.updateTask(
|
final event = EventModel(
|
||||||
'-LZRNhS9mX-SO0XgfQIM',
|
name: 'Langs and trans',
|
||||||
done: true,
|
tasks: <String>[],
|
||||||
text: 'Hellloooooo',
|
highPriority: 0,
|
||||||
priority: 1,
|
mediumPriority: 0,
|
||||||
|
lowPriority: 0,
|
||||||
|
media: <String>[],
|
||||||
|
when: <bool>[],
|
||||||
|
pendigTasks: 0,
|
||||||
|
);
|
||||||
|
|
||||||
|
fire.updateEvent(
|
||||||
|
'vBOvtmTeC8iPg8L4Hixh',
|
||||||
|
'-LZReccofbHpw9UfOTMk',
|
||||||
|
name: 'kjhahslkdjhf',
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
child: Text('Add task'),
|
child: Text('Add task'),
|
||||||
|
|
@ -63,9 +73,11 @@ class App extends StatelessWidget {
|
||||||
];
|
];
|
||||||
//children.add(Text(userSnapshot.data.text));
|
//children.add(Text(userSnapshot.data.text));
|
||||||
|
|
||||||
userSnapshot.data.forEach((EventModel task) {
|
//userSnapshot.data.forEach((EventModel task) {
|
||||||
children.add(Text(task.name));
|
// children.add(Text(task.name));
|
||||||
});
|
//});
|
||||||
|
|
||||||
|
children.add(Text(userSnapshot.data.name));
|
||||||
|
|
||||||
return Column(
|
return Column(
|
||||||
children: children,
|
children: children,
|
||||||
|
|
@ -75,4 +87,4 @@ class App extends StatelessWidget {
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
} */
|
||||||
|
|
|
||||||
|
|
@ -33,4 +33,17 @@ class EventModel {
|
||||||
highPriority = firestoreMap["highPriority"],
|
highPriority = firestoreMap["highPriority"],
|
||||||
mediumPriority = firestoreMap["mediumPriority"],
|
mediumPriority = firestoreMap["mediumPriority"],
|
||||||
lowPriority = firestoreMap["lowPriority"];
|
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-----------------------------
|
//-----------------------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) {
|
Observable<List<EventModel>> getUserEvents(String userDocumentId) {
|
||||||
final mappedStream = firestore
|
final mappedStream = firestore
|
||||||
.collection('users')
|
.collection('users')
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue