From 5102531a70e4c8686e8ad6841a49ae150161c763 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Wed, 20 Apr 2022 16:23:38 +0530 Subject: [PATCH] feat: add_user and user_exists with accompanying test suites DESCRIPTION add_user Adds new user to DB. Implementers must ensure username uniqueness scoped at forge instance level. When applicable, HTML link to profile photo may be stored as we.. user_exists Checks if a user exists in database. Optionally provide hostname of forge instance to get forge-specific results. --- db/db-core/src/lib.rs | 23 ++++++++++++++++++++++- db/db-core/src/tests.rs | 10 +++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/db/db-core/src/lib.rs b/db/db-core/src/lib.rs index 91bfdb4..0eab822 100644 --- a/db/db-core/src/lib.rs +++ b/db/db-core/src/lib.rs @@ -40,6 +40,7 @@ pub mod ops; #[cfg(feature = "test")] pub mod tests; +use dev::*; pub use ops::GetConnection; pub mod prelude { @@ -65,7 +66,20 @@ pub struct CreateForge<'a> { pub forge_type: ForgeImplementation, } -use dev::*; +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +/// add new user to database +pub struct AddUser<'a> { + /// hostname of the forge instance: with scheme but remove trailing slash + /// hostname can be derived from html_link also, but used to link to user's forge instance + pub hostname: &'a str, + /// username of the user + pub username: &'a str, + /// html link to the user profile + pub html_link: &'a str, + /// OPTIONAL: html link to the user's profile photo + pub profile_photo: Option<&'a str>, +} + #[async_trait] /// Starchart's database requirements. To implement support for $Database, kindly implement this /// trait. @@ -84,6 +98,13 @@ pub trait SCDatabase: std::marker::Send + std::marker::Sync + CloneSPDatabase { /// check if forge type exists async fn forge_type_exists(&self, forge_type: &ForgeImplementation) -> DBResult; + + /// add new user to database + async fn add_user(&self, u: &AddUser) -> DBResult<()>; + + /// check if an user exists. When hostname of a forge instace is provided, username search is + /// done only on that forge + async fn user_exists(&self, username: &str, hostname: Option<&str>) -> DBResult; } /// Trait to clone SCDatabase diff --git a/db/db-core/src/tests.rs b/db/db-core/src/tests.rs index caef4d1..f97c56c 100644 --- a/db/db-core/src/tests.rs +++ b/db/db-core/src/tests.rs @@ -19,13 +19,21 @@ use crate::prelude::*; /// adding forge works -pub async fn adding_forge_works(db: &T, create_forge_msg: CreateForge<'static>) { +pub async fn adding_forge_works( + db: &T, + create_forge_msg: CreateForge<'static>, + add_user_msg: AddUser<'static>, + add_user_msg2: AddUser<'static>, +) { let _ = db.delete_forge_instance(&create_forge_msg.hostname).await; db.create_forge_isntance(&create_forge_msg).await.unwrap(); assert!( db.forge_exists(create_forge_msg.hostname).await.unwrap(), "forge creation failed, forge existance check failure" ); + + db.add_user(&add_user_msg).await.unwrap(); + db.add_user(&add_user_msg2).await.unwrap(); } /// test if all forge type implementations are loaded into DB