use std::sync::Arc;
use super::user::DbUserRepository;
use crate::config::{Configuration, ConfigurationPublic, TorrustBackend};
use crate::errors::ServiceError;
use crate::models::user::UserId;
pub struct Service {
configuration: Arc<Configuration>,
user_repository: Arc<DbUserRepository>,
}
impl Service {
#[must_use]
pub fn new(configuration: Arc<Configuration>, user_repository: Arc<DbUserRepository>) -> Service {
Service {
configuration,
user_repository,
}
}
pub async fn get_all(&self, user_id: &UserId) -> Result<TorrustBackend, ServiceError> {
let user = self.user_repository.get_compact(user_id).await?;
if !user.administrator {
return Err(ServiceError::Unauthorized);
}
Ok(self.configuration.get_all().await)
}
pub async fn get_public(&self) -> ConfigurationPublic {
self.configuration.get_public().await
}
pub async fn get_site_name(&self) -> String {
self.configuration.get_site_name().await
}
}