Skip to content

Commit

Permalink
refactor(api): rename structs
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Jun 19, 2023
1 parent d8b2104 commit ff8816f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 19 deletions.
8 changes: 4 additions & 4 deletions src/models/torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ pub struct TorrentListing {

#[allow(clippy::module_name_repetitions)]
#[derive(Debug)]
pub struct TorrentRequest {
pub fields: Create,
pub struct AddTorrentRequest {
pub metadata: Metadata,
pub torrent: Torrent,
}

#[derive(Debug, Deserialize)]
pub struct Create {
pub struct Metadata {
pub title: String,
pub description: String,
pub category: String,
pub tags: Vec<TagId>,
}

impl Create {
impl Metadata {
/// Returns the verify of this [`Create`].
///
/// # Errors
Expand Down
11 changes: 7 additions & 4 deletions src/routes/torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::common::WebAppData;
use crate::errors::{ServiceError, ServiceResult};
use crate::models::info_hash::InfoHash;
use crate::models::response::{NewTorrentResponse, OkResponse};
use crate::models::torrent::{Create, TorrentRequest};
use crate::models::torrent::{AddTorrentRequest, Metadata};
use crate::models::torrent_tag::TagId;
use crate::services::torrent::ListingRequest;
use crate::utils::parse_torrent;
Expand Down Expand Up @@ -166,7 +166,7 @@ fn get_torrent_info_hash_from_request(req: &HttpRequest) -> Result<InfoHash, Ser
}
}

async fn get_torrent_request_from_payload(mut payload: Multipart) -> Result<TorrentRequest, ServiceError> {
async fn get_torrent_request_from_payload(mut payload: Multipart) -> Result<AddTorrentRequest, ServiceError> {
let torrent_buffer = vec![0u8];
let mut torrent_cursor = Cursor::new(torrent_buffer);

Expand Down Expand Up @@ -209,7 +209,7 @@ async fn get_torrent_request_from_payload(mut payload: Multipart) -> Result<Torr
}
}

let fields = Create {
let fields = Metadata {
title,
description,
category,
Expand All @@ -230,5 +230,8 @@ async fn get_torrent_request_from_payload(mut payload: Multipart) -> Result<Torr
}
}

Ok(TorrentRequest { fields, torrent })
Ok(AddTorrentRequest {
metadata: fields,
torrent,
})
}
19 changes: 12 additions & 7 deletions src/services/torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::databases::database::{Category, Database, Error, Sorting};
use crate::errors::ServiceError;
use crate::models::info_hash::InfoHash;
use crate::models::response::{DeletedTorrentResponse, TorrentResponse, TorrentsResponse};
use crate::models::torrent::{TorrentId, TorrentListing, TorrentRequest};
use crate::models::torrent::{AddTorrentRequest, TorrentId, TorrentListing};
use crate::models::torrent_file::{DbTorrentInfo, Torrent, TorrentFile};
use crate::models::torrent_tag::{TagId, TorrentTag};
use crate::models::user::UserId;
Expand Down Expand Up @@ -97,12 +97,12 @@ impl Index {
/// * Unable to get the category from the database.
/// * Unable to insert the torrent into the database.
/// * Unable to add the torrent to the whitelist.
pub async fn add_torrent(&self, mut torrent_request: TorrentRequest, user_id: UserId) -> Result<TorrentId, ServiceError> {
pub async fn add_torrent(&self, mut torrent_request: AddTorrentRequest, user_id: UserId) -> Result<TorrentId, ServiceError> {
torrent_request.torrent.set_announce_urls(&self.configuration).await;

let category = self
.category_repository
.get_by_name(&torrent_request.fields.category)
.get_by_name(&torrent_request.metadata.category)
.await
.map_err(|_| ServiceError::InvalidCategory)?;

Expand All @@ -126,7 +126,7 @@ impl Index {
}

self.torrent_tag_repository
.link_torrent_to_tags(&torrent_id, &torrent_request.fields.tags)
.link_torrent_to_tags(&torrent_id, &torrent_request.metadata.tags)
.await?;

Ok(torrent_id)
Expand Down Expand Up @@ -417,14 +417,19 @@ impl DbTorrentRepository {
/// # Errors
///
/// This function will return an error there is a database error.
pub async fn add(&self, torrent_request: &TorrentRequest, user_id: UserId, category: Category) -> Result<TorrentId, Error> {
pub async fn add(
&self,
torrent_request: &AddTorrentRequest,
user_id: UserId,
category: Category,
) -> Result<TorrentId, Error> {
self.database
.insert_torrent_and_get_id(
&torrent_request.torrent,
user_id,
category.category_id,
&torrent_request.fields.title,
&torrent_request.fields.description,
&torrent_request.metadata.title,
&torrent_request.metadata.description,
)
.await
}
Expand Down
11 changes: 7 additions & 4 deletions src/web/api/v1/contexts/torrent/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use super::responses::{new_torrent_response, torrent_file_response};
use crate::common::AppData;
use crate::errors::ServiceError;
use crate::models::info_hash::InfoHash;
use crate::models::torrent::{Create, TorrentRequest};
use crate::models::torrent::{AddTorrentRequest, Metadata};
use crate::models::torrent_tag::TagId;
use crate::services::torrent::ListingRequest;
use crate::utils::parse_torrent;
Expand Down Expand Up @@ -209,7 +209,7 @@ pub async fn delete_torrent_handler(
/// - The multipart content is invalid.
/// - The torrent file pieces key has a length that is not a multiple of 20.
/// - The binary data cannot be decoded as a torrent file.
async fn get_torrent_request_from_payload(mut payload: Multipart) -> Result<TorrentRequest, ServiceError> {
async fn get_torrent_request_from_payload(mut payload: Multipart) -> Result<AddTorrentRequest, ServiceError> {
let torrent_buffer = vec![0u8];
let mut torrent_cursor = Cursor::new(torrent_buffer);

Expand Down Expand Up @@ -266,7 +266,7 @@ async fn get_torrent_request_from_payload(mut payload: Multipart) -> Result<Torr
}
}

let fields = Create {
let fields = Metadata {
title,
description,
category,
Expand All @@ -288,5 +288,8 @@ async fn get_torrent_request_from_payload(mut payload: Multipart) -> Result<Torr
}
}

Ok(TorrentRequest { fields, torrent })
Ok(AddTorrentRequest {
metadata: fields,
torrent,
})
}

0 comments on commit ff8816f

Please sign in to comment.