Skip to content

Commit 24394ea

Browse files
committed
refactor(api): [#182] Axum API, torrent context, delete torrent info
1 parent ca257ff commit 24394ea

File tree

3 files changed

+37
-17
lines changed

3 files changed

+37
-17
lines changed

src/web/api/v1/contexts/torrent/handlers.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,37 @@ pub async fn update_torrent_info_handler(
167167
}
168168
}
169169

170+
/// Delete a torrent.
171+
///
172+
/// # Errors
173+
///
174+
/// This function will return an error if unable to:
175+
///
176+
/// * Get the user ID from the request.
177+
/// * Get the torrent info-hash from the request.
178+
/// * Delete the torrent.
179+
#[allow(clippy::unused_async)]
180+
pub async fn delete_torrent_handler(
181+
State(app_data): State<Arc<AppData>>,
182+
Extract(maybe_bearer_token): Extract,
183+
Path(info_hash): Path<InfoHashParam>,
184+
) -> Response {
185+
let Ok(info_hash) = InfoHash::from_str(&info_hash.0) else { return ServiceError::BadRequest.into_response() };
186+
187+
let user_id = match app_data.auth.get_user_id_from_bearer_token(&maybe_bearer_token).await {
188+
Ok(user_id) => user_id,
189+
Err(err) => return err.into_response(),
190+
};
191+
192+
match app_data.torrent_service.delete_torrent(&info_hash, &user_id).await {
193+
Ok(deleted_torrent_response) => Json(OkResponseData {
194+
data: deleted_torrent_response,
195+
})
196+
.into_response(),
197+
Err(err) => err.into_response(),
198+
}
199+
}
200+
170201
/// If the user is logged in, returns the user's ID. Otherwise, returns `None`.
171202
///
172203
/// # Errors

src/web/api/v1/contexts/torrent/routes.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,21 @@
33
//! Refer to the [API endpoint documentation](crate::web::api::v1::contexts::torrent).
44
use std::sync::Arc;
55

6-
use axum::routing::{get, post, put};
6+
use axum::routing::{delete, get, post, put};
77
use axum::Router;
88

99
use super::handlers::{
10-
download_torrent_handler, get_torrent_info_handler, get_torrents_handler, update_torrent_info_handler, upload_torrent_handler,
10+
delete_torrent_handler, download_torrent_handler, get_torrent_info_handler, get_torrents_handler,
11+
update_torrent_info_handler, upload_torrent_handler,
1112
};
1213
use crate::common::AppData;
1314

1415
/// Routes for the [`torrent`](crate::web::api::v1::contexts::torrent) API context for single resources.
1516
pub fn router_for_single_resources(app_data: Arc<AppData>) -> Router {
1617
let torrent_info_routes = Router::new()
1718
.route("/", get(get_torrent_info_handler).with_state(app_data.clone()))
18-
.route("/", put(update_torrent_info_handler).with_state(app_data.clone()));
19+
.route("/", put(update_torrent_info_handler).with_state(app_data.clone()))
20+
.route("/", delete(delete_torrent_handler).with_state(app_data.clone()));
1921

2022
Router::new()
2123
.route("/upload", post(upload_torrent_handler).with_state(app_data.clone()))

tests/e2e/contexts/torrent/contract.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -907,8 +907,6 @@ mod with_axum_implementation {
907907
assert_eq!(response.status, 400);
908908
}
909909

910-
/*
911-
912910
#[tokio::test]
913911
async fn it_should_not_allow_guests_to_delete_torrents() {
914912
let mut env = TestEnv::new();
@@ -933,8 +931,6 @@ mod with_axum_implementation {
933931

934932
assert_eq!(response.status, 401);
935933
}
936-
937-
*/
938934
}
939935

940936
mod for_authenticated_users {
@@ -1135,8 +1131,6 @@ mod with_axum_implementation {
11351131
use crate::e2e::contexts::user::steps::new_logged_in_user;
11361132
use crate::e2e::environment::TestEnv;
11371133

1138-
/*
1139-
11401134
#[tokio::test]
11411135
async fn it_should_not_allow_non_admins_to_delete_torrents() {
11421136
let mut env = TestEnv::new();
@@ -1162,8 +1156,6 @@ mod with_axum_implementation {
11621156
assert_eq!(response.status, 403);
11631157
}
11641158

1165-
*/
1166-
11671159
#[tokio::test]
11681160
async fn it_should_allow_non_admin_users_to_update_someone_elses_torrents() {
11691161
let mut env = TestEnv::new();
@@ -1269,15 +1261,12 @@ mod with_axum_implementation {
12691261

12701262
use crate::common::client::Client;
12711263
use crate::common::contexts::torrent::forms::UpdateTorrentFrom;
1272-
//use crate::common::contexts::torrent::responses::{DeletedTorrentResponse, UpdatedTorrentResponse};
1273-
use crate::common::contexts::torrent::responses::UpdatedTorrentResponse;
1264+
use crate::common::contexts::torrent::responses::{DeletedTorrentResponse, UpdatedTorrentResponse};
12741265
use crate::e2e::config::ENV_VAR_E2E_EXCLUDE_AXUM_IMPL;
12751266
use crate::e2e::contexts::torrent::steps::upload_random_torrent_to_index;
12761267
use crate::e2e::contexts::user::steps::{new_logged_in_admin, new_logged_in_user};
12771268
use crate::e2e::environment::TestEnv;
12781269

1279-
/*
1280-
12811270
#[tokio::test]
12821271
async fn it_should_allow_admins_to_delete_torrents_searching_by_info_hash() {
12831272
let mut env = TestEnv::new();
@@ -1307,8 +1296,6 @@ mod with_axum_implementation {
13071296
assert!(response.is_json_and_ok());
13081297
}
13091298

1310-
*/
1311-
13121299
#[tokio::test]
13131300
async fn it_should_allow_admins_to_update_someone_elses_torrents() {
13141301
let mut env = TestEnv::new();

0 commit comments

Comments
 (0)