diff --git a/kordophone-db/src/chat_database.rs b/kordophone-db/src/chat_database.rs index 8c9f2bd..8f4f1eb 100644 --- a/kordophone-db/src/chat_database.rs +++ b/kordophone-db/src/chat_database.rs @@ -1,13 +1,15 @@ -use std::error::Error; use anyhow::Result; use diesel::prelude::*; use diesel::query_dsl::BelongingToDsl; -use crate::{models::{ - conversation::{ - self, Conversation, DbConversation - }, participant::{ConversationParticipant, DbParticipant, Participant} -}, schema}; +use crate::{ + models::{ + Conversation, + db::conversation::Record as ConversationRecord, + db::participant::{Record as ParticipantRecord, ConversationParticipant}, + }, + schema, +}; use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness}; pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!(); @@ -22,9 +24,7 @@ impl ChatDatabase { db.run_pending_migrations(MIGRATIONS) .map_err(|e| anyhow::anyhow!("Error running migrations: {}", e))?; - return Ok(Self { - db: db, - }) + return Ok(Self { db }); } pub fn insert_conversation(&mut self, conversation: Conversation) -> Result<()> { @@ -66,19 +66,19 @@ impl ChatDatabase { let result = conversations .find(match_guid) - .first::(&mut self.db) + .first::(&mut self.db) .optional()?; if let Some(conversation) = result { - let dbParticipants = ConversationParticipant::belonging_to(&conversation) + let db_participants = ConversationParticipant::belonging_to(&conversation) .inner_join(participants) - .select(DbParticipant::as_select()) - .load::(&mut self.db)?; + .select(ParticipantRecord::as_select()) + .load::(&mut self.db)?; - let mut modelConversation: Conversation = conversation.into(); - modelConversation.participants = dbParticipants.into_iter().map(|p| p.into()).collect(); + let mut model_conversation: Conversation = conversation.into(); + model_conversation.participants = db_participants.into_iter().map(|p| p.into()).collect(); - return Ok(Some(modelConversation)); + return Ok(Some(model_conversation)); } Ok(None) @@ -89,14 +89,14 @@ impl ChatDatabase { use crate::schema::participants::dsl::*; let db_conversations = conversations - .load::(&mut self.db)?; + .load::(&mut self.db)?; 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)?; + .select(ParticipantRecord::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(); diff --git a/kordophone-db/src/models/conversation.rs b/kordophone-db/src/models/conversation.rs index e227d00..415e151 100644 --- a/kordophone-db/src/models/conversation.rs +++ b/kordophone-db/src/models/conversation.rs @@ -1,49 +1,6 @@ -use diesel::prelude::*; use chrono::NaiveDateTime; use uuid::Uuid; - -use crate::models::{ - participant::Participant, -}; - -#[derive(Queryable, Selectable, Insertable, AsChangeset, Clone, Identifiable)] -#[diesel(table_name = crate::schema::conversations)] -#[diesel(check_for_backend(diesel::sqlite::Sqlite))] -pub struct DbConversation { - pub id: String, - pub unread_count: i64, - pub display_name: Option, - pub last_message_preview: Option, - pub date: NaiveDateTime, -} - -impl From for DbConversation { - fn from(conversation: Conversation) -> Self { - Self { - id: conversation.guid, - unread_count: conversation.unread_count as i64, - display_name: conversation.display_name, - last_message_preview: conversation.last_message_preview, - date: conversation.date, - } - } -} - -impl From for (DbConversation, Vec) { - fn from(conversation: Conversation) -> Self { - ( - DbConversation { - id: conversation.guid, - unread_count: conversation.unread_count as i64, - display_name: conversation.display_name, - last_message_preview: conversation.last_message_preview, - date: conversation.date, - }, - - conversation.participants - ) - } -} +use crate::models::participant::Participant; #[derive(Clone, Debug)] pub struct Conversation { @@ -72,19 +29,6 @@ impl Conversation { } } -impl From for Conversation { - fn from(db_conversation: DbConversation) -> Self { - Self { - guid: db_conversation.id, - unread_count: db_conversation.unread_count as u16, - display_name: db_conversation.display_name, - last_message_preview: db_conversation.last_message_preview, - date: db_conversation.date, - participants: vec![], - } - } -} - #[derive(Default)] pub struct ConversationBuilder { guid: Option, diff --git a/kordophone-db/src/models/db/conversation.rs b/kordophone-db/src/models/db/conversation.rs new file mode 100644 index 0000000..0ca384e --- /dev/null +++ b/kordophone-db/src/models/db/conversation.rs @@ -0,0 +1,52 @@ +use diesel::prelude::*; +use chrono::NaiveDateTime; +use crate::models::{Conversation, Participant}; + +#[derive(Queryable, Selectable, Insertable, AsChangeset, Clone, Identifiable)] +#[diesel(table_name = crate::schema::conversations)] +#[diesel(check_for_backend(diesel::sqlite::Sqlite))] +pub struct Record { + pub id: String, + pub unread_count: i64, + pub display_name: Option, + pub last_message_preview: Option, + pub date: NaiveDateTime, +} + +impl From for Record { + fn from(conversation: Conversation) -> Self { + Self { + id: conversation.guid, + unread_count: conversation.unread_count as i64, + display_name: conversation.display_name, + last_message_preview: conversation.last_message_preview, + date: conversation.date, + } + } +} + +// This implementation returns the insertable data types for the conversation and participants +impl From for (Record, Vec) { + fn from(conversation: Conversation) -> Self { + ( + Record::from(conversation.clone()), + + // Keep in mind, db::participant::Record is the selectable data type for the + // participants table, whereas Participant is the insertable model type. + conversation.participants + ) + } +} + +impl From for Conversation { + fn from(record: Record) -> Self { + Self { + guid: record.id, + unread_count: record.unread_count as u16, + display_name: record.display_name, + last_message_preview: record.last_message_preview, + date: record.date, + participants: vec![], + } + } +} \ No newline at end of file diff --git a/kordophone-db/src/models/db/mod.rs b/kordophone-db/src/models/db/mod.rs new file mode 100644 index 0000000..6c3c3f6 --- /dev/null +++ b/kordophone-db/src/models/db/mod.rs @@ -0,0 +1,2 @@ +pub mod conversation; +pub mod participant; diff --git a/kordophone-db/src/models/db/participant.rs b/kordophone-db/src/models/db/participant.rs new file mode 100644 index 0000000..2e4ead1 --- /dev/null +++ b/kordophone-db/src/models/db/participant.rs @@ -0,0 +1,37 @@ +use diesel::prelude::*; +use crate::models::Participant; +use crate::schema::conversation_participants; + +#[derive(Queryable, Selectable, AsChangeset, Clone, PartialEq, Debug, Identifiable)] +#[diesel(table_name = crate::schema::participants)] +pub struct Record { + pub id: i32, + pub display_name: String +} + +#[derive(Identifiable, Selectable, Queryable, Associations, Debug)] +#[diesel(belongs_to(super::conversation::Record, foreign_key = conversation_id))] +#[diesel(belongs_to(Record, foreign_key = participant_id))] +#[diesel(table_name = conversation_participants)] +#[diesel(primary_key(conversation_id, participant_id))] +pub struct ConversationParticipant { + pub conversation_id: String, + pub participant_id: i32, +} + +impl From for Participant { + fn from(record: Record) -> Self { + Participant { + display_name: record.display_name + } + } +} + +impl From for Record { + fn from(participant: Participant) -> Self { + Record { + id: 0, // This will be set by the database + display_name: participant.display_name, + } + } +} \ No newline at end of file diff --git a/kordophone-db/src/models/mod.rs b/kordophone-db/src/models/mod.rs index 8323539..4d76d37 100644 --- a/kordophone-db/src/models/mod.rs +++ b/kordophone-db/src/models/mod.rs @@ -1,3 +1,8 @@ pub mod conversation; +pub mod participant; pub mod date; -pub mod participant; \ No newline at end of file +pub mod db; + +// Re-export the public types +pub use conversation::Conversation; +pub use participant::Participant; \ No newline at end of file diff --git a/kordophone-db/src/models/participant.rs b/kordophone-db/src/models/participant.rs index 89733d8..349187a 100644 --- a/kordophone-db/src/models/participant.rs +++ b/kordophone-db/src/models/participant.rs @@ -1,5 +1,4 @@ -use diesel::prelude::*; -use crate::{models::conversation::DbConversation, schema::conversation_participants}; +use diesel::prelude::*; #[derive(Debug, Clone, PartialEq, Insertable)] #[diesel(table_name = crate::schema::participants)] @@ -7,37 +6,14 @@ pub struct Participant { pub display_name: String, } -impl From for Participant { - fn from(participant: DbParticipant) -> Self { - Participant { display_name: participant.display_name } - } -} - -#[derive(Queryable, Selectable, Insertable, AsChangeset, Clone, PartialEq, Debug, Identifiable)] -#[diesel(table_name = crate::schema::participants)] -pub struct DbParticipant { - pub id: i32, - pub display_name: String -} - -#[derive(Identifiable, Selectable, Queryable, Associations, Debug)] -#[diesel(belongs_to(DbConversation, foreign_key = conversation_id))] -#[diesel(belongs_to(DbParticipant, foreign_key = participant_id))] -#[diesel(table_name = conversation_participants)] -#[diesel(primary_key(conversation_id, participant_id))] -pub struct ConversationParticipant { - pub conversation_id: String, - pub participant_id: i32, -} - -impl Into for String { - fn into(self) -> Participant { - Participant { display_name: self } +impl From for Participant { + fn from(display_name: String) -> Self { + Participant { display_name } } } impl From<&str> for Participant { - fn from(s: &str) -> Self { - Participant { display_name: s.into() } + fn from(display_name: &str) -> Self { + Participant { display_name: display_name.to_string() } } }