diff --git a/backend/src/main/java/net/buzzert/kordophone/backend/db/CachedChatDatabase.kt b/backend/src/main/java/net/buzzert/kordophone/backend/db/CachedChatDatabase.kt index 658b0d0..513fe46 100644 --- a/backend/src/main/java/net/buzzert/kordophone/backend/db/CachedChatDatabase.kt +++ b/backend/src/main/java/net/buzzert/kordophone/backend/db/CachedChatDatabase.kt @@ -3,12 +3,16 @@ package net.buzzert.kordophone.backend.db import io.realm.kotlin.Realm import io.realm.kotlin.RealmConfiguration import net.buzzert.kordophone.backend.db.model.Conversation +import net.buzzert.kordophone.backend.db.model.Message import net.buzzert.kordophone.backend.db.model.toDatabaseConversation +import net.buzzert.kordophone.backend.db.model.toDatabaseMessage +import net.buzzert.kordophone.backend.model.GUID import net.buzzert.kordophone.backend.model.Conversation as ModelConversation +import net.buzzert.kordophone.backend.model.Message as ModelMessage internal class CachedChatDatabase (private val realmConfig: RealmConfiguration) { companion object { - private val schema = setOf(Conversation::class) + private val schema = setOf(Conversation::class, Message::class) fun liveDatabase(): CachedChatDatabase { return CachedChatDatabase( @@ -43,4 +47,22 @@ internal class CachedChatDatabase (private val realmConfig: RealmConfiguration) val items = realm.query(Conversation::class).find() return items.map { it.toConversation() } } + + fun writeMessages(messages: List, conversation: ModelConversation) { + val dbConversation = getConversationByGuid(conversation.guid) + realm.writeBlocking { + findLatest(dbConversation)?.messages?.addAll(messages.map { it.toDatabaseMessage() }) + } + } + + fun fetchMessages(conversation: ModelConversation): List { + val dbConversation = getConversationByGuid(conversation.guid) + return dbConversation.messages.map { it.toMessage() } + } + + private fun getConversationByGuid(guid: GUID): Conversation { + return realm.query(Conversation::class, "guid == '$guid'") + .find() + .first() + } } \ No newline at end of file diff --git a/backend/src/main/java/net/buzzert/kordophone/backend/db/model/Conversation.kt b/backend/src/main/java/net/buzzert/kordophone/backend/db/model/Conversation.kt index ebe31c5..dea4054 100644 --- a/backend/src/main/java/net/buzzert/kordophone/backend/db/model/Conversation.kt +++ b/backend/src/main/java/net/buzzert/kordophone/backend/db/model/Conversation.kt @@ -24,18 +24,19 @@ open class Conversation( var lastMessagePreview: String, var guid: GUID, - // TODO: Not sure how to do this yet... - // var messages: RealmList, + var messages: RealmList, ): RealmObject { constructor(): this( _id = ObjectId().toString(), displayName = null, - participants = realmListOf(), + participants = realmListOf(), date = RealmInstant.now(), unreadCount = 0, lastMessagePreview = "", guid = "", + + messages = realmListOf() ) fun toConversation(): ModelConversation { diff --git a/backend/src/test/java/net/buzzert/kordophone/backend/DatabaseTests.kt b/backend/src/test/java/net/buzzert/kordophone/backend/DatabaseTests.kt index 28cd9f9..a08746d 100644 --- a/backend/src/test/java/net/buzzert/kordophone/backend/DatabaseTests.kt +++ b/backend/src/test/java/net/buzzert/kordophone/backend/DatabaseTests.kt @@ -2,13 +2,14 @@ package net.buzzert.kordophone.backend import net.buzzert.kordophone.backend.db.CachedChatDatabase import net.buzzert.kordophone.backend.model.Conversation +import net.buzzert.kordophone.backend.model.Message import org.junit.Assert.assertEquals import org.junit.Test import java.util.Date class DatabaseTests { @Test - fun testCreateRetrieve() { + fun testConversationRetrieval() { val db = CachedChatDatabase.testDatabase() val conversation = Conversation( @@ -28,4 +29,35 @@ class DatabaseTests { val readConversation = readBackConversations[0] assertEquals(readConversation, conversation) } + + @Test + fun testMessageRetrieval() { + val db = CachedChatDatabase.testDatabase() + + val conversation = Conversation( + date = Date(), + participants = listOf("james@magahern.com"), + displayName = "Test", + unreadCount = 1, + lastMessagePreview = "Hello!", + guid = "1234", + ) + + db.writeConversations(listOf(conversation)) + + val message = Message( + text = "Hello!", + guid = "4321", + date = Date(), + sender = "james@magahern.com" + ) + + db.writeMessages(listOf(message), conversation) + + val readMessages = db.fetchMessages(conversation) + assertEquals(readMessages.count(), 1) + + val readMessage = readMessages[0] + assertEquals(readMessage, message) + } } \ No newline at end of file