forked from torrust/torrust-index
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(api): [torrust#181] Axum API, settings contex
- Loading branch information
1 parent
0c26aaa
commit dc469c4
Showing
5 changed files
with
210 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
//! API handlers for the the [`category`](crate::web::api::v1::contexts::category) API | ||
//! context. | ||
use std::sync::Arc; | ||
|
||
use axum::extract::{self, State}; | ||
use axum::response::Json; | ||
|
||
use crate::common::AppData; | ||
use crate::config::{ConfigurationPublic, TorrustBackend}; | ||
use crate::errors::ServiceError; | ||
use crate::web::api::v1::extractors::bearer_token::Extract; | ||
use crate::web::api::v1::responses::{self, OkResponse}; | ||
|
||
/// Get all settings. | ||
/// | ||
/// # Errors | ||
/// | ||
/// This function will return an error if the user does not have permission to | ||
/// view all the settings. | ||
#[allow(clippy::unused_async)] | ||
pub async fn get_all_handler( | ||
State(app_data): State<Arc<AppData>>, | ||
Extract(maybe_bearer_token): Extract, | ||
) -> Result<Json<OkResponse<TorrustBackend>>, ServiceError> { | ||
let user_id = app_data.auth.get_user_id_from_bearer_token(&maybe_bearer_token).await?; | ||
|
||
let all_settings = app_data.settings_service.get_all(&user_id).await?; | ||
|
||
Ok(Json(responses::OkResponse { data: all_settings })) | ||
} | ||
|
||
/// Get public Settings. | ||
#[allow(clippy::unused_async)] | ||
pub async fn get_public_handler(State(app_data): State<Arc<AppData>>) -> Json<responses::OkResponse<ConfigurationPublic>> { | ||
let public_settings = app_data.settings_service.get_public().await; | ||
|
||
Json(responses::OkResponse { data: public_settings }) | ||
} | ||
|
||
/// Get website name. | ||
#[allow(clippy::unused_async)] | ||
pub async fn get_site_name_handler(State(app_data): State<Arc<AppData>>) -> Json<responses::OkResponse<String>> { | ||
let site_name = app_data.settings_service.get_site_name().await; | ||
|
||
Json(responses::OkResponse { data: site_name }) | ||
} | ||
|
||
/// Update all the settings. | ||
/// | ||
/// # Errors | ||
/// | ||
/// This function will return an error if: | ||
/// | ||
/// - The user does not have permission to update the settings. | ||
/// - The settings could not be updated because they were loaded from env vars. | ||
/// See <https://github.com/torrust/torrust-index-backend/issues/144.> | ||
#[allow(clippy::unused_async)] | ||
pub async fn update_handler( | ||
State(app_data): State<Arc<AppData>>, | ||
Extract(maybe_bearer_token): Extract, | ||
extract::Json(torrust_backend): extract::Json<TorrustBackend>, | ||
) -> Result<Json<OkResponse<TorrustBackend>>, ServiceError> { | ||
let user_id = app_data.auth.get_user_id_from_bearer_token(&maybe_bearer_token).await?; | ||
|
||
let new_settings = app_data.settings_service.update_all(torrust_backend, &user_id).await?; | ||
|
||
Ok(Json(responses::OkResponse { data: new_settings })) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
//! API routes for the [`settings`](crate::web::api::v1::contexts::settings) API context. | ||
//! | ||
//! Refer to the [API endpoint documentation](crate::web::api::v1::contexts::settings). | ||
use std::sync::Arc; | ||
|
||
use axum::routing::{get, post}; | ||
use axum::Router; | ||
|
||
use super::handlers::{get_all_handler, get_public_handler, get_site_name_handler, update_handler}; | ||
use crate::common::AppData; | ||
|
||
/// Routes for the [`category`](crate::web::api::v1::contexts::category) API context. | ||
pub fn router(app_data: Arc<AppData>) -> Router { | ||
Router::new() | ||
.route("/", get(get_all_handler).with_state(app_data.clone())) | ||
.route("/name", get(get_site_name_handler).with_state(app_data.clone())) | ||
.route("/public", get(get_public_handler).with_state(app_data.clone())) | ||
.route("/", post(update_handler).with_state(app_data)) | ||
} |
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