Skip to content

Commit

Permalink
refactor: [torrust#445] implemented new extractor in handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
mario-nt committed Jan 30, 2024
1 parent f23bea5 commit b2e3eb9
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 59 deletions.
18 changes: 4 additions & 14 deletions src/web/api/v1/contexts/category/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -43,14 +43,9 @@ pub async fn get_all_handler(State(app_data): State<Arc<AppData>>) -> Response {
#[allow(clippy::unused_async)]
pub async fn add_handler(
State(app_data): State<Arc<AppData>>,
Extract(maybe_bearer_token): Extract,
ExtractLoggedInUser(user_id): ExtractLoggedInUser,
extract::Json(category_form): extract::Json<AddCategoryForm>,
) -> 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(),
Expand All @@ -68,17 +63,12 @@ pub async fn add_handler(
#[allow(clippy::unused_async)]
pub async fn delete_handler(
State(app_data): State<Arc<AppData>>,
Extract(maybe_bearer_token): Extract,
ExtractLoggedInUser(user_id): ExtractLoggedInUser,
extract::Json(category_form): extract::Json<DeleteCategoryForm>,
) -> 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(),
Expand Down
12 changes: 5 additions & 7 deletions src/web/api/v1/contexts/settings/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<Arc<AppData>>, 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<Arc<AppData>>,
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(),
Expand Down
16 changes: 3 additions & 13 deletions src/web/api/v1/contexts/tag/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -43,14 +43,9 @@ pub async fn get_all_handler(State(app_data): State<Arc<AppData>>) -> Response {
#[allow(clippy::unused_async)]
pub async fn add_handler(
State(app_data): State<Arc<AppData>>,
Extract(maybe_bearer_token): Extract,
ExtractLoggedInUser(user_id): ExtractLoggedInUser,
extract::Json(add_tag_form): extract::Json<AddTagForm>,
) -> 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(),
Expand All @@ -68,14 +63,9 @@ pub async fn add_handler(
#[allow(clippy::unused_async)]
pub async fn delete_handler(
State(app_data): State<Arc<AppData>>,
Extract(maybe_bearer_token): Extract,
ExtractLoggedInUser(user_id): ExtractLoggedInUser,
extract::Json(delete_tag_form): extract::Json<DeleteTagForm>,
) -> 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(),
Expand Down
22 changes: 4 additions & 18 deletions src/web/api/v1/contexts/torrent/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<Arc<AppData>>,
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(),
Expand Down Expand Up @@ -220,19 +216,14 @@ 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<Arc<AppData>>,
Extract(maybe_bearer_token): Extract,
ExtractLoggedInUser(user_id): ExtractLoggedInUser,
Path(info_hash): Path<InfoHashParam>,
extract::Json(update_torrent_info_form): extract::Json<UpdateTorrentInfoForm>,
) -> 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
.update_torrent_info(
Expand Down Expand Up @@ -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<Arc<AppData>>,
Extract(maybe_bearer_token): Extract,
ExtractLoggedInUser(user_id): ExtractLoggedInUser,
Path(info_hash): Path<InfoHashParam>,
) -> 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,
Expand Down
9 changes: 2 additions & 7 deletions src/web/api/v1/contexts/user/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -135,15 +135,10 @@ pub async fn renew_token_handler(
pub async fn ban_handler(
State(app_data): State<Arc<AppData>>,
Path(to_be_banned_username): Path<UsernameParam>,
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),
Expand Down

0 comments on commit b2e3eb9

Please sign in to comment.