Skip to content

Commit

Permalink
refator: [#296] move logic to service layer
Browse files Browse the repository at this point in the history
The service should know the model but the model should not know the
service. The dependency should be only on one way.
  • Loading branch information
josecelano committed Sep 19, 2023
1 parent 1660fd5 commit dfdac19
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
23 changes: 0 additions & 23 deletions src/models/torrent_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use sha1::{Digest, Sha1};

use super::info_hash::InfoHash;
use crate::config::Configuration;
use crate::services::torrent_file::CreateTorrentRequest;
use crate::utils::hex::{from_bytes, into_bytes};

#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -68,28 +67,6 @@ pub struct TorrentFile {
}

impl Torrent {
/// It builds a `Torrent` from a request.
///
/// # Panics
///
/// This function will panic if the `torrent_info.pieces` is not a valid hex string.
#[must_use]
pub fn from_request(create_torrent_req: CreateTorrentRequest) -> Self {
let info_dict = create_torrent_req.build_info_dictionary();

Self {
info: info_dict,
announce: None,
nodes: None,
encoding: None,
httpseeds: None,
announce_list: Some(create_torrent_req.announce_urls),
creation_date: None,
comment: create_torrent_req.comment,
created_by: None,
}
}

/// It hydrates a `Torrent` struct from the database data.
///
/// # Panics
Expand Down
28 changes: 25 additions & 3 deletions src/services/torrent_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,35 @@ pub struct CreateTorrentRequest {
}

impl CreateTorrentRequest {
/// It builds a `Torrent` from a request.
///
/// # Panics
///
/// This function will panic if the `torrent_info.pieces` is not a valid hex string.
#[must_use]
pub fn build_torrent(&self) -> Torrent {
let info_dict = self.build_info_dictionary();

Torrent {
info: info_dict,
announce: None,
nodes: None,
encoding: None,
httpseeds: None,
announce_list: Some(self.announce_urls.clone()),
creation_date: None,
comment: self.comment.clone(),
created_by: None,
}
}

/// It builds a `TorrentInfoDictionary` from the current torrent request.
///
/// # Panics
///
/// This function will panic if the `pieces` field is not a valid hex string.
#[must_use]
pub fn build_info_dictionary(&self) -> TorrentInfoDictionary {
fn build_info_dictionary(&self) -> TorrentInfoDictionary {
TorrentInfoDictionary::with(
&self.name,
self.piece_length,
Expand Down Expand Up @@ -62,7 +84,7 @@ pub fn generate_random_torrent(id: Uuid) -> Torrent {

let torrent_announce_urls: Vec<Vec<String>> = vec![];

let torrent_info_request = CreateTorrentRequest {
let create_torrent_req = CreateTorrentRequest {
name: format!("file-{id}.txt"),
pieces: sha1(&file_contents),
piece_length: 16384,
Expand All @@ -73,7 +95,7 @@ pub fn generate_random_torrent(id: Uuid) -> Torrent {
comment: None,
};

Torrent::from_request(torrent_info_request)
create_torrent_req.build_torrent()
}

#[cfg(test)]
Expand Down

0 comments on commit dfdac19

Please sign in to comment.