Private
Public Access
1
0

reorg: separate db::records from insertable models

This commit is contained in:
2024-12-21 17:09:37 -08:00
parent c4c6e4245d
commit 8f523fd7fc
7 changed files with 123 additions and 107 deletions

View File

@@ -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::<DbConversation>(&mut self.db)
.first::<ConversationRecord>(&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::<DbParticipant>(&mut self.db)?;
.select(ParticipantRecord::as_select())
.load::<ParticipantRecord>(&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::<DbConversation>(&mut self.db)?;
.load::<ConversationRecord>(&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::<DbParticipant>(&mut self.db)?;
.select(ParticipantRecord::as_select())
.load::<ParticipantRecord>(&mut self.db)?;
let mut model_conversation: Conversation = db_conversation.into();
model_conversation.participants = db_participants.into_iter().map(|p| p.into()).collect();

View File

@@ -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<String>,
pub last_message_preview: Option<String>,
pub date: NaiveDateTime,
}
impl From<Conversation> 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<Conversation> for (DbConversation, Vec<Participant>) {
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<DbConversation> 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<String>,

View File

@@ -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<String>,
pub last_message_preview: Option<String>,
pub date: NaiveDateTime,
}
impl From<Conversation> 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<Conversation> for (Record, Vec<Participant>) {
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<Record> 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![],
}
}
}

View File

@@ -0,0 +1,2 @@
pub mod conversation;
pub mod participant;

View File

@@ -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<Record> for Participant {
fn from(record: Record) -> Self {
Participant {
display_name: record.display_name
}
}
}
impl From<Participant> for Record {
fn from(participant: Participant) -> Self {
Record {
id: 0, // This will be set by the database
display_name: participant.display_name,
}
}
}

View File

@@ -1,3 +1,8 @@
pub mod conversation;
pub mod date;
pub mod participant;
pub mod date;
pub mod db;
// Re-export the public types
pub use conversation::Conversation;
pub use participant::Participant;

View File

@@ -1,5 +1,4 @@
use diesel::prelude::*;
use crate::{models::conversation::DbConversation, schema::conversation_participants};
#[derive(Debug, Clone, PartialEq, Insertable)]
#[diesel(table_name = crate::schema::participants)]
@@ -7,37 +6,14 @@ pub struct Participant {
pub display_name: String,
}
impl From<DbParticipant> 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<Participant> for String {
fn into(self) -> Participant {
Participant { display_name: self }
impl From<String> 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() }
}
}