Skip to content

Commit a1bd92f

Browse files
committed
fix: sql queries
1 parent 7ce3d5e commit a1bd92f

File tree

5 files changed

+25
-25
lines changed

5 files changed

+25
-25
lines changed

src/databases/database.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,10 @@ pub trait Database: Sync + Send {
231231
async fn update_torrent_description(&self, torrent_id: i64, description: &str) -> Result<(), Error>;
232232

233233
/// Add a new tag.
234-
async fn add_tag(&self, name: &str) -> Result<TorrentTag, Error>;
234+
async fn add_tag(&self, name: &str) -> Result<(), Error>;
235235

236236
/// Delete a tag.
237-
async fn delete_tag(&self, tag_id: TagId) -> Result<TorrentTag, Error>;
237+
async fn delete_tag(&self, tag_id: TagId) -> Result<(), Error>;
238238

239239
/// Add a tag to torrent.
240240
async fn add_torrent_tag_link(&self, torrent_id: i64, tag_id: TagId) -> Result<(), Error>;

src/databases/mysql.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -670,23 +670,21 @@ impl Database for Mysql {
670670
})
671671
}
672672

673-
async fn add_tag(&self, name: &str) -> Result<TorrentTag, database::Error> {
674-
println!("inserting tag: {}", name);
675-
676-
query_as("INSERT INTO torrust_torrent_tags (name) VALUES (?) RETURNING id, name")
673+
async fn add_tag(&self, name: &str) -> Result<(), database::Error> {
674+
query("INSERT INTO torrust_torrent_tags (name) VALUES (?)")
677675
.bind(name)
678-
.fetch_one(&self.pool)
676+
.execute(&self.pool)
679677
.await
678+
.map(|_| ())
680679
.map_err(|err| database::Error::ErrorWithText(err.to_string()))
681680
}
682681

683-
async fn delete_tag(&self, tag_id: TagId) -> Result<TorrentTag, database::Error> {
684-
println!("deleting tag: {}", tag_id);
685-
686-
query_as("DELETE FROM torrust_torrent_tags WHERE tag_id = ? RETURNING id, name")
682+
async fn delete_tag(&self, tag_id: TagId) -> Result<(), database::Error> {
683+
query("DELETE FROM torrust_torrent_tags WHERE tag_id = ?")
687684
.bind(tag_id)
688-
.fetch_one(&self.pool)
685+
.execute(&self.pool)
689686
.await
687+
.map(|_| ())
690688
.map_err(|err| database::Error::ErrorWithText(err.to_string()))
691689
}
692690

src/databases/sqlite.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -660,19 +660,21 @@ impl Database for Sqlite {
660660
})
661661
}
662662

663-
async fn add_tag(&self, name: &str) -> Result<TorrentTag, database::Error> {
664-
query_as("INSERT INTO torrust_torrent_tags (name) VALUES (?) RETURNING id, name")
663+
async fn add_tag(&self, name: &str) -> Result<(), database::Error> {
664+
query("INSERT INTO torrust_torrent_tags (name) VALUES (?)")
665665
.bind(name)
666-
.fetch_one(&self.pool)
666+
.execute(&self.pool)
667667
.await
668+
.map(|_| ())
668669
.map_err(|err| database::Error::ErrorWithText(err.to_string()))
669670
}
670671

671-
async fn delete_tag(&self, tag_id: TagId) -> Result<TorrentTag, database::Error> {
672-
query_as("DELETE FROM torrust_torrent_tags WHERE tag_id = ? RETURNING id, name")
672+
async fn delete_tag(&self, tag_id: TagId) -> Result<(), database::Error> {
673+
query("DELETE FROM torrust_torrent_tags WHERE tag_id = ?")
673674
.bind(tag_id)
674-
.fetch_one(&self.pool)
675+
.execute(&self.pool)
675676
.await
677+
.map(|_| ())
676678
.map_err(|err| database::Error::ErrorWithText(err.to_string()))
677679
}
678680

src/routes/tag.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ pub async fn add_tag(req: HttpRequest, payload: web::Json<AddTag>, app_data: Web
4646
return Err(ServiceError::Unauthorized);
4747
}
4848

49-
let tag = app_data.torrent_tag_repository.add_tag(&payload.name).await?;
49+
app_data.torrent_tag_repository.add_tag(&payload.name).await?;
5050

5151
Ok(HttpResponse::Ok().json(OkResponse {
52-
data: tag,
52+
data: payload.name.to_string(),
5353
}))
5454
}
5555

@@ -72,9 +72,9 @@ pub async fn delete_tag(
7272
return Err(ServiceError::Unauthorized);
7373
}
7474

75-
let tag = app_data.torrent_tag_repository.delete_tag(&payload.tag_id).await?;
75+
app_data.torrent_tag_repository.delete_tag(&payload.tag_id).await?;
7676

7777
Ok(HttpResponse::Ok().json(OkResponse {
78-
data: tag,
78+
data: payload.tag_id,
7979
}))
8080
}

src/services/torrent.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ impl Index {
121121
return Err(e);
122122
}
123123

124-
let _ = self.torrent_tag_repository.link_torrent_to_tags(&torrent_id, &torrent_request.fields.tags).await?;
124+
self.torrent_tag_repository.link_torrent_to_tags(&torrent_id, &torrent_request.fields.tags).await?;
125125

126126
Ok(torrent_id)
127127
}
@@ -549,7 +549,7 @@ impl DbTorrentTagRepository {
549549
/// # Errors
550550
///
551551
/// It returns an error if there is a database error.
552-
pub async fn add_tag(&self, tag_name: &str) -> Result<TorrentTag, Error> {
552+
pub async fn add_tag(&self, tag_name: &str) -> Result<(), Error> {
553553
self.database.add_tag(tag_name).await
554554
}
555555

@@ -594,7 +594,7 @@ impl DbTorrentTagRepository {
594594
/// # Errors
595595
///
596596
/// It returns an error if there is a database error.
597-
pub async fn delete_tag(&self, tag_id: &TagId) -> Result<TorrentTag, Error> {
597+
pub async fn delete_tag(&self, tag_id: &TagId) -> Result<(), Error> {
598598
self.database.delete_tag(*tag_id).await
599599
}
600600

0 commit comments

Comments
 (0)