Private
Public Access
1
0

refactor: with_repository/with_settings

This commit is contained in:
2025-04-25 16:34:00 -07:00
parent 89c9ffc187
commit b1f171136a
5 changed files with 249 additions and 227 deletions

View File

@@ -108,7 +108,9 @@ impl DbClient {
}
pub fn print_conversations(&mut self) -> Result<()> {
let all_conversations = self.database.get_repository().all_conversations()?;
let all_conversations = self.database.with_repository(|repository| {
repository.all_conversations()
})?;
println!("{} Conversations: ", all_conversations.len());
for conversation in all_conversations {
@@ -119,7 +121,10 @@ impl DbClient {
}
pub async fn print_messages(&mut self, conversation_id: &str) -> Result<()> {
let messages = self.database.get_repository().get_messages_for_conversation(conversation_id)?;
let messages = self.database.with_repository(|repository| {
repository.get_messages_for_conversation(conversation_id)
})?;
for message in messages {
println!("{}", MessagePrinter::new(&message.into()));
}
@@ -133,10 +138,14 @@ impl DbClient {
.map(|c| kordophone_db::models::Conversation::from(c))
.collect();
let mut repository = self.database.get_repository();
// Process each conversation
for conversation in db_conversations {
let conversation_id = conversation.guid.clone();
repository.insert_conversation(conversation)?;
// Insert the conversation
self.database.with_repository(|repository| {
repository.insert_conversation(conversation)
})?;
// Fetch and sync messages for this conversation
let messages = client.get_messages(&conversation_id).await?;
@@ -144,59 +153,66 @@ impl DbClient {
.map(|m| kordophone_db::models::Message::from(m))
.collect();
for message in db_messages {
repository.insert_message(&conversation_id, message)?;
}
// Insert each message
self.database.with_repository(|repository| -> Result<()> {
for message in db_messages {
repository.insert_message(&conversation_id, message)?;
}
Ok(())
})?;
}
Ok(())
}
pub fn get_setting(&mut self, key: Option<String>) -> Result<()> {
let mut settings = self.database.get_settings();
match key {
Some(key) => {
// Get a specific setting
let value: Option<String> = settings.get(&key)?;
match value {
Some(v) => println!("{} = {}", key, v),
None => println!("Setting '{}' not found", key),
}
},
None => {
// List all settings
let keys = settings.list_keys()?;
if keys.is_empty() {
println!("No settings found");
} else {
println!("Settings:");
for key in keys {
let value: Option<String> = settings.get(&key)?;
match value {
Some(v) => println!(" {} = {}", key, v),
None => println!(" {} = <error reading value>", key),
self.database.with_settings(|settings| {
match key {
Some(key) => {
// Get a specific setting
let value: Option<String> = settings.get(&key)?;
match value {
Some(v) => println!("{} = {}", key, v),
None => println!("Setting '{}' not found", key),
}
},
None => {
// List all settings
let keys = settings.list_keys()?;
if keys.is_empty() {
println!("No settings found");
} else {
println!("Settings:");
for key in keys {
let value: Option<String> = settings.get(&key)?;
match value {
Some(v) => println!(" {} = {}", key, v),
None => println!(" {} = <error reading value>", key),
}
}
}
}
}
}
Ok(())
Ok(())
})
}
pub fn put_setting(&mut self, key: String, value: String) -> Result<()> {
let mut settings = self.database.get_settings();
settings.put(&key, &value)?;
Ok(())
self.database.with_settings(|settings| {
settings.put(&key, &value)?;
Ok(())
})
}
pub fn delete_setting(&mut self, key: String) -> Result<()> {
let mut settings = self.database.get_settings();
let count = settings.del(&key)?;
if count == 0 {
println!("Setting '{}' not found", key);
}
Ok(())
self.database.with_settings(|settings| {
let count = settings.del(&key)?;
if count == 0 {
println!("Setting '{}' not found", key);
}
Ok(())
})
}
}