Skip to content

Commit

Permalink
refactor: [#501] decouple database struct from API response for Category
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Feb 27, 2024
1 parent f6890cd commit 3debe8e
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 13 deletions.
4 changes: 1 addition & 3 deletions src/web/api/client/v1/contexts/torrent/responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,9 @@ pub struct TorrentDetails {
pub encoding: Option<String>,
}

#[allow(unknown_lints)]
#[allow(clippy::struct_field_names)]
#[derive(Deserialize, PartialEq, Debug)]
pub struct Category {
pub category_id: CategoryId, // todo: rename to `id`
pub id: CategoryId,
pub name: String,
pub num_torrents: u64,
}
Expand Down
7 changes: 5 additions & 2 deletions src/web/api/server/v1/contexts/category/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use axum::extract::{self, State};
use axum::response::{IntoResponse, Json, Response};

use super::forms::{AddCategoryForm, DeleteCategoryForm};
use super::responses::{added_category, deleted_category};
use super::responses::{added_category, deleted_category, Category};
use crate::common::AppData;
use crate::web::api::server::v1::extractors::user_id::ExtractLoggedInUser;
use crate::web::api::server::v1::responses::{self};
Expand All @@ -27,7 +27,10 @@ use crate::web::api::server::v1::responses::{self};
#[allow(clippy::unused_async)]
pub async fn get_all_handler(State(app_data): State<Arc<AppData>>) -> Response {
match app_data.category_repository.get_all().await {
Ok(categories) => Json(responses::OkResponseData { data: categories }).into_response(),
Ok(categories) => {
let categories: Vec<Category> = categories.into_iter().map(Category::from).collect();
Json(responses::OkResponseData { data: categories }).into_response()
}
Err(error) => error.into_response(),
}
}
Expand Down
22 changes: 22 additions & 0 deletions src/web/api/server/v1/contexts/category/responses.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
//! API responses for the the [`category`](crate::web::api::server::v1::contexts::category) API
//! context.
use axum::Json;
use serde::{Deserialize, Serialize};

use crate::databases::database::Category as DatabaseCategory;
use crate::web::api::server::v1::responses::OkResponseData;

#[derive(Debug, Serialize, Deserialize)]
pub struct Category {
pub id: i64,
/// Deprecated. Use `id`.
pub category_id: i64, // todo: remove when the Index GUI uses the new `id` field.
pub name: String,
pub num_torrents: i64,
}

/// Response after successfully creating a new category.
pub fn added_category(category_name: &str) -> Json<OkResponseData<String>> {
Json(OkResponseData {
Expand All @@ -17,3 +28,14 @@ pub fn deleted_category(category_name: &str) -> Json<OkResponseData<String>> {
data: category_name.to_string(),
})
}

impl From<DatabaseCategory> for Category {
fn from(db_category: DatabaseCategory) -> Self {
Category {
id: db_category.category_id,
category_id: db_category.category_id,
name: db_category.name,
num_torrents: db_category.num_torrents,
}
}
}
5 changes: 1 addition & 4 deletions tests/common/contexts/torrent/asserts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ pub fn assert_expected_torrent_details(torrent: &TorrentDetails, expected_torren
("info_hash", torrent.info_hash == expected_torrent.info_hash),
("title", torrent.title == expected_torrent.title),
("description", torrent.description == expected_torrent.description),
(
"category.category_id",
torrent.category.category_id == expected_torrent.category.category_id,
),
("category.id", torrent.category.id == expected_torrent.category.id),
("category.name", torrent.category.name == expected_torrent.category.name),
("file_size", torrent.file_size == expected_torrent.file_size),
("seeders", torrent.seeders == expected_torrent.seeders),
Expand Down
4 changes: 1 addition & 3 deletions tests/common/contexts/torrent/responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,9 @@ pub struct TorrentDetails {
pub encoding: Option<String>,
}

#[allow(unknown_lints)]
#[allow(clippy::struct_field_names)]
#[derive(Deserialize, PartialEq, Debug)]
pub struct Category {
pub category_id: CategoryId, // todo: rename to `id`
pub id: CategoryId,
pub name: String,
pub num_torrents: u64,
}
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/web/api/v1/contexts/torrent/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ mod for_guests {
title: test_torrent.index_info.title.clone(),
description: test_torrent.index_info.description,
category: Category {
category_id: software_predefined_category_id(),
id: software_predefined_category_id(),
name: test_torrent.index_info.category,
num_torrents: 19, // Ignored in assertion
},
Expand Down

0 comments on commit 3debe8e

Please sign in to comment.