Skip to content

Commit

Permalink
refactor(api): move Create struct
Browse files Browse the repository at this point in the history
It's been used also in Axum implementation and ActixWeb implementation
will be removed.
  • Loading branch information
josecelano committed Jun 19, 2023
1 parent 9591239 commit d8b2104
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
26 changes: 25 additions & 1 deletion src/models/torrent.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use serde::{Deserialize, Serialize};

use super::torrent_tag::TagId;
use crate::errors::ServiceError;
use crate::models::torrent_file::Torrent;
use crate::routes::torrent::Create;

#[allow(clippy::module_name_repetitions)]
pub type TorrentId = i64;
Expand All @@ -28,3 +29,26 @@ pub struct TorrentRequest {
pub fields: Create,
pub torrent: Torrent,
}

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

impl Create {
/// Returns the verify of this [`Create`].
///
/// # Errors
///
/// This function will return an `BadRequest` error if the `title` or the `category` is empty.
pub fn verify(&self) -> Result<(), ServiceError> {
if self.title.is_empty() || self.category.is_empty() {
Err(ServiceError::BadRequest)
} else {
Ok(())
}
}
}
25 changes: 1 addition & 24 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::TorrentRequest;
use crate::models::torrent::{Create, TorrentRequest};
use crate::models::torrent_tag::TagId;
use crate::services::torrent::ListingRequest;
use crate::utils::parse_torrent;
Expand Down Expand Up @@ -40,29 +40,6 @@ pub struct Count {
pub count: i32,
}

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

impl Create {
/// Returns the verify of this [`Create`].
///
/// # Errors
///
/// This function will return an `BadRequest` error if the `title` or the `category` is empty.
pub fn verify(&self) -> Result<(), ServiceError> {
if self.title.is_empty() || self.category.is_empty() {
Err(ServiceError::BadRequest)
} else {
Ok(())
}
}
}

#[derive(Debug, Deserialize)]
pub struct Update {
title: Option<String>,
Expand Down
3 changes: 1 addition & 2 deletions src/web/api/v1/contexts/torrent/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ 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::TorrentRequest;
use crate::models::torrent::{Create, TorrentRequest};
use crate::models::torrent_tag::TagId;
use crate::routes::torrent::Create;
use crate::services::torrent::ListingRequest;
use crate::utils::parse_torrent;
use crate::web::api::v1::auth::get_optional_logged_in_user;
Expand Down

0 comments on commit d8b2104

Please sign in to comment.