Skip to content

Commit

Permalink
MESSAGE DONE
Browse files Browse the repository at this point in the history
  • Loading branch information
ODAncona committed Aug 30, 2022
1 parent e2bb3b0 commit f1bacc5
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 41 deletions.
35 changes: 22 additions & 13 deletions lib/api/firebase_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,48 @@ 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");
throw Exception(error);
});
}

@override
void deleteMessage(String messageId) {
collectionReference.doc(messageId).delete();
}

@override
Future<Message> 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<String, dynamic>;
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) {
log("Failed to update message: $error");
throw Exception(error);
});
}

@override
void deleteMessage(String messageId) {
collectionReference.doc(messageId).delete();
}
}
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
31 changes: 20 additions & 11 deletions lib/model/message.dart
Original file line number Diff line number Diff line change
@@ -1,39 +1,44 @@
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;
String? content;
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<String, dynamic> message) {
factory Message.fromFirestore(
DocumentSnapshot<Map<String, dynamic>> 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<String, dynamic> toFirestore() {
return {
'id': id,
'uid': uid,
'time': time,
'fromId': fromId,
'toId': toId,
Expand All @@ -46,4 +51,8 @@ class Message implements IModel {
String toString() {
return 'Message{$fromId $toId $content $type}';
}

void setContent(String newContent) {
content = newContent;
}
}
58 changes: 42 additions & 16 deletions test/message_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> 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);
});
}

0 comments on commit f1bacc5

Please sign in to comment.