-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabase.dart
40 lines (32 loc) · 1.3 KB
/
Database.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import 'package:cloud_firestore/cloud_firestore.dart';
class DatabaseMethods{
createChatRoom(String chatroomID, chatroomMap){
FirebaseFirestore.instance.collection("chatrooms").doc(chatroomID).set(chatroomMap)
.catchError((e){
print(e.toString());
});
}
getMessages(String chatroomid)async{
return await FirebaseFirestore.instance.collection("chatrooms").doc(chatroomid).collection('chats')
.orderBy("timestamp", descending: false).snapshots();
}
getChatrooms(String username)async{
return await FirebaseFirestore.instance.collection("chatrooms").where("users", arrayContains: username).snapshots();
}
addMessage(String chatroomid, MessageMap){
FirebaseFirestore.instance.collection("chatrooms").doc(chatroomid).collection('chats').add(MessageMap)
.catchError((e){
print(e.toString());
});
}
deleteMessage(int timestamp, String chatroomid )async{
await FirebaseFirestore.instance.collection("chatrooms").doc(chatroomid).collection('chats')
.where("timestamp", isEqualTo: timestamp).get().then((snapshot){
print("deleting message");
snapshot.docs.forEach((element) {
element.reference.delete();
print('message deleted');
});
});
}
}