diff --git a/src/app.rs b/src/app.rs index 005616aa..1e6b421e 100644 --- a/src/app.rs +++ b/src/app.rs @@ -14,7 +14,10 @@ use crate::config::Configuration; use crate::databases::database; use crate::services::authentication::{DbUserAuthenticationRepository, JsonWebToken, Service}; use crate::services::category::{self, DbCategoryRepository}; -use crate::services::torrent::{DbTorrentAnnounceUrlRepository, DbTorrentFileRepository, DbTorrentInfoRepository, DbTorrentListingGenerator, DbTorrentRepository, DbTorrentTagRepository}; +use crate::services::torrent::{ + DbTorrentAnnounceUrlRepository, DbTorrentFileRepository, DbTorrentInfoRepository, DbTorrentListingGenerator, + DbTorrentRepository, DbTorrentTagRepository, +}; use crate::services::user::{self, DbBannedUserList, DbUserProfileRepository, DbUserRepository}; use crate::services::{proxy, settings, torrent}; use crate::tracker::statistics_importer::StatisticsImporter; diff --git a/src/common.rs b/src/common.rs index 9c13a40b..db0361e3 100644 --- a/src/common.rs +++ b/src/common.rs @@ -6,7 +6,10 @@ use crate::config::Configuration; use crate::databases::database::Database; use crate::services::authentication::{DbUserAuthenticationRepository, JsonWebToken, Service}; use crate::services::category::{self, DbCategoryRepository}; -use crate::services::torrent::{DbTorrentAnnounceUrlRepository, DbTorrentFileRepository, DbTorrentInfoRepository, DbTorrentListingGenerator, DbTorrentRepository, DbTorrentTagRepository}; +use crate::services::torrent::{ + DbTorrentAnnounceUrlRepository, DbTorrentFileRepository, DbTorrentInfoRepository, DbTorrentListingGenerator, + DbTorrentRepository, DbTorrentTagRepository, +}; use crate::services::user::{self, DbBannedUserList, DbUserProfileRepository, DbUserRepository}; use crate::services::{proxy, settings, torrent}; use crate::tracker::statistics_importer::StatisticsImporter; diff --git a/src/databases/mysql.rs b/src/databases/mysql.rs index e68533e6..925f502a 100644 --- a/src/databases/mysql.rs +++ b/src/databases/mysql.rs @@ -725,7 +725,9 @@ impl Database for Mysql { } async fn add_torrent_tag_links(&self, torrent_id: i64, tag_ids: &Vec) -> Result<(), database::Error> { - let mut transaction = self.pool.begin() + let mut transaction = self + .pool + .begin() .await .map_err(|err| database::Error::ErrorWithText(err.to_string()))?; @@ -738,7 +740,8 @@ impl Database for Mysql { .map_err(|err| database::Error::ErrorWithText(err.to_string()))?; } - transaction.commit() + transaction + .commit() .await .map_err(|err| database::Error::ErrorWithText(err.to_string())) } @@ -771,9 +774,7 @@ impl Database for Mysql { } async fn get_tags(&self) -> Result, database::Error> { - query_as::<_, TorrentTag>( - "SELECT tag_id, name FROM torrust_torrent_tags" - ) + query_as::<_, TorrentTag>("SELECT tag_id, name FROM torrust_torrent_tags") .fetch_all(&self.pool) .await .map_err(|_| database::Error::Error) @@ -784,12 +785,12 @@ impl Database for Mysql { "SELECT torrust_torrent_tags.tag_id, torrust_torrent_tags.name FROM torrust_torrent_tags JOIN torrust_torrent_tag_links ON torrust_torrent_tags.tag_id = torrust_torrent_tag_links.tag_id - WHERE torrust_torrent_tag_links.torrent_id = ?" + WHERE torrust_torrent_tag_links.torrent_id = ?", ) - .bind(torrent_id) - .fetch_all(&self.pool) - .await - .map_err(|_| database::Error::Error) + .bind(torrent_id) + .fetch_all(&self.pool) + .await + .map_err(|_| database::Error::Error) } async fn update_tracker_info( diff --git a/src/databases/sqlite.rs b/src/databases/sqlite.rs index d7ba9bb2..327ea0cd 100644 --- a/src/databases/sqlite.rs +++ b/src/databases/sqlite.rs @@ -715,7 +715,9 @@ impl Database for Sqlite { } async fn add_torrent_tag_links(&self, torrent_id: i64, tag_ids: &Vec) -> Result<(), database::Error> { - let mut transaction = self.pool.begin() + let mut transaction = self + .pool + .begin() .await .map_err(|err| database::Error::ErrorWithText(err.to_string()))?; @@ -728,7 +730,8 @@ impl Database for Sqlite { .map_err(|err| database::Error::ErrorWithText(err.to_string()))?; } - transaction.commit() + transaction + .commit() .await .map_err(|err| database::Error::ErrorWithText(err.to_string())) } @@ -761,9 +764,7 @@ impl Database for Sqlite { } async fn get_tags(&self) -> Result, database::Error> { - query_as::<_, TorrentTag>( - "SELECT tag_id, name FROM torrust_torrent_tags" - ) + query_as::<_, TorrentTag>("SELECT tag_id, name FROM torrust_torrent_tags") .fetch_all(&self.pool) .await .map_err(|_| database::Error::Error) @@ -774,12 +775,12 @@ impl Database for Sqlite { "SELECT torrust_torrent_tags.tag_id, torrust_torrent_tags.name FROM torrust_torrent_tags JOIN torrust_torrent_tag_links ON torrust_torrent_tags.tag_id = torrust_torrent_tag_links.tag_id - WHERE torrust_torrent_tag_links.torrent_id = ?" + WHERE torrust_torrent_tag_links.torrent_id = ?", ) - .bind(torrent_id) - .fetch_all(&self.pool) - .await - .map_err(|_| database::Error::Error) + .bind(torrent_id) + .fetch_all(&self.pool) + .await + .map_err(|_| database::Error::Error) } async fn update_tracker_info( diff --git a/src/models/mod.rs b/src/models/mod.rs index 5fff6a4a..754bfe80 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -3,6 +3,6 @@ pub mod info_hash; pub mod response; pub mod torrent; pub mod torrent_file; +pub mod torrent_tag; pub mod tracker_key; pub mod user; -pub mod torrent_tag; diff --git a/src/routes/mod.rs b/src/routes/mod.rs index fc76c52a..bbb439a4 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -5,9 +5,9 @@ pub mod category; pub mod proxy; pub mod root; pub mod settings; +pub mod tag; pub mod torrent; pub mod user; -pub mod tag; pub const API_VERSION: &str = "v1"; diff --git a/src/routes/tag.rs b/src/routes/tag.rs index 7fc9a4f6..7982b077 100644 --- a/src/routes/tag.rs +++ b/src/routes/tag.rs @@ -9,20 +9,13 @@ use crate::routes::API_VERSION; pub fn init(cfg: &mut web::ServiceConfig) { cfg.service( - web::scope(&format!("/{API_VERSION}/tag")) - .service( + web::scope(&format!("/{API_VERSION}/tag")).service( web::resource("") .route(web::post().to(add_tag)) .route(web::delete().to(delete_tag)), - ) - ); - cfg.service( - web::scope(&format!("/{API_VERSION}/tags")) - .service( - web::resource("") - .route(web::get().to(get_tags)) - ) + ), ); + cfg.service(web::scope(&format!("/{API_VERSION}/tags")).service(web::resource("").route(web::get().to(get_tags)))); } pub async fn get_tags(app_data: WebAppData) -> ServiceResult { @@ -58,11 +51,7 @@ pub struct DeleteTag { pub tag_id: TagId, } -pub async fn delete_tag( - req: HttpRequest, - payload: web::Json, - app_data: WebAppData, -) -> ServiceResult { +pub async fn delete_tag(req: HttpRequest, payload: web::Json, app_data: WebAppData) -> ServiceResult { let user_id = app_data.auth.get_user_id_from_request(&req).await?; let user = app_data.user_repository.get_compact(&user_id).await?; @@ -74,7 +63,5 @@ pub async fn delete_tag( app_data.torrent_tag_repository.delete_tag(&payload.tag_id).await?; - Ok(HttpResponse::Ok().json(OkResponse { - data: payload.tag_id, - })) + Ok(HttpResponse::Ok().json(OkResponse { data: payload.tag_id })) } diff --git a/src/routes/torrent.rs b/src/routes/torrent.rs index 4b857f4b..86ddf2ba 100644 --- a/src/routes/torrent.rs +++ b/src/routes/torrent.rs @@ -45,7 +45,7 @@ pub struct Create { pub title: String, pub description: String, pub category: String, - pub tags: Vec + pub tags: Vec, } impl Create { @@ -236,7 +236,7 @@ async fn get_torrent_request_from_payload(mut payload: Multipart) -> Result, @@ -124,7 +124,9 @@ impl Index { return Err(e); } - self.torrent_tag_repository.link_torrent_to_tags(&torrent_id, &torrent_request.fields.tags).await?; + self.torrent_tag_repository + .link_torrent_to_tags(&torrent_id, &torrent_request.fields.tags) + .await?; Ok(torrent_id) } @@ -476,7 +478,9 @@ impl DbTorrentInfoRepository { } if let Some(tags) = opt_tags { - let mut current_tags: Vec = self.database.get_tags_for_torrent_id(*torrent_id) + let mut current_tags: Vec = self + .database + .get_tags_for_torrent_id(*torrent_id) .await? .iter() .map(|tag| tag.tag_id)