-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: [#157] extract service: settings
Decoupling services from actix-web framework.
- Loading branch information
1 parent
c9fb249
commit 0387b97
Showing
6 changed files
with
131 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod about; | ||
pub mod category; | ||
pub mod proxy; | ||
pub mod settings; | ||
pub mod user; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
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, | ||
} | ||
} | ||
|
||
/// It gets all the settings. | ||
/// | ||
/// # Errors | ||
/// | ||
/// It returns an error if the user does not have the required permissions. | ||
pub async fn get_all(&self, user_id: &UserId) -> Result<TorrustBackend, ServiceError> { | ||
let user = self.user_repository.get_compact_user(user_id).await?; | ||
|
||
// Check if user is administrator | ||
// todo: extract authorization service | ||
if !user.administrator { | ||
return Err(ServiceError::Unauthorized); | ||
} | ||
|
||
Ok(self.configuration.get_all().await) | ||
} | ||
|
||
/// It updates all the settings. | ||
/// | ||
/// # Errors | ||
/// | ||
/// It returns an error if the user does not have the required permissions. | ||
pub async fn update_all(&self, torrust_backend: TorrustBackend, user_id: &UserId) -> Result<TorrustBackend, ServiceError> { | ||
let user = self.user_repository.get_compact_user(user_id).await?; | ||
|
||
// Check if user is administrator | ||
// todo: extract authorization service | ||
if !user.administrator { | ||
return Err(ServiceError::Unauthorized); | ||
} | ||
|
||
let _ = self.configuration.update_settings(torrust_backend).await; | ||
|
||
Ok(self.configuration.get_all().await) | ||
} | ||
|
||
/// It gets only the public settings. | ||
/// | ||
/// # Errors | ||
/// | ||
/// It returns an error if the user does not have the required permissions. | ||
pub async fn get_public(&self) -> ConfigurationPublic { | ||
self.configuration.get_public().await | ||
} | ||
|
||
/// It gets the site name from the settings. | ||
/// | ||
/// # Errors | ||
/// | ||
/// It returns an error if the user does not have the required permissions. | ||
pub async fn get_site_name(&self) -> String { | ||
self.configuration.get_site_name().await | ||
} | ||
} |