reorg: separate db::records from insertable models
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user