Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Axum API: torrent context #202

Merged
merged 7 commits into from
Jun 16, 2023
19 changes: 19 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ text-to-png = "0.2"
indexmap = "1.9"
thiserror = "1.0"
binascii = "0.1"
axum = "0.6.18"
axum = { version = "0.6.18", features = ["multipart"]}
hyper = "0.14.26"
tower-http = { version = "0.4.0", features = ["cors"]}

Expand Down
2 changes: 1 addition & 1 deletion src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl Authentication {

// Begin Axum

/// Get User id from bearer token
/// Get logged-in user ID from bearer token
///
/// # Errors
///
Expand Down
5 changes: 5 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ pub enum ServiceError {
DatabaseError,
}

// Begin ActixWeb error handling
// todo: remove after migration to Axum

#[derive(Serialize, Deserialize)]
pub struct ErrorToResponse {
pub error: String,
Expand All @@ -156,6 +159,8 @@ impl ResponseError for ServiceError {
}
}

// End ActixWeb error handling

impl From<sqlx::Error> for ServiceError {
fn from(e: sqlx::Error) -> Self {
eprintln!("{e:?}");
Expand Down
10 changes: 5 additions & 5 deletions src/web/api/v1/contexts/category/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::common::AppData;
use crate::databases::database::{self, Category};
use crate::errors::ServiceError;
use crate::web::api::v1::extractors::bearer_token::Extract;
use crate::web::api::v1::responses::{self, OkResponse};
use crate::web::api::v1::responses::{self, OkResponseData};

/// It handles the request to get all the categories.
///
Expand All @@ -29,9 +29,9 @@ use crate::web::api::v1::responses::{self, OkResponse};
#[allow(clippy::unused_async)]
pub async fn get_all_handler(
State(app_data): State<Arc<AppData>>,
) -> Result<Json<responses::OkResponse<Vec<Category>>>, database::Error> {
) -> Result<Json<responses::OkResponseData<Vec<Category>>>, database::Error> {
match app_data.category_repository.get_all().await {
Ok(categories) => Ok(Json(responses::OkResponse { data: categories })),
Ok(categories) => Ok(Json(responses::OkResponseData { data: categories })),
Err(error) => Err(error),
}
}
Expand All @@ -49,7 +49,7 @@ pub async fn add_handler(
State(app_data): State<Arc<AppData>>,
Extract(maybe_bearer_token): Extract,
extract::Json(category_form): extract::Json<AddCategoryForm>,
) -> Result<Json<OkResponse<String>>, ServiceError> {
) -> Result<Json<OkResponseData<String>>, ServiceError> {
let user_id = app_data.auth.get_user_id_from_bearer_token(&maybe_bearer_token).await?;

match app_data.category_service.add_category(&category_form.name, &user_id).await {
Expand All @@ -71,7 +71,7 @@ pub async fn delete_handler(
State(app_data): State<Arc<AppData>>,
Extract(maybe_bearer_token): Extract,
extract::Json(category_form): extract::Json<DeleteCategoryForm>,
) -> Result<Json<OkResponse<String>>, ServiceError> {
) -> Result<Json<OkResponseData<String>>, ServiceError> {
// code-review: why do we need to send the whole category object to delete it?
// And we should use the ID instead of the name, because the name could change
// or we could add support for multiple languages.
Expand Down
10 changes: 5 additions & 5 deletions src/web/api/v1/contexts/category/responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
//! context.
use axum::Json;

use crate::web::api::v1::responses::OkResponse;
use crate::web::api::v1::responses::OkResponseData;

/// Response after successfully creating a new category.
pub fn added_category(category_name: &str) -> Json<OkResponse<String>> {
Json(OkResponse {
pub fn added_category(category_name: &str) -> Json<OkResponseData<String>> {
Json(OkResponseData {
data: category_name.to_string(),
})
}

/// Response after successfully deleting a new category.
pub fn deleted_category(category_name: &str) -> Json<OkResponse<String>> {
Json(OkResponse {
pub fn deleted_category(category_name: &str) -> Json<OkResponseData<String>> {
Json(OkResponseData {
data: category_name.to_string(),
})
}
18 changes: 9 additions & 9 deletions src/web/api/v1/contexts/settings/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::common::AppData;
use crate::config::{ConfigurationPublic, TorrustBackend};
use crate::errors::ServiceError;
use crate::web::api::v1::extractors::bearer_token::Extract;
use crate::web::api::v1::responses::{self, OkResponse};
use crate::web::api::v1::responses::{self, OkResponseData};

/// Get all settings.
///
Expand All @@ -21,28 +21,28 @@ use crate::web::api::v1::responses::{self, OkResponse};
pub async fn get_all_handler(
State(app_data): State<Arc<AppData>>,
Extract(maybe_bearer_token): Extract,
) -> Result<Json<OkResponse<TorrustBackend>>, ServiceError> {
) -> Result<Json<OkResponseData<TorrustBackend>>, ServiceError> {
let user_id = app_data.auth.get_user_id_from_bearer_token(&maybe_bearer_token).await?;

let all_settings = app_data.settings_service.get_all(&user_id).await?;

Ok(Json(responses::OkResponse { data: all_settings }))
Ok(Json(responses::OkResponseData { data: all_settings }))
}

/// Get public Settings.
#[allow(clippy::unused_async)]
pub async fn get_public_handler(State(app_data): State<Arc<AppData>>) -> Json<responses::OkResponse<ConfigurationPublic>> {
pub async fn get_public_handler(State(app_data): State<Arc<AppData>>) -> Json<responses::OkResponseData<ConfigurationPublic>> {
let public_settings = app_data.settings_service.get_public().await;

Json(responses::OkResponse { data: public_settings })
Json(responses::OkResponseData { data: public_settings })
}

/// Get website name.
#[allow(clippy::unused_async)]
pub async fn get_site_name_handler(State(app_data): State<Arc<AppData>>) -> Json<responses::OkResponse<String>> {
pub async fn get_site_name_handler(State(app_data): State<Arc<AppData>>) -> Json<responses::OkResponseData<String>> {
let site_name = app_data.settings_service.get_site_name().await;

Json(responses::OkResponse { data: site_name })
Json(responses::OkResponseData { data: site_name })
}

/// Update all the settings.
Expand All @@ -59,10 +59,10 @@ pub async fn update_handler(
State(app_data): State<Arc<AppData>>,
Extract(maybe_bearer_token): Extract,
extract::Json(torrust_backend): extract::Json<TorrustBackend>,
) -> Result<Json<OkResponse<TorrustBackend>>, ServiceError> {
) -> Result<Json<OkResponseData<TorrustBackend>>, ServiceError> {
let user_id = app_data.auth.get_user_id_from_bearer_token(&maybe_bearer_token).await?;

let new_settings = app_data.settings_service.update_all(torrust_backend, &user_id).await?;

Ok(Json(responses::OkResponse { data: new_settings }))
Ok(Json(responses::OkResponseData { data: new_settings }))
}
10 changes: 5 additions & 5 deletions src/web/api/v1/contexts/tag/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::databases::database;
use crate::errors::ServiceError;
use crate::models::torrent_tag::TorrentTag;
use crate::web::api::v1::extractors::bearer_token::Extract;
use crate::web::api::v1::responses::{self, OkResponse};
use crate::web::api::v1::responses::{self, OkResponseData};

/// It handles the request to get all the tags.
///
Expand All @@ -30,9 +30,9 @@ use crate::web::api::v1::responses::{self, OkResponse};
#[allow(clippy::unused_async)]
pub async fn get_all_handler(
State(app_data): State<Arc<AppData>>,
) -> Result<Json<responses::OkResponse<Vec<TorrentTag>>>, database::Error> {
) -> Result<Json<responses::OkResponseData<Vec<TorrentTag>>>, database::Error> {
match app_data.tag_repository.get_all().await {
Ok(tags) => Ok(Json(responses::OkResponse { data: tags })),
Ok(tags) => Ok(Json(responses::OkResponseData { data: tags })),
Err(error) => Err(error),
}
}
Expand All @@ -50,7 +50,7 @@ pub async fn add_handler(
State(app_data): State<Arc<AppData>>,
Extract(maybe_bearer_token): Extract,
extract::Json(add_tag_form): extract::Json<AddTagForm>,
) -> Result<Json<OkResponse<String>>, ServiceError> {
) -> Result<Json<OkResponseData<String>>, ServiceError> {
let user_id = app_data.auth.get_user_id_from_bearer_token(&maybe_bearer_token).await?;

match app_data.tag_service.add_tag(&add_tag_form.name, &user_id).await {
Expand All @@ -72,7 +72,7 @@ pub async fn delete_handler(
State(app_data): State<Arc<AppData>>,
Extract(maybe_bearer_token): Extract,
extract::Json(delete_tag_form): extract::Json<DeleteTagForm>,
) -> Result<Json<OkResponse<String>>, ServiceError> {
) -> Result<Json<OkResponseData<String>>, ServiceError> {
let user_id = app_data.auth.get_user_id_from_bearer_token(&maybe_bearer_token).await?;

match app_data.tag_service.delete_tag(&delete_tag_form.tag_id, &user_id).await {
Expand Down
10 changes: 5 additions & 5 deletions src/web/api/v1/contexts/tag/responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
use axum::Json;

use crate::models::torrent_tag::TagId;
use crate::web::api::v1::responses::OkResponse;
use crate::web::api::v1::responses::OkResponseData;

/// Response after successfully creating a new tag.
pub fn added_tag(tag_name: &str) -> Json<OkResponse<String>> {
Json(OkResponse {
pub fn added_tag(tag_name: &str) -> Json<OkResponseData<String>> {
Json(OkResponseData {
data: tag_name.to_string(),
})
}

/// Response after successfully deleting a tag.
pub fn deleted_tag(tag_id: TagId) -> Json<OkResponse<String>> {
Json(OkResponse {
pub fn deleted_tag(tag_id: TagId) -> Json<OkResponseData<String>> {
Json(OkResponseData {
data: tag_id.to_string(),
})
}
10 changes: 10 additions & 0 deletions src/web/api/v1/contexts/torrent/forms.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use serde::Deserialize;

use crate::models::torrent_tag::TagId;

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