You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

66 lines
1.9 KiB

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<dyn DictRepositoryFactory>,
}
impl DictionaryService {
pub fn new(repo_factory: Arc<dyn DictRepositoryFactory>) -> Self {
Self { repo_factory }
}
pub async fn list_dictionaries(&self) -> Result<Vec<DictionarySummary>, 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<Dict, ServiceError> {
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<u64, ServiceError> {
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)
}
}