diff --git a/src/models/torrent.rs b/src/models/torrent.rs index 75e0d805..2ecbf984 100644 --- a/src/models/torrent.rs +++ b/src/models/torrent.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; use crate::models::torrent_file::Torrent; -use crate::routes::torrent::CreateTorrent; +use crate::routes::torrent::Create; #[allow(clippy::module_name_repetitions)] #[allow(dead_code)] @@ -22,6 +22,6 @@ pub struct TorrentListing { #[allow(clippy::module_name_repetitions)] #[derive(Debug)] pub struct TorrentRequest { - pub fields: CreateTorrent, + pub fields: Create, pub torrent: Torrent, } diff --git a/src/routes/torrent.rs b/src/routes/torrent.rs index 32ad44df..0f121fd4 100644 --- a/src/routes/torrent.rs +++ b/src/routes/torrent.rs @@ -20,42 +20,47 @@ use crate::AsCSV; pub fn init(cfg: &mut web::ServiceConfig) { cfg.service( web::scope("/torrent") - .service(web::resource("/upload").route(web::post().to(upload_torrent))) + .service(web::resource("/upload").route(web::post().to(upload))) .service(web::resource("/download/{info_hash}").route(web::get().to(download_torrent_handler))) .service( web::resource("/{info_hash}") - .route(web::get().to(get_torrent_handler)) - .route(web::put().to(update_torrent_handler)) - .route(web::delete().to(delete_torrent_handler)), + .route(web::get().to(get)) + .route(web::put().to(update)) + .route(web::delete().to(delete)), ), ); cfg.service(web::scope("/torrents").service(web::resource("").route(web::get().to(get_torrents_handler)))); } #[derive(FromRow)] -pub struct TorrentCount { +pub struct Count { pub count: i32, } #[derive(Debug, Deserialize)] -pub struct CreateTorrent { +pub struct Create { pub title: String, pub description: String, pub category: String, } -impl CreateTorrent { +impl Create { + /// Returns the verify of this [`Create`]. + /// + /// # Errors + /// + /// This function will return an `BadRequest` error if the `title` or the `category` is empty. pub fn verify(&self) -> Result<(), ServiceError> { - if !self.title.is_empty() && !self.category.is_empty() { - return Ok(()); + if self.title.is_empty() || self.category.is_empty() { + Err(ServiceError::BadRequest) + } else { + Ok(()) } - - Err(ServiceError::BadRequest) } } #[derive(Debug, Deserialize)] -pub struct TorrentSearch { +pub struct Search { page_size: Option, page: Option, sort: Option, @@ -65,12 +70,21 @@ pub struct TorrentSearch { } #[derive(Debug, Deserialize)] -pub struct TorrentUpdate { +pub struct Update { title: Option, description: Option, } -pub async fn upload_torrent(req: HttpRequest, payload: Multipart, app_data: WebAppData) -> ServiceResult { +/// Upload a Torrent to the Index +/// +/// # Errors +/// +/// This function will return an error if unable to get the user from the database. +/// This function will return an error if unable to get torrent request from payload. +/// This function will return an error if unable to get the category from the database. +/// This function will return an error if unable to insert the torrent into the database. +/// This function will return an error if unable to add the torrent to the whitelist. +pub async fn upload(req: HttpRequest, payload: Multipart, app_data: WebAppData) -> ServiceResult { let user = app_data.auth.get_user_compact_from_request(&req).await?; // get torrent and fields from request @@ -166,7 +180,17 @@ pub async fn download_torrent_handler(req: HttpRequest, app_data: WebAppData) -> Ok(HttpResponse::Ok().content_type("application/x-bittorrent").body(buffer)) } -pub async fn get_torrent_handler(req: HttpRequest, app_data: WebAppData) -> ServiceResult { +/// Get Torrent from the Index +/// +/// # Errors +/// +/// This function will return an error if unable to get torrent ID. +/// This function will return an error if unable to get torrent listing from id. +/// This function will return an error if unable to get torrent category from id. +/// This function will return an error if unable to get torrent files from id. +/// This function will return an error if unable to get torrent info from id. +/// This function will return an error if unable to get torrent announce url(s) from id. +pub async fn get(req: HttpRequest, app_data: WebAppData) -> ServiceResult { // optional let user = app_data.auth.get_user_compact_from_request(&req).await; @@ -251,11 +275,16 @@ pub async fn get_torrent_handler(req: HttpRequest, app_data: WebAppData) -> Serv Ok(HttpResponse::Ok().json(OkResponse { data: torrent_response })) } -pub async fn update_torrent_handler( - req: HttpRequest, - payload: web::Json, - app_data: WebAppData, -) -> ServiceResult { +/// Update a Torrent in the Index +/// +/// # Errors +/// +/// This function will return an error if unable to get user. +/// This function will return an error if unable to get torrent id from request. +/// This function will return an error if unable to get listing from id. +/// This function will return an `ServiceError::Unauthorized` if user is not a owner or an administrator. +/// This function will return an error if unable to update the torrent tile or description. +pub async fn update(req: HttpRequest, payload: web::Json, app_data: WebAppData) -> ServiceResult { let user = app_data.auth.get_user_compact_from_request(&req).await?; let infohash = get_torrent_infohash_from_request(&req)?; @@ -293,7 +322,15 @@ pub async fn update_torrent_handler( Ok(HttpResponse::Ok().json(OkResponse { data: torrent_response })) } -pub async fn delete_torrent_handler(req: HttpRequest, app_data: WebAppData) -> ServiceResult { +/// Delete a Torrent from the Index +/// +/// # Errors +/// +/// This function will return an error if unable to get the user. +/// This function will return an `ServiceError::Unauthorized` if the user is not an administrator. +/// This function will return an error if unable to get the torrent listing from it's ID. +/// This function will return an error if unable to delete the torrent from the database. +pub async fn delete(req: HttpRequest, app_data: WebAppData) -> ServiceResult { let user = app_data.auth.get_user_compact_from_request(&req).await?; // check if user is administrator @@ -327,7 +364,7 @@ pub async fn delete_torrent_handler(req: HttpRequest, app_data: WebAppData) -> S /// # Errors /// /// Returns a `ServiceError::DatabaseError` if the database query fails. -pub async fn get_torrents_handler(params: Query, app_data: WebAppData) -> ServiceResult { +pub async fn get_torrents_handler(params: Query, app_data: WebAppData) -> ServiceResult { let settings = app_data.cfg.settings.read().await; let sort = params.sort.unwrap_or(Sorting::UploadedDesc); @@ -405,7 +442,7 @@ async fn get_torrent_request_from_payload(mut payload: Multipart) -> Result Result