Skip to content

Commit

Permalink
Merge #206: Axum API: fix errors and minor refactorings
Browse files Browse the repository at this point in the history
f0017ae refactor(api): move auth logic to web api (Jose Celano)
ff8816f refactor(api): rename structs (Jose Celano)
d8b2104 refactor(api): move Create struct (Jose Celano)
9591239 refector(api): move API_VERSION const out of ActixWeb implementation (Jose Celano)
a6881e3 refactor: move funtion get_optional_logged_in_user (Jose Celano)
b73d864 fix(api): Axum API, error should return a 500 status code (Jose Celano)
b4744e7 test(api): fix test for empty categories (Jose Celano)
34db879 fix(api): Axum API, fix delete tag response (Jose Celano)

Pull request description:

  Before enabling Auxm API, there are some minor changes and fixes to do.

Top commit has no ACKs.

Tree-SHA512: 5f02553f58aa9cebe98fe500577b6bfe235bafa40b04835a5a2a8527fd519ddfc1004a1272548e96ae2e611589de05dac39e3533c587312b61aea4fe46d2dcce
  • Loading branch information
josecelano committed Jun 19, 2023
2 parents 48b27ea + f0017ae commit a662f9c
Show file tree
Hide file tree
Showing 27 changed files with 310 additions and 276 deletions.
2 changes: 1 addition & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::sync::Arc;

use tokio::task::JoinHandle;

