reorg: separate db::records from insertable models
This commit is contained in:
@@ -1,13 +1,15 @@
|
|||||||
use std::error::Error;
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
use diesel::query_dsl::BelongingToDsl;
|
use diesel::query_dsl::BelongingToDsl;
|
||||||
|
|
||||||
use crate::{models::{
|
use crate::{
|
||||||
conversation::{
|
models::{
|
||||||
self, Conversation, DbConversation
|
Conversation,
|
||||||
}, participant::{ConversationParticipant, DbParticipant, Participant}
|
db::conversation::Record as ConversationRecord,
|
||||||
}, schema};
|
db::participant::{Record as ParticipantRecord, ConversationParticipant},
|
||||||
|
},
|
||||||
|
schema,
|
||||||
|
};
|
||||||
|
|
||||||
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
|
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
|
||||||
pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!();
|
pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!();
|
||||||
@@ -22,9 +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 {
|
return Ok(Self { db });
|
||||||
db: db,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn insert_conversation(&mut self, conversation: Conversation) -> Result<()> {
|
pub fn insert_conversation(&mut self, conversation: Conversation) -> Result<()> {
|
||||||
@@ -66,19 +66,19 @@ impl ChatDatabase {
|
|||||||
|
|
||||||
let result = conversations
|
let result = conversations
|
||||||
.find(match_guid)
|
.find(match_guid)
|
||||||
.first::<DbConversation>(&mut self.db)
|
.first::<ConversationRecord>(&mut self.db)
|
||||||
.optional()?;
|
.optional()?;
|
||||||
|
|
||||||
if let Some(conversation) = result {
|
if let Some(conversation) = result {
|
||||||
let dbParticipants = ConversationParticipant::belonging_to(&conversation)
|
let db_participants = ConversationParticipant::belonging_to(&conversation)
|
||||||
.inner_join(participants)
|
.inner_join(participants)
|
||||||
.select(DbParticipant::as_select())
|
.select(ParticipantRecord::as_select())
|
||||||
.load::<DbParticipant>(&mut self.db)?;
|
.load::<ParticipantRecord>(&mut self.db)?;
|
||||||
|
|
||||||
let mut modelConversation: Conversation = conversation.into();
|
let mut model_conversation: Conversation = conversation.into();
|
||||||
modelConversation.participants = dbParticipants.into_iter().map(|p| p.into()).collect();
|
model_conversation.participants = db_participants.into_iter().map(|p| p.into()).collect();
|
||||||
|
|
||||||
return Ok(Some(modelConversation));
|
return Ok(Some(model_conversation));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(None)
|
Ok(None)
|
||||||
@@ -89,14 +89,14 @@ impl ChatDatabase {
|
|||||||
use crate::schema::participants::dsl::*;
|
use crate::schema::participants::dsl::*;
|
||||||
|
|
||||||
let db_conversations = conversations
|
let db_conversations = conversations
|
||||||
.load::<DbConversation>(&mut self.db)?;
|
.load::<ConversationRecord>(&mut self.db)?;
|
||||||
|
|
||||||
let mut result = Vec::new();
|
let mut result = Vec::new();
|
||||||
for db_conversation in db_conversations {
|
for db_conversation in db_conversations {
|
||||||
let db_participants = ConversationParticipant::belonging_to(&db_conversation)
|
let db_participants = ConversationParticipant::belonging_to(&db_conversation)
|
||||||
.inner_join(participants)
|
.inner_join(participants)
|
||||||
.select(DbParticipant::as_select())
|
.select(ParticipantRecord::as_select())
|
||||||
.load::<DbParticipant>(&mut self.db)?;
|
.load::<ParticipantRecord>(&mut self.db)?;
|
||||||
|
|
||||||
let mut model_conversation: Conversation = db_conversation.into();
|
let mut model_conversation: Conversation = db_conversation.into();
|
||||||
model_conversation.participants = db_participants.into_iter().map(|p| p.into()).collect();
|
model_conversation.participants = db_participants.into_iter().map(|p| p.into()).collect();
|
||||||
|
|||||||
@@ -1,49 +1,6 @@
|
|||||||
use diesel::prelude::*;
|
|
||||||
use chrono::NaiveDateTime;
|
use chrono::NaiveDateTime;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
use crate::models::participant::Participant;
|
||||||
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
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Conversation {
|
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)]
|
#[derive(Default)]
|
||||||
pub struct ConversationBuilder {
|
pub struct ConversationBuilder {
|
||||||
guid: Option<String>,
|
guid: Option<String>,
|
||||||
|
|||||||
52
kordophone-db/src/models/db/conversation.rs
Normal file
52
kordophone-db/src/models/db/conversation.rs
Normal 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![],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
2
kordophone-db/src/models/db/mod.rs
Normal file
2
kordophone-db/src/models/db/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
pub mod conversation;
|
||||||
|
pub mod participant;
|
||||||
37
kordophone-db/src/models/db/participant.rs
Normal file
37
kordophone-db/src/models/db/participant.rs
Normal 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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +1,8 @@
|
|||||||
pub mod conversation;
|
pub mod conversation;
|
||||||
|
pub mod participant;
|
||||||
pub mod date;
|
pub mod date;
|
||||||
pub mod participant;
|
pub mod db;
|
||||||
|
|
||||||
|
// Re-export the public types
|
||||||
|
pub use conversation::Conversation;
|
||||||
|
pub use participant::Participant;
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
use crate::{models::conversation::DbConversation, schema::conversation_participants};
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Insertable)]
|
#[derive(Debug, Clone, PartialEq, Insertable)]
|
||||||
#[diesel(table_name = crate::schema::participants)]
|
#[diesel(table_name = crate::schema::participants)]
|
||||||
@@ -7,37 +6,14 @@ pub struct Participant {
|
|||||||
pub display_name: String,
|
pub display_name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<DbParticipant> for Participant {
|
impl From<String> for Participant {
|
||||||
fn from(participant: DbParticipant) -> Self {
|
fn from(display_name: String) -> Self {
|
||||||
Participant { display_name: 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<&str> for Participant {
|
impl From<&str> for Participant {
|
||||||
fn from(s: &str) -> Self {
|
fn from(display_name: &str) -> Self {
|
||||||
Participant { display_name: s.into() }
|
Participant { display_name: display_name.to_string() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user