Private
Public Access
1
0
Files
Kordophone/kordophone-db/src/chat_database.rs

109 lines
3.7 KiB
Rust
Raw Normal View History

use anyhow::Result;
use diesel::prelude::*;
use diesel::query_dsl::BelongingToDsl;
use crate::{
models::{
Conversation,
db::conversation::Record as ConversationRecord,
db::participant::{Record as ParticipantRecord, ConversationParticipant},
},
schema,
};
2024-12-08 21:12:17 -08:00
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!();
2024-12-08 21:12:17 -08:00
pub struct ChatDatabase {
db: SqliteConnection,
2024-12-08 21:12:17 -08:00
}
impl ChatDatabase {
pub fn new_in_memory() -> Result<Self> {
let mut db = SqliteConnection::establish(":memory:")?;
db.run_pending_migrations(MIGRATIONS)
.map_err(|e| anyhow::anyhow!("Error running migrations: {}", e))?;
2024-12-21 18:01:00 -08:00
Ok(Self { db })
2024-12-08 21:12:17 -08:00
}
pub fn insert_conversation(&mut self, conversation: Conversation) -> Result<()> {
use crate::schema::conversations::dsl::*;
use crate::schema::participants::dsl::*;
use crate::schema::conversation_participants::dsl::*;
let (db_conversation, db_participants) = conversation.into();
diesel::replace_into(conversations)
.values(&db_conversation)
.execute(&mut self.db)?;
diesel::replace_into(participants)
.values(&db_participants)
.execute(&mut self.db)?;
// Sqlite backend doesn't support batch insert, so we have to do this manually
for participant in db_participants {
let pid = participants
.select(schema::participants::id)
.filter(schema::participants::display_name.eq(&participant.display_name))
.first::<i32>(&mut self.db)?;
diesel::replace_into(conversation_participants)
.values((
conversation_id.eq(&db_conversation.id),
participant_id.eq(pid),
))
.execute(&mut self.db)?;
2024-12-08 21:12:17 -08:00
}
Ok(())
2024-12-08 21:12:17 -08:00
}
pub fn get_conversation_by_guid(&mut self, match_guid: &str) -> Result<Option<Conversation>> {
use crate::schema::conversations::dsl::*;
use crate::schema::participants::dsl::*;
2024-12-08 21:12:17 -08:00
let result = conversations
.find(match_guid)
.first::<ConversationRecord>(&mut self.db)
.optional()?;
if let Some(conversation) = result {
let db_participants = ConversationParticipant::belonging_to(&conversation)
.inner_join(participants)
.select(ParticipantRecord::as_select())
.load::<ParticipantRecord>(&mut self.db)?;
let mut model_conversation: Conversation = conversation.into();
model_conversation.participants = db_participants.into_iter().map(|p| p.into()).collect();
2024-12-08 21:12:17 -08:00
return Ok(Some(model_conversation));
}
Ok(None)
}
pub fn all_conversations(&mut self) -> Result<Vec<Conversation>> {
use crate::schema::conversations::dsl::*;
use crate::schema::participants::dsl::*;
let db_conversations = conversations
.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(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();
result.push(model_conversation);
}
Ok(result)
2024-12-08 21:12:17 -08:00
}
}