Skip to content

Commit

Permalink
feat: [#303] store in the database the torrent fields creation_date c…
Browse files Browse the repository at this point in the history
…reated_by encoding

API responses now contain the three field in the

 - Torrent details endpoint
 - Torrent list endpoint
  • Loading branch information
MMelchor authored and mario-nt committed Nov 7, 2023
1 parent 70cef70 commit b738291
Show file tree
Hide file tree
Showing 15 changed files with 72 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE torrust_torrents ADD COLUMN creation_date BIGINT UNSIGNED NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE torrust_torrents ADD COLUMN created_by TEXT NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE torrust_torrents ADD COLUMN "encoding" TEXT NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "torrust_torrents" ADD COLUMN "creation_date" BIGINT NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "torrust_torrents" ADD COLUMN "created_by" TEXT NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "torrust_torrents" ADD COLUMN "encoding" TEXT NULL;
16 changes: 14 additions & 2 deletions src/databases/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,9 @@ impl Database for Mysql {
tt.size AS file_size,
tt.name,
tt.comment,
tt.creation_date,
tt.created_by,
tt.encoding,
CAST(COALESCE(sum(ts.seeders),0) as signed) as seeders,
CAST(COALESCE(sum(ts.leechers),0) as signed) as leechers
FROM torrust_torrents tt
Expand Down Expand Up @@ -465,8 +468,11 @@ impl Database for Mysql {
root_hash,
`source`,
comment,
date_uploaded
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, UTC_TIMESTAMP())",
date_uploaded,
creation_date,
created_by,
encoding
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, UTC_TIMESTAMP(), ?, ?, ?)",
)
.bind(uploader_id)
.bind(metadata.category_id)
Expand Down Expand Up @@ -754,6 +760,9 @@ impl Database for Mysql {
tt.size AS file_size,
tt.name,
tt.comment,
tt.creation_date,
tt.created_by,
tt.encoding,
CAST(COALESCE(sum(ts.seeders),0) as signed) as seeders,
CAST(COALESCE(sum(ts.leechers),0) as signed) as leechers
FROM torrust_torrents tt
Expand Down Expand Up @@ -782,6 +791,9 @@ impl Database for Mysql {
tt.size AS file_size,
tt.name,
tt.comment,
tt.creation_date,
tt.created_by,
tt.encoding,
CAST(COALESCE(sum(ts.seeders),0) as signed) as seeders,
CAST(COALESCE(sum(ts.leechers),0) as signed) as leechers
FROM torrust_torrents tt
Expand Down
19 changes: 17 additions & 2 deletions src/databases/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,9 @@ impl Database for Sqlite {
tt.size AS file_size,
tt.name,
tt.comment,
tt.creation_date,
tt.created_by,
tt.encoding,
CAST(COALESCE(sum(ts.seeders),0) as signed) as seeders,
CAST(COALESCE(sum(ts.leechers),0) as signed) as leechers
FROM torrust_torrents tt
Expand Down Expand Up @@ -455,8 +458,11 @@ impl Database for Sqlite {
root_hash,
`source`,
comment,
date_uploaded
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, strftime('%Y-%m-%d %H:%M:%S',DATETIME('now', 'utc')))",
date_uploaded,
creation_date,
created_by,
encoding
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, strftime('%Y-%m-%d %H:%M:%S',DATETIME('now', 'utc')), ?, ?, ?)",
)
.bind(uploader_id)
.bind(metadata.category_id)
Expand All @@ -469,6 +475,9 @@ impl Database for Sqlite {
.bind(root_hash)
.bind(torrent.info.source.clone())
.bind(torrent.comment.clone())
.bind(torrent.creation_date.clone())
.bind(torrent.created_by.clone())
.bind(torrent.encoding.clone())
.execute(&mut *tx)
.await
.map(|v| v.last_insert_rowid())
Expand Down Expand Up @@ -743,6 +752,9 @@ impl Database for Sqlite {
tt.size AS file_size,
tt.name,
tt.comment,
tt.creation_date,
tt.created_by,
tt.encoding,
CAST(COALESCE(sum(ts.seeders),0) as signed) as seeders,
CAST(COALESCE(sum(ts.leechers),0) as signed) as leechers
FROM torrust_torrents tt
Expand Down Expand Up @@ -770,6 +782,9 @@ impl Database for Sqlite {
tt.size AS file_size,
tt.name,
tt.comment,
tt.creation_date,
tt.created_by,
tt.encoding,
CAST(COALESCE(sum(ts.seeders),0) as signed) as seeders,
CAST(COALESCE(sum(ts.leechers),0) as signed) as leechers
FROM torrust_torrents tt
Expand Down
6 changes: 6 additions & 0 deletions src/models/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ pub struct TorrentResponse {
pub tags: Vec<TorrentTag>,
pub name: String,
pub comment: Option<String>,
pub creation_date: Option<i64>,
pub created_by: Option<String>,
pub encoding: Option<String>,
}

impl TorrentResponse {
Expand All @@ -85,6 +88,9 @@ impl TorrentResponse {
tags: vec![],
name: torrent_listing.name,
comment: torrent_listing.comment,
creation_date: torrent_listing.creation_date,
created_by: torrent_listing.created_by,
encoding: torrent_listing.encoding,
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/models/torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ pub struct TorrentListing {
pub leechers: i64,
pub name: String,
pub comment: Option<String>,
pub creation_date: Option<i64>,
pub created_by: Option<String>,
pub encoding: Option<String>,
}

#[derive(Debug, Display, PartialEq, Eq, Error)]
Expand Down
9 changes: 6 additions & 3 deletions src/models/torrent_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ impl Torrent {
info: info_dict,
announce: None,
nodes: None,
encoding: None,
encoding: db_torrent.encoding.clone(),
httpseeds: None,
announce_list: Some(torrent_announce_urls),
creation_date: None,
creation_date: db_torrent.creation_date.clone(),
comment: db_torrent.comment.clone(),
created_by: None,
created_by: db_torrent.created_by.clone(),
}
}

Expand Down Expand Up @@ -303,6 +303,9 @@ pub struct DbTorrent {
pub private: Option<u8>,
pub root_hash: i64,
pub comment: Option<String>,
pub creation_date: Option<i64>,
pub created_by: Option<String>,
pub encoding: Option<String>,
}

#[allow(clippy::module_name_repetitions)]
Expand Down
12 changes: 9 additions & 3 deletions src/services/torrent_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ pub struct CreateTorrentRequest {
// Other fields of the root level metainfo dictionary
pub announce_urls: Vec<Vec<String>>,
pub comment: Option<String>,
pub creation_date: Option<i64>,
pub created_by: Option<String>,
pub encoding: Option<String>,
}

impl CreateTorrentRequest {
Expand All @@ -35,12 +38,12 @@ impl CreateTorrentRequest {
info: info_dict,
announce: None,
nodes: None,
encoding: None,
encoding: self.encoding.clone(),
httpseeds: None,
announce_list: Some(self.announce_urls.clone()),
creation_date: None,
creation_date: self.creation_date.clone(),
comment: self.comment.clone(),
created_by: None,
created_by: self.created_by.clone(),
}
}

Expand Down Expand Up @@ -93,6 +96,9 @@ pub fn generate_random_torrent(id: Uuid) -> Torrent {
files: torrent_files,
announce_urls: torrent_announce_urls,
comment: None,
creation_date: None,
created_by: None,
encoding: None,
};

create_torrent_req.build_torrent()
Expand Down
3 changes: 2 additions & 1 deletion tests/common/contexts/torrent/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ use which::which;
pub struct TorrentFileInfo {
pub name: String,
pub comment: Option<String>,
pub creation_date: Option<u64>,
pub creation_date: Option<i64>,
pub created_by: Option<String>,
pub encoding: Option<String>,
pub source: Option<String>,
pub info_hash: String,
pub torrent_size: u64,
Expand Down
6 changes: 6 additions & 0 deletions tests/common/contexts/torrent/responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ pub struct ListItem {
pub leechers: i64,
pub name: String,
pub comment: Option<String>,
pub creation_date: Option<i64>,
pub created_by: Option<String>,
pub encoding: Option<String>,
}

#[derive(Deserialize, PartialEq, Debug)]
Expand All @@ -66,6 +69,9 @@ pub struct TorrentDetails {
pub tags: Vec<Tag>,
pub name: String,
pub comment: Option<String>,
pub creation_date: Option<i64>,
pub created_by: Option<String>,
pub encoding: Option<String>,
}

#[derive(Deserialize, PartialEq, Debug)]
Expand Down
3 changes: 3 additions & 0 deletions tests/e2e/web/api/v1/contexts/torrent/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,9 @@ mod for_guests {
tags: vec![],
name: test_torrent.index_info.name.clone(),
comment: test_torrent.file_info.comment.clone(),
creation_date: test_torrent.file_info.creation_date.clone(),
created_by: test_torrent.file_info.created_by.clone(),
encoding: test_torrent.file_info.encoding.clone(),
};

assert_expected_torrent_details(&torrent_details_response.data, &expected_torrent);
Expand Down

0 comments on commit b738291

Please sign in to comment.