torrust_index_backend/models/
response.rs1use serde::{Deserialize, Serialize};
2
3use super::torrent::TorrentId;
4use crate::databases::database::Category;
5use crate::models::torrent::TorrentListing;
6use crate::models::torrent_file::TorrentFile;
7use crate::models::torrent_tag::TorrentTag;
8
9pub enum OkResponses {
10 TokenResponse(TokenResponse),
11}
12
13#[allow(clippy::module_name_repetitions)]
14#[derive(Serialize, Deserialize, Debug)]
15pub struct OkResponse<T> {
16 pub data: T,
17}
18
19#[allow(clippy::module_name_repetitions)]
20#[derive(Serialize, Deserialize, Debug)]
21pub struct ErrorResponse<T> {
22 pub errors: Vec<T>,
23}
24
25#[allow(clippy::module_name_repetitions)]
26#[derive(Serialize, Deserialize, Debug)]
27pub struct TokenResponse {
28 pub token: String,
29 pub username: String,
30 pub admin: bool,
31}
32
33#[allow(clippy::module_name_repetitions)]
34#[derive(Serialize, Deserialize, Debug)]
35pub struct NewTorrentResponse {
36 pub torrent_id: TorrentId,
37 pub info_hash: String,
38}
39
40#[allow(clippy::module_name_repetitions)]
41#[derive(Serialize, Deserialize, Debug)]
42pub struct DeletedTorrentResponse {
43 pub torrent_id: TorrentId,
44 pub info_hash: String,
45}
46
47#[allow(clippy::module_name_repetitions)]
48#[derive(Debug, Serialize, Deserialize, sqlx::FromRow)]
49pub struct TorrentResponse {
50 pub torrent_id: i64,
51 pub uploader: String,
52 pub info_hash: String,
53 pub title: String,
54 pub description: Option<String>,
55 pub category: Option<Category>,
56 pub upload_date: String,
57 pub file_size: i64,
58 pub seeders: i64,
59 pub leechers: i64,
60 pub files: Vec<TorrentFile>,
61 pub trackers: Vec<String>,
62 pub magnet_link: String,
63 pub tags: Vec<TorrentTag>,
64}
65
66impl TorrentResponse {
67 #[must_use]
68 pub fn from_listing(torrent_listing: TorrentListing, category: Option<Category>) -> TorrentResponse {
69 TorrentResponse {
70 torrent_id: torrent_listing.torrent_id,
71 uploader: torrent_listing.uploader,
72 info_hash: torrent_listing.info_hash,
73 title: torrent_listing.title,
74 description: torrent_listing.description,
75 category,
76 upload_date: torrent_listing.date_uploaded,
77 file_size: torrent_listing.file_size,
78 seeders: torrent_listing.seeders,
79 leechers: torrent_listing.leechers,
80 files: vec![],
81 trackers: vec![],
82 magnet_link: String::new(),
83 tags: vec![],
84 }
85 }
86}
87
88#[allow(clippy::module_name_repetitions)]
89#[derive(Serialize, Deserialize, Debug, sqlx::FromRow)]
90pub struct TorrentsResponse {
91 pub total: u32,
92 pub results: Vec<TorrentListing>,
93}