diff --git a/lib/api/firebase_message.dart b/lib/api/firebase_message.dart index cbaa579..6805b65 100644 --- a/lib/api/firebase_message.dart +++ b/lib/api/firebase_message.dart @@ -13,7 +13,11 @@ class FirebaseMessage extends FirebaseAPI implements IMessage { @override void createMessage(Message message) { collectionReference - .add(message.toFirestore()) + .withConverter( + fromFirestore: Message.fromFirestore, + toFirestore: (Message message, options) => message.toFirestore()) + .doc(message.uid) + .set(message) .then((value) => log("Message Added")) .catchError((error) { log("Failed to add message: $error"); @@ -21,26 +25,26 @@ class FirebaseMessage extends FirebaseAPI implements IMessage { }); } - @override - void deleteMessage(String messageId) { - collectionReference.doc(messageId).delete(); - } - @override Future readMessage(String messageId) async { - final docRef = collectionReference.doc(messageId); - final doc = await docRef.get(); - if (!doc.exists) { + final docRef = collectionReference.doc(messageId).withConverter( + fromFirestore: Message.fromFirestore, + toFirestore: (Message city, _) => city.toFirestore(), + ); + final docSnapshot = await docRef.get(); + final message = docSnapshot.data(); + if (message != null) { + return message; + } else { + log("Doc does not exist"); throw Error(); } - final data = doc.data() as Map; - return Message.fromJson(data); } @override - updateMessage(Message message) { + void updateMessage(Message message) { collectionReference - .doc('FAKE') + .doc(message.uid) .update(message.toFirestore()) .then((value) => log("Message Updated")) .catchError((error) { @@ -48,4 +52,9 @@ class FirebaseMessage extends FirebaseAPI implements IMessage { throw Exception(error); }); } + + @override + void deleteMessage(String messageId) { + collectionReference.doc(messageId).delete(); + } } diff --git a/lib/main.dart b/lib/main.dart index fed9c7b..94a29c2 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -20,7 +20,7 @@ void main() async { ); IMessage msg = FirebaseMessage(FirebaseFirestore.instance); - final m1 = Message(id: '', fromId: 'alice', toId: 'bob', content: "HELLOW"); + final m1 = Message(uid: '', fromId: 'alice', toId: 'bob', content: "HELLOW"); msg.createMessage(m1); log("test"); runApp(MyApp()); diff --git a/lib/model/message.dart b/lib/model/message.dart index 7442454..e5a304b 100644 --- a/lib/model/message.dart +++ b/lib/model/message.dart @@ -1,9 +1,10 @@ +import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:uuid/uuid.dart'; import 'imodel.dart'; class Message implements IModel { - String id = const Uuid().v1(); + String uid = const Uuid().v1(); DateTime? time; String? fromId; String? toId; @@ -11,29 +12,33 @@ class Message implements IModel { int? type; Message({ - required this.id, + String? uid, this.time, this.fromId, this.toId, this.content, this.type, - }); + }) : uid = uid ?? const Uuid().v1(); - factory Message.fromJson(Map message) { + factory Message.fromFirestore( + DocumentSnapshot> snapshot, + SnapshotOptions? options, + ) { + final data = snapshot.data(); return Message( - id: message['id'], - time: message['time'], - fromId: message['fromId'], - toId: message['toId'], - content: message['content'], - type: message['type'], + uid: data?['uid'], + time: data?['time'], + fromId: data?['fromId'], + toId: data?['toId'], + content: data?['content'], + type: data?['type'], ); } @override Map toFirestore() { return { - 'id': id, + 'uid': uid, 'time': time, 'fromId': fromId, 'toId': toId, @@ -46,4 +51,8 @@ class Message implements IModel { String toString() { return 'Message{$fromId $toId $content $type}'; } + + void setContent(String newContent) { + content = newContent; + } } diff --git a/test/message_test.dart b/test/message_test.dart index cf4732a..2b47c26 100644 --- a/test/message_test.dart +++ b/test/message_test.dart @@ -5,38 +5,64 @@ import 'package:pdg_app/api/firebase_message.dart'; import 'package:pdg_app/model/message.dart'; final db = FakeFirebaseFirestore(); -Message msg1 = - Message(content: 'Bonjour', fromId: 'alice', toId: 'bob', id: ''); +Message msg1 = Message(content: 'Bonjour', fromId: 'alice', toId: 'bob'); +Message msg2 = Message(content: 'ciao', fromId: 'alice', toId: 'bob'); -void populateMockMessage(Message c) async { - await db.collection('message').add(c.toFirestore()); +Future populateMockMessage(Message c) async { + await db.collection('message').doc(c.uid).set(c.toFirestore()); } void main() { - late final IMessage messageApi; + late IMessage messageApi; + final messages = db.collection('message'); setUp(() async { - populateMockMessage(msg1); + populateMockMessage(msg2); messageApi = FirebaseMessage(db); }); - test("Create Message", () { + test("Create Message", () async { messageApi.createMessage(msg1); - expect(msg1, msg1); + final docSnapshot = await messages + .doc(msg1.uid) + .withConverter( + fromFirestore: Message.fromFirestore, + toFirestore: (Message message, _) => message.toFirestore(), + ) + .get(); + final message = docSnapshot.data(); + expect(msg1.toString(), message.toString()); }); - test("Read Message", () { - messageApi.readMessage('fd'); - expect(msg1, msg1); + test("Read Message", () async { + final Message m2Bis = await messageApi.readMessage(msg2.uid); + expect(msg2.toString(), m2Bis.toString()); }); - test("Update message", () { + test("Update message", () async { + msg1.setContent('Filippo'); messageApi.updateMessage(msg1); - expect(msg1, msg1); + final docSnapshot = await messages + .doc(msg1.uid) + .withConverter( + fromFirestore: Message.fromFirestore, + toFirestore: (Message city, _) => city.toFirestore(), + ) + .get(); + final c2 = docSnapshot.data(); + expect('Filippo', c2!.content); }); - test("Delete message", () { - messageApi.deleteMessage(''); - expect(msg1, msg1); + test("Delete message", () async { + messageApi.deleteMessage(msg2.uid); + final docSnapshot = await messages + .doc(msg2.uid) + .withConverter( + fromFirestore: Message.fromFirestore, + toFirestore: (Message city, _) => city.toFirestore(), + ) + .get(); + final message = docSnapshot.data(); + expect(message, null); }); }