Skip to content

Commit a46d300

Browse files
committed
refactor: [#276] extract function Torrent::validate_and_build_metadata
1 parent ca6e97c commit a46d300

File tree

7 files changed

+44
-40
lines changed

7 files changed

+44
-40
lines changed

src/databases/database.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ pub trait Database: Sync + Send {
196196
original_info_hash: &InfoHash,
197197
torrent: &Torrent,
198198
uploader_id: UserId,
199-
category_id: i64,
199+
category_id: CategoryId,
200200
title: &str,
201201
description: &str,
202202
) -> Result<i64, Error>;

src/databases/mysql.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ impl Database for Mysql {
432432
original_info_hash: &InfoHash,
433433
torrent: &Torrent,
434434
uploader_id: UserId,
435-
category_id: i64,
435+
category_id: CategoryId,
436436
title: &str,
437437
description: &str,
438438
) -> Result<i64, database::Error> {

src/databases/sqlite.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ impl Database for Sqlite {
422422
original_info_hash: &InfoHash,
423423
torrent: &Torrent,
424424
uploader_id: UserId,
425-
category_id: i64,
425+
category_id: CategoryId,
426426
title: &str,
427427
description: &str,
428428
) -> Result<i64, database::Error> {

src/errors.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,7 @@ impl From<MetadataError> for ServiceError {
209209
fn from(e: MetadataError) -> Self {
210210
eprintln!("{e}");
211211
match e {
212-
MetadataError::MissingTorrentTitle | MetadataError::MissingTorrentCategoryName => {
213-
ServiceError::MissingMandatoryMetadataFields
214-
}
212+
MetadataError::MissingTorrentTitle => ServiceError::MissingMandatoryMetadataFields,
215213
MetadataError::InvalidTorrentTitleLength => ServiceError::InvalidTorrentTitleLength,
216214
}
217215
}

src/models/torrent.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use derive_more::{Display, Error};
22
use serde::{Deserialize, Serialize};
33

4+
use super::category::CategoryId;
45
use super::torrent_tag::TagId;
56

67
const MIN_TORRENT_TITLE_LENGTH: usize = 3;
@@ -28,12 +29,9 @@ pub struct TorrentListing {
2829

2930
#[derive(Debug, Display, PartialEq, Eq, Error)]
3031
pub enum MetadataError {
31-
#[display(fmt = "Missing mandatory torrent title")]
32+
#[display(fmt = "Missing mandatory torrent title.")]
3233
MissingTorrentTitle,
3334

34-
#[display(fmt = "Missing mandatory torrent category name")]
35-
MissingTorrentCategoryName,
36-
3735
#[display(fmt = "Torrent title is too short.")]
3836
InvalidTorrentTitleLength,
3937
}
@@ -42,7 +40,7 @@ pub enum MetadataError {
4240
pub struct Metadata {
4341
pub title: String,
4442
pub description: String,
45-
pub category: String,
43+
pub category_id: CategoryId,
4644
pub tags: Vec<TagId>,
4745
}
4846

@@ -53,13 +51,13 @@ impl Metadata {
5351
///
5452
/// This function will return an error if the metadata fields do not have a
5553
/// valid format.
56-
pub fn new(title: &str, description: &str, category: &str, tag_ids: &[TagId]) -> Result<Self, MetadataError> {
57-
Self::validate_format(title, description, category, tag_ids)?;
54+
pub fn new(title: &str, description: &str, category_id: CategoryId, tag_ids: &[TagId]) -> Result<Self, MetadataError> {
55+
Self::validate_format(title, description, category_id, tag_ids)?;
5856

5957
Ok(Self {
6058
title: title.to_owned(),
6159
description: description.to_owned(),
62-
category: category.to_owned(),
60+
category_id,
6361
tags: tag_ids.to_vec(),
6462
})
6563
}
@@ -76,15 +74,16 @@ impl Metadata {
7674
///
7775
/// This function will return an error if any of the metadata fields does
7876
/// not have a valid format.
79-
fn validate_format(title: &str, _description: &str, category: &str, _tag_ids: &[TagId]) -> Result<(), MetadataError> {
77+
fn validate_format(
78+
title: &str,
79+
_description: &str,
80+
_category_id: CategoryId,
81+
_tag_ids: &[TagId],
82+
) -> Result<(), MetadataError> {
8083
if title.is_empty() {
8184
return Err(MetadataError::MissingTorrentTitle);
8285
}
8386

84-
if category.is_empty() {
85-
return Err(MetadataError::MissingTorrentCategoryName);
86-
}
87-
8887
if title.len() < MIN_TORRENT_TITLE_LENGTH {
8988
return Err(MetadataError::InvalidTorrentTitleLength);
9089
}

src/services/torrent.rs

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use serde_derive::{Deserialize, Serialize};
77
use super::category::DbCategoryRepository;
88
use super::user::DbUserRepository;
99
use crate::config::Configuration;
10-
use crate::databases::database::{Category, Database, Error, Sorting};
10+
use crate::databases::database::{Database, Error, Sorting};
1111
use crate::errors::ServiceError;
1212
use crate::models::category::CategoryId;
1313
use crate::models::info_hash::InfoHash;
@@ -38,7 +38,7 @@ pub struct Index {
3838
pub struct AddTorrentRequest {
3939
pub title: String,
4040
pub description: String,
41-
pub category: String,
41+
pub category_name: String,
4242
pub tags: Vec<TagId>,
4343
pub torrent_buffer: Vec<u8>,
4444
}
@@ -130,23 +130,9 @@ impl Index {
130130
user_id: UserId,
131131
) -> Result<AddTorrentResponse, ServiceError> {
132132
// Authorization: only authenticated users ere allowed to upload torrents
133-
134133
let _user = self.user_repository.get_compact(&user_id).await?;
135134

136-
// Validate and build metadata
137-
138-
let metadata = Metadata::new(
139-
&add_torrent_req.title,
140-
&add_torrent_req.description,
141-
&add_torrent_req.category,
142-
&add_torrent_req.tags,
143-
)?;
144-
145-
let category = self
146-
.category_repository
147-
.get_by_name(&metadata.category)
148-
.await
149-
.map_err(|_| ServiceError::InvalidCategory)?;
135+
let metadata = self.validate_and_build_metadata(&add_torrent_req).await?;
150136

151137
// Validate and build torrent file
152138

@@ -198,7 +184,7 @@ impl Index {
198184

199185
let torrent_id = self
200186
.torrent_repository
201-
.add(&original_info_hash, &torrent, &metadata, user_id, category)
187+
.add(&original_info_hash, &torrent, &metadata, user_id, metadata.category_id)
202188
.await?;
203189

204190
self.torrent_tag_repository
@@ -236,6 +222,27 @@ impl Index {
236222
})
237223
}
238224

225+
async fn validate_and_build_metadata(&self, add_torrent_req: &AddTorrentRequest) -> Result<Metadata, ServiceError> {
226+
if add_torrent_req.category_name.is_empty() {
227+
return Err(ServiceError::MissingMandatoryMetadataFields);
228+
}
229+
230+
let category = self
231+
.category_repository
232+
.get_by_name(&add_torrent_req.category_name)
233+
.await
234+
.map_err(|_| ServiceError::InvalidCategory)?;
235+
236+
let metadata = Metadata::new(
237+
&add_torrent_req.title,
238+
&add_torrent_req.description,
239+
category.category_id,
240+
&add_torrent_req.tags,
241+
)?;
242+
243+
Ok(metadata)
244+
}
245+
239246
/// Gets a torrent from the Index.
240247
///
241248
/// # Errors
@@ -533,14 +540,14 @@ impl DbTorrentRepository {
533540
torrent: &Torrent,
534541
metadata: &Metadata,
535542
user_id: UserId,
536-
category: Category,
543+
category_id: CategoryId,
537544
) -> Result<TorrentId, Error> {
538545
self.database
539546
.insert_torrent_and_get_id(
540547
original_info_hash,
541548
torrent,
542549
user_id,
543-
category.category_id,
550+
category_id,
544551
&metadata.title,
545552
&metadata.description,
546553
)

src/web/api/v1/contexts/torrent/handlers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ async fn build_add_torrent_request_from_payload(mut payload: Multipart) -> Resul
396396
Ok(AddTorrentRequest {
397397
title,
398398
description,
399-
category,
399+
category_name: category,
400400
tags,
401401
torrent_buffer: torrent_cursor.into_inner(),
402402
})

0 commit comments

Comments
 (0)