use crate::auth::Authentication;
use crate::bootstrap::logging;
use crate::cache::image::manager::ImageCacheService;
use crate::common::AppData;
Expand All @@ -19,6 +18,7 @@ use crate::services::torrent::{
use crate::services::user::{self, DbBannedUserList, DbUserProfileRepository, DbUserRepository};
use crate::services::{proxy, settings, torrent};
use crate::tracker::statistics_importer::StatisticsImporter;
use crate::web::api::v1::auth::Authentication;
use crate::web::api::{start, Implementation};
use crate::{mailer, tracker};

Expand Down
109 changes: 0 additions & 109 deletions src/auth.rs

This file was deleted.

2 changes: 1 addition & 1 deletion src/common.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::sync::Arc;

use crate::auth::Authentication;
use crate::cache::image::manager::ImageCacheService;
use crate::config::Configuration;
use crate::databases::database::Database;
Expand All @@ -14,6 +13,7 @@ use crate::services::torrent::{
use crate::services::user::{self, DbBannedUserList, DbUserProfileRepository, DbUserRepository};
use crate::services::{proxy, settings, torrent};
use crate::tracker::statistics_importer::StatisticsImporter;
use crate::web::api::v1::auth::Authentication;
use crate::{mailer, tracker};
pub type Username = String;

Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,6 @@
//! In addition to the production code documentation you can find a lot of
//! examples in the [tests](https://github.com/torrust/torrust-index-backend/tree/develop/tests/e2e/contexts) directory.
pub mod app;
pub mod auth;
pub mod bootstrap;
pub mod cache;
pub mod common;
Expand Down
2 changes: 1 addition & 1 deletion src/mailer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use serde::{Deserialize, Serialize};

use crate::config::Configuration;
use crate::errors::ServiceError;
use crate::routes::API_VERSION;
use crate::utils::clock;
use crate::web::api::API_VERSION;

pub struct Service {
cfg: Arc<Configuration>,
Expand Down
30 changes: 27 additions & 3 deletions 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 @@ -24,7 +25,30 @@ 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 Metadata {
pub title: String,
pub description: String,
pub category: String,
pub tags: Vec<TagId>,
}

impl Metadata {
/// 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(())
}
}
}
2 changes: 1 addition & 1 deletion src/routes/about.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use actix_web::http::StatusCode;
use actix_web::{web, HttpResponse, Responder};

use crate::errors::ServiceResult;
use crate::routes::API_VERSION;
use crate::services::about::{index_page, license_page};
use crate::web::api::API_VERSION;

pub fn init(cfg: &mut web::ServiceConfig) {
cfg.service(
Expand Down
2 changes: 1 addition & 1 deletion src/routes/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
use crate::common::WebAppData;
use crate::errors::ServiceResult;
use crate::models::response::OkResponse;
use crate::routes::API_VERSION;
use crate::web::api::API_VERSION;

pub fn init(cfg: &mut web::ServiceConfig) {
cfg.service(
Expand Down
2 changes: 0 additions & 2 deletions src/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ pub mod tag;
pub mod torrent;
pub mod user;

pub const API_VERSION: &str = "v1";

pub fn init(cfg: &mut web::ServiceConfig) {
user::init(cfg);
torrent::init(cfg);
Expand Down
2 changes: 1 addition & 1 deletion src/routes/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use actix_web::{web, HttpRequest, HttpResponse, Responder};
use crate::cache::image::manager::Error;
use crate::common::WebAppData;
use crate::errors::ServiceResult;
use crate::routes::API_VERSION;
use crate::ui::proxy::{load_error_images, map_error_to_image};
use crate::web::api::API_VERSION;

pub fn init(cfg: &mut web::ServiceConfig) {
cfg.service(
Expand Down
3 changes: 2 additions & 1 deletion src/routes/root.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use actix_web::web;

use crate::routes::{about, API_VERSION};
use crate::routes::about;
use crate::web::api::API_VERSION;

pub fn init(cfg: &mut web::ServiceConfig) {
cfg.service(web::scope("/").service(web::resource("").route(web::get().to(about::get))));
Expand Down
2 changes: 1 addition & 1 deletion src/routes/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::common::WebAppData;
use crate::config;
use crate::errors::ServiceResult;
use crate::models::response::OkResponse;
use crate::routes::API_VERSION;
use crate::web::api::API_VERSION;

pub fn init(cfg: &mut web::ServiceConfig) {
cfg.service(
Expand Down
2 changes: 1 addition & 1 deletion src/routes/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::common::WebAppData;
use crate::errors::ServiceResult;
use crate::models::response::OkResponse;
use crate::models::torrent_tag::TagId;
use crate::routes::API_VERSION;
use crate::web::api::API_VERSION;

pub fn init(cfg: &mut web::ServiceConfig) {
cfg.service(
Expand Down
36 changes: 8 additions & 28 deletions src/routes/torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ 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::{AddTorrentRequest, Metadata};
use crate::models::torrent_tag::TagId;
use crate::routes::API_VERSION;
use crate::services::torrent::ListingRequest;
use crate::utils::parse_torrent;
use crate::web::api::API_VERSION;

pub fn init(cfg: &mut web::ServiceConfig) {
cfg.service(
Expand All @@ -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 Expand Up @@ -189,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 @@ -232,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 @@ -253,5 +230,8 @@ async fn get_torrent_request_from_payload(mut payload: Multipart) -> Result<Torr
}
}

Ok(TorrentRequest { fields, torrent })
Ok(AddTorrentRequest {
metadata: fields,
torrent,
})
}
2 changes: 1 addition & 1 deletion src/routes/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use serde::{Deserialize, Serialize};
use crate::common::WebAppData;
use crate::errors::{ServiceError, ServiceResult};
use crate::models::response::{OkResponse, TokenResponse};
use crate::routes::API_VERSION;
use crate::web::api::v1::contexts::user::forms::RegistrationForm;
use crate::web::api::API_VERSION;

pub fn init(cfg: &mut web::ServiceConfig) {
cfg.service(
Expand Down
2 changes: 1 addition & 1 deletion src/services/about.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Templates for "about" static pages.
use crate::routes::API_VERSION;
use crate::web::api::API_VERSION;

#[must_use]
pub fn index_page() -> String {
Expand Down
Loading

0 comments on commit a662f9c

Please sign in to comment.