diff --git a/kordophone-db/src/chat_database.rs b/kordophone-db/src/chat_database.rs index 8d25f83..8c9f2bd 100644 --- a/kordophone-db/src/chat_database.rs +++ b/kordophone-db/src/chat_database.rs @@ -86,14 +86,23 @@ impl ChatDatabase { pub fn all_conversations(&mut self) -> Result> { use crate::schema::conversations::dsl::*; + use crate::schema::participants::dsl::*; - let result = conversations - .load::(&mut self.db)? - .into_iter() - .map(|c| c.into()) - .collect(); + let db_conversations = conversations + .load::(&mut self.db)?; - // TODO: Need to resolve participants here also somehow... + let mut result = Vec::new(); + for db_conversation in db_conversations { + let db_participants = ConversationParticipant::belonging_to(&db_conversation) + .inner_join(participants) + .select(DbParticipant::as_select()) + .load::(&mut self.db)?; + + let mut model_conversation: Conversation = db_conversation.into(); + model_conversation.participants = db_participants.into_iter().map(|p| p.into()).collect(); + + result.push(model_conversation); + } Ok(result) } diff --git a/kordophone-db/src/lib.rs b/kordophone-db/src/lib.rs index f7623f7..baa25fa 100644 --- a/kordophone-db/src/lib.rs +++ b/kordophone-db/src/lib.rs @@ -83,4 +83,42 @@ mod tests { assert_eq!(participants, read_participants); } + + #[test] + fn test_all_conversations_with_participants() { + let mut db = ChatDatabase::new_in_memory().unwrap(); + + // Create two conversations with different participants + let participants1: Vec = vec!["one".into(), "two".into()]; + let participants2: Vec = vec!["three".into(), "four".into()]; + + let guid1 = uuid::Uuid::new_v4().to_string(); + let conversation1 = ConversationBuilder::new() + .guid(&guid1) + .display_name("Test 1") + .participants(participants1.clone()) + .build(); + + let guid2 = uuid::Uuid::new_v4().to_string(); + let conversation2 = ConversationBuilder::new() + .guid(&guid2) + .display_name("Test 2") + .participants(participants2.clone()) + .build(); + + // Insert both conversations + db.insert_conversation(conversation1).unwrap(); + db.insert_conversation(conversation2).unwrap(); + + // Get all conversations and verify the results + let all_conversations = db.all_conversations().unwrap(); + assert_eq!(all_conversations.len(), 2); + + // Find and verify each conversation's participants + let conv1 = all_conversations.iter().find(|c| c.guid == guid1).unwrap(); + let conv2 = all_conversations.iter().find(|c| c.guid == guid2).unwrap(); + + assert_eq!(conv1.participants, participants1); + assert_eq!(conv2.participants, participants2); + } }