Skip to content

Commit 7152654

Browse files
committed
refactor: [#56] rename structs for DB records
Add the "Record" suffix to structs representing DB records. In order to avoid mixing them up with models.
1 parent 99edf52 commit 7152654

File tree

2 files changed

+30
-32
lines changed

2 files changed

+30
-32
lines changed

src/upgrades/from_v1_0_0_to_v2_0_0/databases/sqlite_v1_0_0.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ use sqlx::{query_as, SqlitePool};
55
use crate::databases::database::DatabaseError;
66

77
#[derive(Debug, Serialize, Deserialize, sqlx::FromRow)]
8-
pub struct Category {
8+
pub struct CategoryRecord {
99
pub category_id: i64,
1010
pub name: String,
1111
}
1212

1313
#[derive(Debug, Serialize, Deserialize, sqlx::FromRow)]
14-
pub struct User {
14+
pub struct UserRecord {
1515
pub user_id: i64,
1616
pub username: String,
1717
pub email: String,
@@ -21,15 +21,15 @@ pub struct User {
2121
}
2222

2323
#[derive(Debug, Serialize, Deserialize, sqlx::FromRow)]
24-
pub struct TrackerKey {
24+
pub struct TrackerKeyRecord {
2525
pub key_id: i64,
2626
pub user_id: i64,
2727
pub key: String,
2828
pub valid_until: i64,
2929
}
3030

3131
#[derive(Debug, Serialize, Deserialize, sqlx::FromRow)]
32-
pub struct Torrent {
32+
pub struct TorrentRecord {
3333
pub torrent_id: i64,
3434
pub uploader: String,
3535
pub info_hash: String,
@@ -43,7 +43,7 @@ pub struct Torrent {
4343
}
4444

4545
#[derive(Debug, Serialize, Deserialize, sqlx::FromRow)]
46-
pub struct TorrentFile {
46+
pub struct TorrentFileRecord {
4747
pub file_id: i64,
4848
pub torrent_uid: i64,
4949
pub number: i64,
@@ -64,42 +64,42 @@ impl SqliteDatabaseV1_0_0 {
6464
Self { pool: db }
6565
}
6666

67-
pub async fn get_categories_order_by_id(&self) -> Result<Vec<Category>, DatabaseError> {
68-
query_as::<_, Category>(
67+
pub async fn get_categories_order_by_id(&self) -> Result<Vec<CategoryRecord>, DatabaseError> {
68+
query_as::<_, CategoryRecord>(
6969
"SELECT category_id, name FROM torrust_categories ORDER BY category_id ASC",
7070
)
7171
.fetch_all(&self.pool)
7272
.await
7373
.map_err(|_| DatabaseError::Error)
7474
}
7575

76-
pub async fn get_users(&self) -> Result<Vec<User>, sqlx::Error> {
77-
query_as::<_, User>("SELECT * FROM torrust_users ORDER BY user_id ASC")
76+
pub async fn get_users(&self) -> Result<Vec<UserRecord>, sqlx::Error> {
77+
query_as::<_, UserRecord>("SELECT * FROM torrust_users ORDER BY user_id ASC")
7878
.fetch_all(&self.pool)
7979
.await
8080
}
8181

82-
pub async fn get_user_by_username(&self, username: &str) -> Result<User, sqlx::Error> {
83-
query_as::<_, User>("SELECT * FROM torrust_users WHERE username = ?")
82+
pub async fn get_user_by_username(&self, username: &str) -> Result<UserRecord, sqlx::Error> {
83+
query_as::<_, UserRecord>("SELECT * FROM torrust_users WHERE username = ?")
8484
.bind(username)
8585
.fetch_one(&self.pool)
8686
.await
8787
}
8888

89-
pub async fn get_tracker_keys(&self) -> Result<Vec<TrackerKey>, sqlx::Error> {
90-
query_as::<_, TrackerKey>("SELECT * FROM torrust_tracker_keys ORDER BY key_id ASC")
89+
pub async fn get_tracker_keys(&self) -> Result<Vec<TrackerKeyRecord>, sqlx::Error> {
90+
query_as::<_, TrackerKeyRecord>("SELECT * FROM torrust_tracker_keys ORDER BY key_id ASC")
9191
.fetch_all(&self.pool)
9292
.await
9393
}
9494

95-
pub async fn get_torrents(&self) -> Result<Vec<Torrent>, sqlx::Error> {
96-
query_as::<_, Torrent>("SELECT * FROM torrust_torrents ORDER BY torrent_id ASC")
95+
pub async fn get_torrents(&self) -> Result<Vec<TorrentRecord>, sqlx::Error> {
96+
query_as::<_, TorrentRecord>("SELECT * FROM torrust_torrents ORDER BY torrent_id ASC")
9797
.fetch_all(&self.pool)
9898
.await
9999
}
100100

101-
pub async fn get_torrent_files(&self) -> Result<Vec<TorrentFile>, sqlx::Error> {
102-
query_as::<_, TorrentFile>("SELECT * FROM torrust_torrent_files ORDER BY file_id ASC")
101+
pub async fn get_torrent_files(&self) -> Result<Vec<TorrentFileRecord>, sqlx::Error> {
102+
query_as::<_, TorrentFileRecord>("SELECT * FROM torrust_torrent_files ORDER BY file_id ASC")
103103
.fetch_all(&self.pool)
104104
.await
105105
}

src/upgrades/from_v1_0_0_to_v2_0_0/databases/sqlite_v2_0_0.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ use sqlx::{query, query_as, SqlitePool};
55
use crate::databases::database::DatabaseError;
66
use crate::models::torrent_file::TorrentFile;
77

8-
use super::sqlite_v1_0_0::Torrent;
8+
use super::sqlite_v1_0_0::TorrentRecord;
99

1010
#[derive(Debug, Serialize, Deserialize, sqlx::FromRow)]
11-
pub struct Category {
11+
pub struct CategoryRecord {
1212
pub category_id: i64,
1313
pub name: String,
1414
}
@@ -39,8 +39,8 @@ impl SqliteDatabaseV2_0_0 {
3939
.map_err(|_| DatabaseError::Error)
4040
}
4141

42-
pub async fn get_categories(&self) -> Result<Vec<Category>, DatabaseError> {
43-
query_as::<_, Category>("SELECT tc.category_id, tc.name, COUNT(tt.category_id) as num_torrents FROM torrust_categories tc LEFT JOIN torrust_torrents tt on tc.category_id = tt.category_id GROUP BY tc.name")
42+
pub async fn get_categories(&self) -> Result<Vec<CategoryRecord>, DatabaseError> {
43+
query_as::<_, CategoryRecord>("SELECT tc.category_id, tc.name, COUNT(tt.category_id) as num_torrents FROM torrust_categories tc LEFT JOIN torrust_torrents tt on tc.category_id = tt.category_id GROUP BY tc.name")
4444
.fetch_all(&self.pool)
4545
.await
4646
.map_err(|_| DatabaseError::Error)
@@ -73,15 +73,13 @@ impl SqliteDatabaseV2_0_0 {
7373
date_imported: &str,
7474
administrator: bool,
7575
) -> Result<i64, sqlx::Error> {
76-
query(
77-
"INSERT INTO torrust_users (user_id, date_imported, administrator) VALUES (?, ?, ?)",
78-
)
79-
.bind(user_id)
80-
.bind(date_imported)
81-
.bind(administrator)
82-
.execute(&self.pool)
83-
.await
84-
.map(|v| v.last_insert_rowid())
76+
query("INSERT INTO torrust_users (user_id, date_imported, administrator) VALUES (?, ?, ?)")
77+
.bind(user_id)
78+
.bind(date_imported)
79+
.bind(administrator)
80+
.execute(&self.pool)
81+
.await
82+
.map(|v| v.last_insert_rowid())
8583
}
8684

8785
pub async fn insert_user_profile(
@@ -202,7 +200,7 @@ impl SqliteDatabaseV2_0_0 {
202200

203201
pub async fn insert_torrent_file_for_torrent_with_multiple_files(
204202
&self,
205-
torrent: &Torrent,
203+
torrent: &TorrentRecord,
206204
file: &TorrentFile,
207205
) -> Result<i64, sqlx::Error> {
208206
query(
@@ -218,7 +216,7 @@ impl SqliteDatabaseV2_0_0 {
218216
.map(|v| v.last_insert_rowid())
219217
}
220218

221-
pub async fn insert_torrent_info(&self, torrent: &Torrent) -> Result<i64, sqlx::Error> {
219+
pub async fn insert_torrent_info(&self, torrent: &TorrentRecord) -> Result<i64, sqlx::Error> {
222220
query(
223221
"INSERT INTO torrust_torrent_info (torrent_id, title, description)
224222
VALUES (?, ?, ?)",

0 commit comments

Comments
 (0)