From b998a16e5d165300649718e73044793025417272 Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Fri, 16 Jun 2023 12:28:25 +0100 Subject: [PATCH] refactor(api): [#182] Axum API, torrent context, search for torrents --- src/web/api/v1/contexts/torrent/handlers.rs | 13 ++++++++++++- src/web/api/v1/contexts/torrent/routes.rs | 9 +++++++-- src/web/api/v1/routes.rs | 8 +++++--- tests/e2e/contexts/torrent/contract.rs | 7 ++++--- 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/web/api/v1/contexts/torrent/handlers.rs b/src/web/api/v1/contexts/torrent/handlers.rs index fd0f945f..aaf6043f 100644 --- a/src/web/api/v1/contexts/torrent/handlers.rs +++ b/src/web/api/v1/contexts/torrent/handlers.rs @@ -4,8 +4,9 @@ use std::io::{Cursor, Write}; use std::str::FromStr; use std::sync::Arc; -use axum::extract::{Multipart, Path, State}; +use axum::extract::{Multipart, Path, Query, State}; use axum::response::{IntoResponse, Response}; +use axum::Json; use serde::Deserialize; use super::responses::{new_torrent_response, torrent_file_response}; @@ -16,8 +17,10 @@ use crate::models::torrent::TorrentRequest; use crate::models::torrent_tag::TagId; use crate::models::user::UserId; use crate::routes::torrent::Create; +use crate::services::torrent::ListingRequest; use crate::utils::parse_torrent; use crate::web::api::v1::extractors::bearer_token::{BearerToken, Extract}; +use crate::web::api::v1::responses::OkResponseData; /// Upload a new torrent file to the Index /// @@ -77,6 +80,14 @@ pub async fn download_torrent_handler( torrent_file_response(bytes) } +#[allow(clippy::unused_async)] +pub async fn get_torrents_handler(State(app_data): State>, Query(criteria): Query) -> Response { + match app_data.torrent_service.generate_torrent_info_listing(&criteria).await { + Ok(torrents_response) => Json(OkResponseData { data: torrents_response }).into_response(), + Err(err) => err.into_response(), + } +} + /// If the user is logged in, returns the user's ID. Otherwise, returns `None`. /// /// # Errors diff --git a/src/web/api/v1/contexts/torrent/routes.rs b/src/web/api/v1/contexts/torrent/routes.rs index 0130872f..5618ef5b 100644 --- a/src/web/api/v1/contexts/torrent/routes.rs +++ b/src/web/api/v1/contexts/torrent/routes.rs @@ -6,12 +6,17 @@ use std::sync::Arc; use axum::routing::{get, post}; use axum::Router; -use super::handlers::{download_torrent_handler, upload_torrent_handler}; +use super::handlers::{download_torrent_handler, get_torrents_handler, upload_torrent_handler}; use crate::common::AppData; -/// Routes for the [`torrent`](crate::web::api::v1::contexts::torrent) API context. +/// Routes for the [`torrent`](crate::web::api::v1::contexts::torrent) API context for single resources. pub fn router_for_single_resources(app_data: Arc) -> Router { Router::new() .route("/upload", post(upload_torrent_handler).with_state(app_data.clone())) .route("/download/:info_hash", get(download_torrent_handler).with_state(app_data)) } + +/// Routes for the [`torrent`](crate::web::api::v1::contexts::torrent) API context for multiple resources. +pub fn router_for_multiple_resources(app_data: Arc) -> Router { + Router::new().route("/", get(get_torrents_handler).with_state(app_data)) +} diff --git a/src/web/api/v1/routes.rs b/src/web/api/v1/routes.rs index 10c27d20..2667bf1b 100644 --- a/src/web/api/v1/routes.rs +++ b/src/web/api/v1/routes.rs @@ -24,15 +24,17 @@ pub fn router(app_data: Arc) -> Router { .nest("/tag", tag::routes::router_for_single_resources(app_data.clone())) .nest("/tags", tag::routes::router_for_multiple_resources(app_data.clone())) .nest("/settings", settings::routes::router(app_data.clone())) - .nest("/torrent", torrent::routes::router_for_single_resources(app_data.clone())); + .nest("/torrent", torrent::routes::router_for_single_resources(app_data.clone())) + .nest("/torrents", torrent::routes::router_for_multiple_resources(app_data.clone())); Router::new() .route("/", get(about_page_handler).with_state(app_data)) .nest("/v1", v1_api_routes) - // For development purposes only. + // + //.layer(CorsLayer::permissive()) // Uncomment this line and the `use` import. + // // It allows calling the API on a different port. For example // API: http://localhost:3000/v1 // Webapp: http://localhost:8080 - //Router::new().nest("/v1", api_routes).layer(CorsLayer::permissive()) } diff --git a/tests/e2e/contexts/torrent/contract.rs b/tests/e2e/contexts/torrent/contract.rs index 474c6011..2f35786c 100644 --- a/tests/e2e/contexts/torrent/contract.rs +++ b/tests/e2e/contexts/torrent/contract.rs @@ -635,18 +635,17 @@ mod with_axum_implementation { //use crate::common::contexts::category::fixtures::software_predefined_category_id; //use crate::common::contexts::torrent::asserts::assert_expected_torrent_details; use crate::common::contexts::torrent::requests::InfoHash; + use crate::common::contexts::torrent::responses::TorrentListResponse; //use crate::common::contexts::torrent::responses::{ // Category, File, TorrentDetails, TorrentDetailsResponse, TorrentListResponse, //}; - //use crate::common::http::{Query, QueryParam}; + use crate::common::http::{Query, QueryParam}; use crate::e2e::config::ENV_VAR_E2E_EXCLUDE_AXUM_IMPL; use crate::e2e::contexts::torrent::asserts::expected_torrent; use crate::e2e::contexts::torrent::steps::upload_random_torrent_to_index; use crate::e2e::contexts::user::steps::new_logged_in_user; use crate::e2e::environment::TestEnv; - /* - #[tokio::test] async fn it_should_allow_guests_to_get_torrents() { let mut env = TestEnv::new(); @@ -786,6 +785,8 @@ mod with_axum_implementation { assert!(response.is_json_and_ok()); } + /* + #[tokio::test] async fn it_should_allow_guests_to_get_torrent_details_searching_by_info_hash() { let mut env = TestEnv::new();