Skip to content

Commit 09e1620

Browse files
committed
refactor: [#501] decouple database struct from API response for Category
1 parent f6890cd commit 09e1620

File tree

8 files changed

+59
-16
lines changed

8 files changed

+59
-16
lines changed

src/models/category.rs

+24
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,26 @@
1+
use serde::{Deserialize, Serialize};
2+
3+
use crate::databases::database::Category as DatabaseCategory;
4+
5+
#[derive(Debug, Serialize, Deserialize, sqlx::FromRow)]
6+
pub struct Category {
7+
pub id: i64,
8+
// Deprecated. Use `id`.
9+
pub category_id: i64,
10+
pub name: String,
11+
pub num_torrents: i64,
12+
}
13+
114
#[allow(clippy::module_name_repetitions)]
215
pub type CategoryId = i64;
16+
17+
impl From<DatabaseCategory> for Category {
18+
fn from(db_category: DatabaseCategory) -> Self {
19+
Category {
20+
id: db_category.category_id,
21+
category_id: db_category.category_id,
22+
name: db_category.name,
23+
num_torrents: db_category.num_torrents,
24+
}
25+
}
26+
}

src/models/response.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use serde::{Deserialize, Serialize};
22

3+
use super::category::Category;
34
use super::torrent::TorrentId;
4-
use crate::databases::database::Category;
5+
use crate::databases::database::Category as DatabaseCategory;
56
use crate::models::torrent::TorrentListing;
67
use crate::models::torrent_file::TorrentFile;
78
use crate::models::torrent_tag::TorrentTag;
@@ -70,14 +71,14 @@ pub struct TorrentResponse {
7071

7172
impl TorrentResponse {
7273
#[must_use]
73-
pub fn from_listing(torrent_listing: TorrentListing, category: Option<Category>) -> TorrentResponse {
74+
pub fn from_listing(torrent_listing: TorrentListing, category: Option<DatabaseCategory>) -> TorrentResponse {
7475
TorrentResponse {
7576
torrent_id: torrent_listing.torrent_id,
7677
uploader: torrent_listing.uploader,
7778
info_hash: torrent_listing.info_hash,
7879
title: torrent_listing.title,
7980
description: torrent_listing.description,
80-
category,
81+
category: category.map(std::convert::Into::into),
8182
upload_date: torrent_listing.date_uploaded,
8283
file_size: torrent_listing.file_size,
8384
seeders: torrent_listing.seeders,

src/web/api/client/v1/contexts/torrent/responses.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,9 @@ pub struct TorrentDetails {
7575
pub encoding: Option<String>,
7676
}
7777

78-
#[allow(unknown_lints)]
79-
#[allow(clippy::struct_field_names)]
8078
#[derive(Deserialize, PartialEq, Debug)]
8179
pub struct Category {
82-
pub category_id: CategoryId, // todo: rename to `id`
80+
pub id: CategoryId,
8381
pub name: String,
8482
pub num_torrents: u64,
8583
}

src/web/api/server/v1/contexts/category/handlers.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use axum::extract::{self, State};
66
use axum::response::{IntoResponse, Json, Response};
77

88
use super::forms::{AddCategoryForm, DeleteCategoryForm};
9-
use super::responses::{added_category, deleted_category};
9+
use super::responses::{added_category, deleted_category, Category};
1010
use crate::common::AppData;
1111
use crate::web::api::server::v1::extractors::user_id::ExtractLoggedInUser;
1212
use crate::web::api::server::v1::responses::{self};
@@ -27,7 +27,10 @@ use crate::web::api::server::v1::responses::{self};
2727
#[allow(clippy::unused_async)]
2828
pub async fn get_all_handler(State(app_data): State<Arc<AppData>>) -> Response {
2929
match app_data.category_repository.get_all().await {
30-
Ok(categories) => Json(responses::OkResponseData { data: categories }).into_response(),
30+
Ok(categories) => {
31+
let categories: Vec<Category> = categories.into_iter().map(Category::from).collect();
32+
Json(responses::OkResponseData { data: categories }).into_response()
33+
}
3134
Err(error) => error.into_response(),
3235
}
3336
}

src/web/api/server/v1/contexts/category/responses.rs

+22
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
//! API responses for the the [`category`](crate::web::api::server::v1::contexts::category) API
22
//! context.
33
use axum::Json;
4+
use serde::{Deserialize, Serialize};
45

6+
use crate::databases::database::Category as DatabaseCategory;
57
use crate::web::api::server::v1::responses::OkResponseData;
68

9+
#[derive(Debug, Serialize, Deserialize)]
10+
pub struct Category {
11+
pub id: i64,
12+
/// Deprecated. Use `id`.
13+
pub category_id: i64, // todo: remove when the Index GUI uses the new `id` field.
14+
pub name: String,
15+
pub num_torrents: i64,
16+
}
17+
718
/// Response after successfully creating a new category.
819
pub fn added_category(category_name: &str) -> Json<OkResponseData<String>> {
920
Json(OkResponseData {
@@ -17,3 +28,14 @@ pub fn deleted_category(category_name: &str) -> Json<OkResponseData<String>> {
1728
data: category_name.to_string(),
1829
})
1930
}
31+
32+
impl From<DatabaseCategory> for Category {
33+
fn from(db_category: DatabaseCategory) -> Self {
34+
Category {
35+
id: db_category.category_id,
36+
category_id: db_category.category_id,
37+
name: db_category.name,
38+
num_torrents: db_category.num_torrents,
39+
}
40+
}
41+
}

tests/common/contexts/torrent/asserts.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ pub fn assert_expected_torrent_details(torrent: &TorrentDetails, expected_torren
1515
("info_hash", torrent.info_hash == expected_torrent.info_hash),
1616
("title", torrent.title == expected_torrent.title),
1717
("description", torrent.description == expected_torrent.description),
18-
(
19-
"category.category_id",
20-
torrent.category.category_id == expected_torrent.category.category_id,
21-
),
18+
("category.id", torrent.category.id == expected_torrent.category.id),
2219
("category.name", torrent.category.name == expected_torrent.category.name),
2320
("file_size", torrent.file_size == expected_torrent.file_size),
2421
("seeders", torrent.seeders == expected_torrent.seeders),

tests/common/contexts/torrent/responses.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,9 @@ pub struct TorrentDetails {
7474
pub encoding: Option<String>,
7575
}
7676

77-
#[allow(unknown_lints)]
78-
#[allow(clippy::struct_field_names)]
7977
#[derive(Deserialize, PartialEq, Debug)]
8078
pub struct Category {
81-
pub category_id: CategoryId, // todo: rename to `id`
79+
pub id: CategoryId,
8280
pub name: String,
8381
pub num_torrents: u64,
8482
}

tests/e2e/web/api/v1/contexts/torrent/contract.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ mod for_guests {
182182
title: test_torrent.index_info.title.clone(),
183183
description: test_torrent.index_info.description,
184184
category: Category {
185-
category_id: software_predefined_category_id(),
185+
id: software_predefined_category_id(),
186186
name: test_torrent.index_info.category,
187187
num_torrents: 19, // Ignored in assertion
188188
},

0 commit comments

Comments
 (0)