Private
Public Access
1
0

clippy/cleanup

This commit is contained in:
2024-12-21 18:01:00 -08:00
parent 53d4604b63
commit 89f8d21ebb
5 changed files with 25 additions and 11 deletions

View File

@@ -24,7 +24,7 @@ impl ChatDatabase {
db.run_pending_migrations(MIGRATIONS) db.run_pending_migrations(MIGRATIONS)
.map_err(|e| anyhow::anyhow!("Error running migrations: {}", e))?; .map_err(|e| anyhow::anyhow!("Error running migrations: {}", e))?;
return Ok(Self { db }); Ok(Self { db })
} }
pub fn insert_conversation(&mut self, conversation: Conversation) -> Result<()> { pub fn insert_conversation(&mut self, conversation: Conversation) -> Result<()> {

View File

@@ -1,6 +1,9 @@
use diesel::prelude::*; use diesel::prelude::*;
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use crate::models::{Conversation, Participant}; use crate::models::{
Conversation,
db::participant::InsertableRecord as InsertableParticipant,
};
#[derive(Queryable, Selectable, Insertable, AsChangeset, Clone, Identifiable)] #[derive(Queryable, Selectable, Insertable, AsChangeset, Clone, Identifiable)]
#[diesel(table_name = crate::schema::conversations)] #[diesel(table_name = crate::schema::conversations)]
@@ -26,14 +29,15 @@ impl From<Conversation> for Record {
} }
// This implementation returns the insertable data types for the conversation and participants // This implementation returns the insertable data types for the conversation and participants
impl From<Conversation> for (Record, Vec<Participant>) { impl From<Conversation> for (Record, Vec<InsertableParticipant>) {
fn from(conversation: Conversation) -> Self { fn from(conversation: Conversation) -> Self {
( (
Record::from(conversation.clone()), 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 conversation.participants
.into_iter()
.map(InsertableParticipant::from)
.collect()
) )
} }
} }

View File

@@ -2,13 +2,27 @@ use diesel::prelude::*;
use crate::models::Participant; use crate::models::Participant;
use crate::schema::conversation_participants; use crate::schema::conversation_participants;
#[derive(Queryable, Selectable, AsChangeset, Clone, PartialEq, Debug, Identifiable)] #[derive(Queryable, Selectable, AsChangeset, Identifiable)]
#[diesel(table_name = crate::schema::participants)] #[diesel(table_name = crate::schema::participants)]
pub struct Record { pub struct Record {
pub id: i32, pub id: i32,
pub display_name: String pub display_name: String
} }
#[derive(Insertable)]
#[diesel(table_name = crate::schema::participants)]
pub struct InsertableRecord {
pub display_name: String
}
impl From<Participant> for InsertableRecord {
fn from(participant: Participant) -> Self {
InsertableRecord {
display_name: participant.display_name
}
}
}
#[derive(Identifiable, Selectable, Queryable, Associations, Debug)] #[derive(Identifiable, Selectable, Queryable, Associations, Debug)]
#[diesel(belongs_to(super::conversation::Record, foreign_key = conversation_id))] #[diesel(belongs_to(super::conversation::Record, foreign_key = conversation_id))]
#[diesel(belongs_to(Record, foreign_key = participant_id))] #[diesel(belongs_to(Record, foreign_key = participant_id))]

View File

@@ -2,6 +2,5 @@ pub mod conversation;
pub mod participant; pub mod participant;
pub mod db; pub mod db;
// Re-export the public types
pub use conversation::Conversation; pub use conversation::Conversation;
pub use participant::Participant; pub use participant::Participant;

View File

@@ -1,7 +1,4 @@
use diesel::prelude::*; #[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Insertable)]
#[diesel(table_name = crate::schema::participants)]
pub struct Participant { pub struct Participant {
pub display_name: String, pub display_name: String,
} }