Skip to content

Commit 21a1f16

Browse files
committed
feat!: [#97] make torrent category optional
The JSON response can now contain a `null` value for the `category`: ```json { "data": { "torrent_id": 1, "uploader": "josecelano", "info_hash": "E8564469C258B1373BC2D5749FB83B1BF83D68A0", "title": "Ubuntu", "description": null, "category": null, "upload_date": "2023-06-27 11:35:09", "file_size": 1261707713, "seeders": 0, "leechers": 0, "files": [ { "path": [ "NNN.url" ], "length": 114, "md5sum": null }, { "path": [ "XXX.url" ], "length": 121, "md5sum": null }, { "path": [ "XXX.avi" ], "length": 1261707478, "md5sum": null } ], "trackers": [ "udp://tracker:6969", ], "magnet_link": "magnet:?xt=urn:btih:E8564469C258B1373BC2D5749FB83B1BF83D68A0&dn=Ubuntu&tr=udp%3A%2F%2Ftracker%3A6969e", "tags": [] } } ```
1 parent c27a5a9 commit 21a1f16

File tree

5 files changed

+45
-5
lines changed

5 files changed

+45
-5
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- Step 1: Allow null categories for torrents
2+
ALTER TABLE torrust_torrents MODIFY category_id INTEGER NULL;
3+
4+
-- Step 2: Set torrent category to NULL when category is deleted
5+
ALTER TABLE `torrust_torrents` DROP FOREIGN KEY `torrust_torrents_ibfk_2`;
6+
ALTER TABLE `torrust_torrents` ADD CONSTRAINT `torrust_torrents_ibfk_2` FOREIGN KEY (`category_id`) REFERENCES `torrust_categories` (`category_id`) ON DELETE SET NULL;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
-- Step 1: Create a new table with the new structure
2+
CREATE TABLE IF NOT EXISTS "torrust_torrents_new" (
3+
"torrent_id" INTEGER NOT NULL,
4+
"uploader_id" INTEGER NOT NULL,
5+
"category_id" INTEGER NULL,
6+
"info_hash" TEXT NOT NULL UNIQUE,
7+
"size" INTEGER NOT NULL,
8+
"name" TEXT NOT NULL,
9+
"pieces" TEXT NOT NULL,
10+
"piece_length" INTEGER NOT NULL,
11+
"private" BOOLEAN DEFAULT NULL,
12+
"root_hash" INT NOT NULL DEFAULT 0,
13+
"date_uploaded" TEXT NOT NULL,
14+
FOREIGN KEY("uploader_id") REFERENCES "torrust_users"("user_id") ON DELETE CASCADE,
15+
FOREIGN KEY("category_id") REFERENCES "torrust_categories"("category_id") ON DELETE SET NULL,
16+
PRIMARY KEY("torrent_id" AUTOINCREMENT)
17+
);
18+
19+
-- Step 2: Copy rows from the current table to the new table
20+
INSERT INTO torrust_torrents_new (torrent_id, uploader_id, category_id, info_hash, size, name, pieces, piece_length, private, root_hash, date_uploaded)
21+
SELECT torrent_id, uploader_id, category_id, info_hash, size, name, pieces, piece_length, private, root_hash, date_uploaded
22+
FROM torrust_torrents;
23+
24+
-- Step 3: Delete the current table
25+
DROP TABLE torrust_torrents;
26+
27+
-- Step 1: Rename the new table
28+
ALTER TABLE torrust_torrents_new RENAME TO torrust_torrents;

src/models/response.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub struct TorrentResponse {
5252
pub info_hash: String,
5353
pub title: String,
5454
pub description: Option<String>,
55-
pub category: Category,
55+
pub category: Option<Category>,
5656
pub upload_date: String,
5757
pub file_size: i64,
5858
pub seeders: i64,
@@ -65,7 +65,7 @@ pub struct TorrentResponse {
6565

6666
impl TorrentResponse {
6767
#[must_use]
68-
pub fn from_listing(torrent_listing: TorrentListing, category: Category) -> TorrentResponse {
68+
pub fn from_listing(torrent_listing: TorrentListing, category: Option<Category>) -> TorrentResponse {
6969
TorrentResponse {
7070
torrent_id: torrent_listing.torrent_id,
7171
uploader: torrent_listing.uploader,

src/models/torrent.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub struct TorrentListing {
1616
pub info_hash: String,
1717
pub title: String,
1818
pub description: Option<String>,
19-
pub category_id: i64,
19+
pub category_id: Option<i64>,
2020
pub date_uploaded: String,
2121
pub file_size: i64,
2222
pub seeders: i64,

src/services/torrent.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,10 @@ impl Index {
221221

222222
let torrent_id = torrent_listing.torrent_id;
223223

224-
let category = self.category_repository.get_by_id(&torrent_listing.category_id).await?;
224+
let category = match torrent_listing.category_id {
225+
Some(category_id) => Some(self.category_repository.get_by_id(&category_id).await?),
226+
None => None,
227+
};
225228

226229
let mut torrent_response = TorrentResponse::from_listing(torrent_listing, category);
227230

@@ -382,7 +385,10 @@ impl Index {
382385
.one_torrent_by_torrent_id(&torrent_listing.torrent_id)
383386
.await?;
384387

385-
let category = self.category_repository.get_by_id(&torrent_listing.category_id).await?;
388+
let category = match torrent_listing.category_id {
389+
Some(category_id) => Some(self.category_repository.get_by_id(&category_id).await?),
390+
None => None,
391+
};
386392

387393
let torrent_response = TorrentResponse::from_listing(torrent_listing, category);
388394

0 commit comments

Comments
 (0)