From 02d233d92ef90f3580b1dcebeb297465233a8730 Mon Sep 17 00:00:00 2001 From: Mario Date: Sat, 14 Dec 2024 13:23:50 +0100 Subject: [PATCH] feat: [#658] new database method to get all user profiles --- src/databases/database.rs | 3 +++ src/databases/mysql.rs | 9 ++++++++- src/databases/sqlite.rs | 7 +++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/databases/database.rs b/src/databases/database.rs index 84dd1860..40487b61 100644 --- a/src/databases/database.rs +++ b/src/databases/database.rs @@ -143,6 +143,9 @@ pub trait Database: Sync + Send { /// Get `UserProfile` from `username`. async fn get_user_profile_from_username(&self, username: &str) -> Result; + /// Get all user profiles as `Vec`. + async fn get_user_profiles(&self) -> Result, Error>; + /// Get `UserCompact` from `user_id`. async fn get_user_compact_from_id(&self, user_id: i64) -> Result; diff --git a/src/databases/mysql.rs b/src/databases/mysql.rs index ba8afc6d..af8a764b 100644 --- a/src/databases/mysql.rs +++ b/src/databases/mysql.rs @@ -148,13 +148,20 @@ impl Database for Mysql { } async fn get_user_profile_from_username(&self, username: &str) -> Result { - query_as::<_, UserProfile>(r#"SELECT user_id, username, COALESCE(email, "") as email, email_verified, COALESCE(bio, "") as bio, COALESCE(avatar, "") as avatar FROM torrust_user_profiles WHERE username = ?"#) + query_as::<_, UserProfile>(r#"SELECT user_id, username, COALESCE(email, "") as email FROM torrust_user_profiles"#) .bind(username) .fetch_one(&self.pool) .await .map_err(|_| database::Error::UserNotFound) } + async fn get_user_profiles(&self) -> Result, database::Error> { + query_as::<_, UserProfile>(r#"SELECT * FROM torrust_user_profiles"#) + .fetch_all(&self.pool) + .await + .map_err(|_| database::Error::Error) + } + async fn get_user_compact_from_id(&self, user_id: i64) -> Result { query_as::<_, UserCompact>("SELECT tu.user_id, tp.username, tu.administrator FROM torrust_users tu INNER JOIN torrust_user_profiles tp ON tu.user_id = tp.user_id WHERE tu.user_id = ?") .bind(user_id) diff --git a/src/databases/sqlite.rs b/src/databases/sqlite.rs index b1e20005..8b91c155 100644 --- a/src/databases/sqlite.rs +++ b/src/databases/sqlite.rs @@ -156,6 +156,13 @@ impl Database for Sqlite { .map_err(|_| database::Error::UserNotFound) } + async fn get_user_profiles(&self) -> Result, database::Error> { + query_as::<_, UserProfile>("SELECT * FROM torrust_user_profiles") + .fetch_all(&self.pool) + .await + .map_err(|_| database::Error::Error) + } + async fn get_user_compact_from_id(&self, user_id: i64) -> Result { query_as::<_, UserCompact>("SELECT tu.user_id, tp.username, tu.administrator FROM torrust_users tu INNER JOIN torrust_user_profiles tp ON tu.user_id = tp.user_id WHERE tu.user_id = ?") .bind(user_id)