use crate::common::errors::ServiceError; use crate::dictionary::{Dict, DictEntry, DictRepositoryFactory}; use std::sync::Arc; #[derive(Debug, Clone)] pub struct DictionarySummary { pub name: String, pub entry_count: u64, } pub struct DictionaryService { repo_factory: Arc, } impl DictionaryService { pub fn new(repo_factory: Arc) -> Self { Self { repo_factory } } pub async fn list_dictionaries(&self) -> Result, ServiceError> { let dict_names = self.repo_factory.list_all().await?; let mut summaries = Vec::with_capacity(dict_names.len()); for dict_name in dict_names { let repo = self.repo_factory.create(&dict_name).await?; let entry_count = repo.count_entries().await?; summaries.push(DictionarySummary { name: dict_name, entry_count, }); } Ok(summaries) } pub async fn get_dictionary( &self, name: &str, limit: usize, offset: usize, ) -> Result { let repo = self.repo_factory.create(name).await?; repo.fetch_many(limit, offset).await.map_err(Into::into) } pub async fn get_entry_count(&self, name: &str) -> Result { let repo = self.repo_factory.create(name).await?; repo.count_entries().await.map_err(Into::into) } pub async fn create_dictionary(&self, name: &str) -> Result<(), ServiceError> { let repo = self.repo_factory.create(name).await?; repo.create_dict().await.map_err(Into::into) } pub async fn save_entries( &self, name: &str, entries: &[DictEntry], ) -> Result<(), ServiceError> { let repo = self.repo_factory.create(name).await?; repo.save_entries(entries).await.map_err(Into::into) } }