Skip to content

Commit

Permalink
fix: sql queries
Browse files Browse the repository at this point in the history
  • Loading branch information
mickvandijke committed May 24, 2023
1 parent 7ce3d5e commit a1bd92f
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/databases/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,10 @@ pub trait Database: Sync + Send {
async fn update_torrent_description(&self, torrent_id: i64, description: &str) -> Result<(), Error>;

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

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

/// Add a tag to torrent.
async fn add_torrent_tag_link(&self, torrent_id: i64, tag_id: TagId) -> Result<(), Error>;
Expand Down
18 changes: 8 additions & 10 deletions src/databases/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -670,23 +670,21 @@ impl Database for Mysql {
})
}

async fn add_tag(&self, name: &str) -> Result<TorrentTag, database::Error> {
println!("inserting tag: {}", name);

query_as("INSERT INTO torrust_torrent_tags (name) VALUES (?) RETURNING id, name")
async fn add_tag(&self, name: &str) -> Result<(), database::Error> {
query("INSERT INTO torrust_torrent_tags (name) VALUES (?)")
.bind(name)
.fetch_one(&self.pool)
.execute(&self.pool)
.await
.map(|_| ())
.map_err(|err| database::Error::ErrorWithText(err.to_string()))
}

async fn delete_tag(&self, tag_id: TagId) -> Result<TorrentTag, database::Error> {
println!("deleting tag: {}", tag_id);

query_as("DELETE FROM torrust_torrent_tags WHERE tag_id = ? RETURNING id, name")
async fn delete_tag(&self, tag_id: TagId) -> Result<(), database::Error> {
query("DELETE FROM torrust_torrent_tags WHERE tag_id = ?")
.bind(tag_id)
.fetch_one(&self.pool)
.execute(&self.pool)
.await
.map(|_| ())
.map_err(|err| database::Error::ErrorWithText(err.to_string()))
}

Expand Down
14 changes: 8 additions & 6 deletions src/databases/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -660,19 +660,21 @@ impl Database for Sqlite {
})
}

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

async fn delete_tag(&self, tag_id: TagId) -> Result<TorrentTag, database::Error> {
query_as("DELETE FROM torrust_torrent_tags WHERE tag_id = ? RETURNING id, name")
async fn delete_tag(&self, tag_id: TagId) -> Result<(), database::Error> {
query("DELETE FROM torrust_torrent_tags WHERE tag_id = ?")
.bind(tag_id)
.fetch_one(&self.pool)
.execute(&self.pool)
.await
.map(|_| ())
.map_err(|err| database::Error::ErrorWithText(err.to_string()))
}

Expand Down
8 changes: 4 additions & 4 deletions src/routes/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ pub async fn add_tag(req: HttpRequest, payload: web::Json<AddTag>, app_data: Web
return Err(ServiceError::Unauthorized);
}

let tag = app_data.torrent_tag_repository.add_tag(&payload.name).await?;
app_data.torrent_tag_repository.add_tag(&payload.name).await?;

Ok(HttpResponse::Ok().json(OkResponse {
data: tag,
data: payload.name.to_string(),
}))
}

Expand All @@ -72,9 +72,9 @@ pub async fn delete_tag(
return Err(ServiceError::Unauthorized);
}

let tag = app_data.torrent_tag_repository.delete_tag(&payload.tag_id).await?;
app_data.torrent_tag_repository.delete_tag(&payload.tag_id).await?;

Ok(HttpResponse::Ok().json(OkResponse {
data: tag,
data: payload.tag_id,
}))
}
6 changes: 3 additions & 3 deletions src/services/torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl Index {
return Err(e);
}

let _ = 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)
}
Expand Down Expand Up @@ -549,7 +549,7 @@ impl DbTorrentTagRepository {
/// # Errors
///
/// It returns an error if there is a database error.
pub async fn add_tag(&self, tag_name: &str) -> Result<TorrentTag, Error> {
pub async fn add_tag(&self, tag_name: &str) -> Result<(), Error> {
self.database.add_tag(tag_name).await
}

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

Expand Down

0 comments on commit a1bd92f

Please sign in to comment.