Skip to content

Commit

Permalink
refactor: [#256] extract struct AddTorrentResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Aug 24, 2023
1 parent 29c3d46 commit 140895a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
14 changes: 12 additions & 2 deletions src/services/torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ pub struct AddTorrentRequest {
pub torrent_buffer: Vec<u8>,
}

pub struct AddTorrentResponse {
pub torrent_id: TorrentId,
pub info_hash: String,
pub original_info_hash: String,
}

/// User request to generate a torrent listing.
#[derive(Debug, Deserialize)]
pub struct ListingRequest {
Expand Down Expand Up @@ -120,7 +126,7 @@ impl Index {
&self,
add_torrent_form: AddTorrentRequest,
user_id: UserId,
) -> Result<(TorrentId, InfoHash), ServiceError> {
) -> Result<AddTorrentResponse, ServiceError> {
let metadata = Metadata {
title: add_torrent_form.title,
description: add_torrent_form.description,
Expand Down Expand Up @@ -184,7 +190,11 @@ impl Index {
.link_torrent_to_tags(&torrent_id, &metadata.tags)
.await?;

Ok((torrent_id, info_hash))
Ok(AddTorrentResponse {
torrent_id,
info_hash: info_hash.to_string(),
original_info_hash: original_info_hash.to_string(),
})
}

/// Gets a torrent from the Index.
Expand Down
2 changes: 1 addition & 1 deletion src/web/api/v1/contexts/torrent/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub async fn upload_torrent_handler(
};

match app_data.torrent_service.add_torrent(add_torrent_form, user_id).await {
Ok(torrent_ids) => new_torrent_response(torrent_ids.0, &torrent_ids.1.to_hex_string()).into_response(),
Ok(response) => new_torrent_response(response.torrent_id, &response.info_hash).into_response(),
Err(error) => error.into_response(),
}
}
Expand Down

0 comments on commit 140895a

Please sign in to comment.