refactor: with_repository/with_settings
This commit is contained in:
@@ -3,7 +3,6 @@ use diesel::prelude::*;
|
||||
use diesel::query_dsl::BelongingToDsl;
|
||||
|
||||
use crate::{
|
||||
database::Database,
|
||||
models::{
|
||||
Conversation,
|
||||
Message,
|
||||
@@ -20,12 +19,12 @@ use crate::{
|
||||
};
|
||||
|
||||
pub struct Repository<'a> {
|
||||
db: &'a mut Database,
|
||||
connection: &'a mut SqliteConnection,
|
||||
}
|
||||
|
||||
impl<'a> Repository<'a> {
|
||||
pub fn new(db: &'a mut Database) -> Self {
|
||||
Self { db }
|
||||
pub fn new(connection: &'a mut SqliteConnection) -> Self {
|
||||
Self { connection }
|
||||
}
|
||||
|
||||
pub fn insert_conversation(&mut self, conversation: Conversation) -> Result<()> {
|
||||
@@ -37,25 +36,25 @@ impl<'a> Repository<'a> {
|
||||
|
||||
diesel::replace_into(conversations)
|
||||
.values(&db_conversation)
|
||||
.execute(&mut self.db.connection)?;
|
||||
.execute(self.connection)?;
|
||||
|
||||
diesel::replace_into(participants)
|
||||
.values(&db_participants)
|
||||
.execute(&mut self.db.connection)?;
|
||||
.execute(self.connection)?;
|
||||
|
||||
// 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.connection)?;
|
||||
.first::<i32>(self.connection)?;
|
||||
|
||||
diesel::replace_into(conversation_participants)
|
||||
.values((
|
||||
conversation_id.eq(&db_conversation.id),
|
||||
participant_id.eq(pid),
|
||||
))
|
||||
.execute(&mut self.db.connection)?;
|
||||
.execute(self.connection)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -67,14 +66,14 @@ impl<'a> Repository<'a> {
|
||||
|
||||
let result = conversations
|
||||
.find(match_guid)
|
||||
.first::<ConversationRecord>(&mut self.db.connection)
|
||||
.first::<ConversationRecord>(self.connection)
|
||||
.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.connection)?;
|
||||
.load::<ParticipantRecord>(self.connection)?;
|
||||
|
||||
let mut model_conversation: Conversation = conversation.into();
|
||||
model_conversation.participants = db_participants.into_iter().map(|p| p.into()).collect();
|
||||
@@ -90,14 +89,14 @@ impl<'a> Repository<'a> {
|
||||
use crate::schema::participants::dsl::*;
|
||||
|
||||
let db_conversations = conversations
|
||||
.load::<ConversationRecord>(&mut self.db.connection)?;
|
||||
.load::<ConversationRecord>(self.connection)?;
|
||||
|
||||
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.connection)?;
|
||||
.load::<ParticipantRecord>(self.connection)?;
|
||||
|
||||
let mut model_conversation: Conversation = db_conversation.into();
|
||||
model_conversation.participants = db_participants.into_iter().map(|p| p.into()).collect();
|
||||
@@ -119,14 +118,14 @@ impl<'a> Repository<'a> {
|
||||
|
||||
diesel::replace_into(messages)
|
||||
.values(&db_message)
|
||||
.execute(&mut self.db.connection)?;
|
||||
.execute(self.connection)?;
|
||||
|
||||
diesel::replace_into(conversation_messages)
|
||||
.values((
|
||||
conversation_id.eq(conversation_guid),
|
||||
message_id.eq(&db_message.id),
|
||||
))
|
||||
.execute(&mut self.db.connection)?;
|
||||
.execute(self.connection)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -141,7 +140,7 @@ impl<'a> Repository<'a> {
|
||||
.inner_join(messages)
|
||||
.select(MessageRecord::as_select())
|
||||
.order_by(schema::messages::date.asc())
|
||||
.load::<MessageRecord>(&mut self.db.connection)?;
|
||||
.load::<MessageRecord>(self.connection)?;
|
||||
|
||||
let mut result = Vec::new();
|
||||
for message_record in message_records {
|
||||
@@ -151,7 +150,7 @@ impl<'a> Repository<'a> {
|
||||
if let Some(pid) = message_record.sender_participant_id {
|
||||
let participant = participants
|
||||
.find(pid)
|
||||
.first::<ParticipantRecord>(&mut self.db.connection)?;
|
||||
.first::<ParticipantRecord>(self.connection)?;
|
||||
message.sender = participant.into();
|
||||
}
|
||||
|
||||
@@ -166,7 +165,7 @@ impl<'a> Repository<'a> {
|
||||
// Huge caveat with this is that it depends on whatever the last insert was, prevents concurrent inserts.
|
||||
fn last_insert_id(&mut self) -> Result<i32> {
|
||||
Ok(diesel::select(diesel::dsl::sql::<diesel::sql_types::Integer>("last_insert_rowid()"))
|
||||
.get_result(&mut self.db.connection)?)
|
||||
.get_result(self.connection)?)
|
||||
}
|
||||
|
||||
fn get_or_create_participant(&mut self, participant: &Participant) -> Option<i32> {
|
||||
@@ -177,7 +176,7 @@ impl<'a> Repository<'a> {
|
||||
|
||||
let existing_participant = participants
|
||||
.filter(display_name.eq(p_name))
|
||||
.first::<ParticipantRecord>(&mut self.db.connection)
|
||||
.first::<ParticipantRecord>(self.connection)
|
||||
.optional()
|
||||
.unwrap();
|
||||
|
||||
@@ -192,7 +191,7 @@ impl<'a> Repository<'a> {
|
||||
|
||||
diesel::insert_into(participants)
|
||||
.values(&participant_record)
|
||||
.execute(&mut self.db.connection)
|
||||
.execute(self.connection)
|
||||
.unwrap();
|
||||
|
||||
self.last_insert_id().ok()
|
||||
|
||||
Reference in New Issue
Block a user