From f7de451e35c3270a591ec4427eac404094075d6b Mon Sep 17 00:00:00 2001 From: Mario Date: Tue, 30 Jan 2024 01:01:14 +0100 Subject: [PATCH] refactor: [#445] implemented new extractor in handlers --- src/web/api/v1/contexts/category/handlers.rs | 18 ++++------------ src/web/api/v1/contexts/settings/handlers.rs | 12 +++++------ src/web/api/v1/contexts/tag/handlers.rs | 16 +++----------- src/web/api/v1/contexts/torrent/handlers.rs | 22 ++++---------------- src/web/api/v1/contexts/user/handlers.rs | 9 ++------ 5 files changed, 18 insertions(+), 59 deletions(-) diff --git a/src/web/api/v1/contexts/category/handlers.rs b/src/web/api/v1/contexts/category/handlers.rs index da0c1209..8265e2e1 100644 --- a/src/web/api/v1/contexts/category/handlers.rs +++ b/src/web/api/v1/contexts/category/handlers.rs @@ -8,7 +8,7 @@ use axum::response::{IntoResponse, Json, Response}; use super::forms::{AddCategoryForm, DeleteCategoryForm}; use super::responses::{added_category, deleted_category}; use crate::common::AppData; -use crate::web::api::v1::extractors::bearer_token::Extract; +use crate::web::api::v1::extractors::user_id::ExtractLoggedInUser; use crate::web::api::v1::responses::{self}; /// It handles the request to get all the categories. @@ -43,14 +43,9 @@ pub async fn get_all_handler(State(app_data): State>) -> Response { #[allow(clippy::unused_async)] pub async fn add_handler( State(app_data): State>, - Extract(maybe_bearer_token): Extract, + ExtractLoggedInUser(user_id): ExtractLoggedInUser, extract::Json(category_form): extract::Json, ) -> Response { - let user_id = match app_data.auth.get_user_id_from_bearer_token(&maybe_bearer_token).await { - Ok(user_id) => user_id, - Err(error) => return error.into_response(), - }; - match app_data.category_service.add_category(&category_form.name, &user_id).await { Ok(_) => added_category(&category_form.name).into_response(), Err(error) => error.into_response(), @@ -68,17 +63,12 @@ pub async fn add_handler( #[allow(clippy::unused_async)] pub async fn delete_handler( State(app_data): State>, - Extract(maybe_bearer_token): Extract, + ExtractLoggedInUser(user_id): ExtractLoggedInUser, extract::Json(category_form): extract::Json, ) -> Response { // code-review: why do we need to send the whole category object to delete it? // And we should use the ID instead of the name, because the name could change - // or we could add support for multiple languages. - - let user_id = match app_data.auth.get_user_id_from_bearer_token(&maybe_bearer_token).await { - Ok(user_id) => user_id, - Err(error) => return error.into_response(), - }; + // or we could add support for multiple languages. let user_id = match app_data.auth.get_user_id_from_bearer_token(&maybe_bearer_token).await { match app_data.category_service.delete_category(&category_form.name, &user_id).await { Ok(()) => deleted_category(&category_form.name).into_response(), diff --git a/src/web/api/v1/contexts/settings/handlers.rs b/src/web/api/v1/contexts/settings/handlers.rs index f4d94541..b6878f27 100644 --- a/src/web/api/v1/contexts/settings/handlers.rs +++ b/src/web/api/v1/contexts/settings/handlers.rs @@ -6,7 +6,7 @@ use axum::extract::State; use axum::response::{IntoResponse, Json, Response}; use crate::common::AppData; -use crate::web::api::v1::extractors::bearer_token::Extract; +use crate::web::api::v1::extractors::user_id::ExtractLoggedInUser; use crate::web::api::v1::responses; /// Get all settings. @@ -16,12 +16,10 @@ use crate::web::api::v1::responses; /// 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>, Extract(maybe_bearer_token): Extract) -> Response { - let user_id = match app_data.auth.get_user_id_from_bearer_token(&maybe_bearer_token).await { - Ok(user_id) => user_id, - Err(error) => return error.into_response(), - }; - +pub async fn get_all_handler( + State(app_data): State>, + ExtractLoggedInUser(user_id): ExtractLoggedInUser, +) -> Response { let all_settings = match app_data.settings_service.get_all(&user_id).await { Ok(all_settings) => all_settings, Err(error) => return error.into_response(), diff --git a/src/web/api/v1/contexts/tag/handlers.rs b/src/web/api/v1/contexts/tag/handlers.rs index f750c385..0413495e 100644 --- a/src/web/api/v1/contexts/tag/handlers.rs +++ b/src/web/api/v1/contexts/tag/handlers.rs @@ -8,7 +8,7 @@ use axum::response::{IntoResponse, Json, Response}; use super::forms::{AddTagForm, DeleteTagForm}; use super::responses::{added_tag, deleted_tag}; use crate::common::AppData; -use crate::web::api::v1::extractors::bearer_token::Extract; +use crate::web::api::v1::extractors::user_id::ExtractLoggedInUser; use crate::web::api::v1::responses::{self}; /// It handles the request to get all the tags. @@ -43,14 +43,9 @@ pub async fn get_all_handler(State(app_data): State>) -> Response { #[allow(clippy::unused_async)] pub async fn add_handler( State(app_data): State>, - Extract(maybe_bearer_token): Extract, + ExtractLoggedInUser(user_id): ExtractLoggedInUser, extract::Json(add_tag_form): extract::Json, ) -> Response { - let user_id = match app_data.auth.get_user_id_from_bearer_token(&maybe_bearer_token).await { - Ok(user_id) => user_id, - Err(error) => return error.into_response(), - }; - match app_data.tag_service.add_tag(&add_tag_form.name, &user_id).await { Ok(_) => added_tag(&add_tag_form.name).into_response(), Err(error) => error.into_response(), @@ -68,14 +63,9 @@ pub async fn add_handler( #[allow(clippy::unused_async)] pub async fn delete_handler( State(app_data): State>, - Extract(maybe_bearer_token): Extract, + ExtractLoggedInUser(user_id): ExtractLoggedInUser, extract::Json(delete_tag_form): extract::Json, ) -> Response { - let user_id = match app_data.auth.get_user_id_from_bearer_token(&maybe_bearer_token).await { - Ok(user_id) => user_id, - Err(error) => return error.into_response(), - }; - match app_data.tag_service.delete_tag(&delete_tag_form.tag_id, &user_id).await { Ok(()) => deleted_tag(delete_tag_form.tag_id).into_response(), Err(error) => error.into_response(), diff --git a/src/web/api/v1/contexts/torrent/handlers.rs b/src/web/api/v1/contexts/torrent/handlers.rs index bab51443..a337257f 100644 --- a/src/web/api/v1/contexts/torrent/handlers.rs +++ b/src/web/api/v1/contexts/torrent/handlers.rs @@ -23,6 +23,7 @@ use crate::services::torrent_file::generate_random_torrent; use crate::utils::parse_torrent; use crate::web::api::v1::auth::get_optional_logged_in_user; use crate::web::api::v1::extractors::bearer_token::Extract; +use crate::web::api::v1::extractors::user_id::ExtractLoggedInUser; use crate::web::api::v1::responses::OkResponseData; use crate::web::api::v1::routes::API_VERSION_URL_PREFIX; @@ -37,14 +38,9 @@ use crate::web::api::v1::routes::API_VERSION_URL_PREFIX; #[allow(clippy::unused_async)] pub async fn upload_torrent_handler( State(app_data): State>, - Extract(maybe_bearer_token): Extract, + ExtractLoggedInUser(user_id): ExtractLoggedInUser, multipart: Multipart, ) -> Response { - let user_id = match app_data.auth.get_user_id_from_bearer_token(&maybe_bearer_token).await { - Ok(user_id) => user_id, - Err(error) => return error.into_response(), - }; - let add_torrent_form = match build_add_torrent_request_from_payload(multipart).await { Ok(torrent_request) => torrent_request, Err(error) => return error.into_response(), @@ -220,7 +216,7 @@ async fn redirect_to_details_url_using_canonical_info_hash_if_needed( #[allow(clippy::unused_async)] pub async fn update_torrent_info_handler( State(app_data): State>, - Extract(maybe_bearer_token): Extract, + ExtractLoggedInUser(user_id): ExtractLoggedInUser, Path(info_hash): Path, extract::Json(update_torrent_info_form): extract::Json, ) -> Response { @@ -228,11 +224,6 @@ pub async fn update_torrent_info_handler( return errors::Request::InvalidInfoHashParam.into_response(); }; - let user_id = match app_data.auth.get_user_id_from_bearer_token(&maybe_bearer_token).await { - Ok(user_id) => user_id, - Err(error) => return error.into_response(), - }; - match app_data .torrent_service .update_torrent_info( @@ -262,18 +253,13 @@ pub async fn update_torrent_info_handler( #[allow(clippy::unused_async)] pub async fn delete_torrent_handler( State(app_data): State>, - Extract(maybe_bearer_token): Extract, + ExtractLoggedInUser(user_id): ExtractLoggedInUser, Path(info_hash): Path, ) -> Response { let Ok(info_hash) = InfoHash::from_str(&info_hash.lowercase()) else { return errors::Request::InvalidInfoHashParam.into_response(); }; - let user_id = match app_data.auth.get_user_id_from_bearer_token(&maybe_bearer_token).await { - Ok(user_id) => user_id, - Err(error) => return error.into_response(), - }; - match app_data.torrent_service.delete_torrent(&info_hash, &user_id).await { Ok(deleted_torrent_response) => Json(OkResponseData { data: deleted_torrent_response, diff --git a/src/web/api/v1/contexts/user/handlers.rs b/src/web/api/v1/contexts/user/handlers.rs index 170bd073..0992ed06 100644 --- a/src/web/api/v1/contexts/user/handlers.rs +++ b/src/web/api/v1/contexts/user/handlers.rs @@ -10,7 +10,7 @@ use serde::Deserialize; use super::forms::{JsonWebToken, LoginForm, RegistrationForm}; use super::responses::{self}; use crate::common::AppData; -use crate::web::api::v1::extractors::bearer_token::Extract; +use crate::web::api::v1::extractors::user_id::ExtractLoggedInUser; use crate::web::api::v1::responses::OkResponseData; // Registration @@ -135,15 +135,10 @@ pub async fn renew_token_handler( pub async fn ban_handler( State(app_data): State>, Path(to_be_banned_username): Path, - Extract(maybe_bearer_token): Extract, + ExtractLoggedInUser(user_id): ExtractLoggedInUser, ) -> Response { // todo: add reason and `date_expiry` parameters to request - let user_id = match app_data.auth.get_user_id_from_bearer_token(&maybe_bearer_token).await { - Ok(user_id) => user_id, - Err(error) => return error.into_response(), - }; - match app_data.ban_service.ban_user(&to_be_banned_username.0, &user_id).await { Ok(()) => Json(OkResponseData { data: format!("Banned user: {}", to_be_banned_username.0),