From 6f21a8b49ea048c01ad05f7516f8953dedebe6b6 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Wed, 20 Apr 2022 16:27:56 +0530 Subject: [PATCH] feat: implement add_user and user_exists for sqlite --- .../20220405113942_world_forges.sql | 3 ++ db/db-sqlx-sqlite/sqlx-data.json | 10 +++++ db/db-sqlx-sqlite/src/lib.rs | 41 +++++++++++++++++++ db/db-sqlx-sqlite/src/tests.rs | 22 +++++++++- 4 files changed, 74 insertions(+), 2 deletions(-) diff --git a/db/db-sqlx-sqlite/migrations/20220405113942_world_forges.sql b/db/db-sqlx-sqlite/migrations/20220405113942_world_forges.sql index 08161ce..a30b92f 100644 --- a/db/db-sqlx-sqlite/migrations/20220405113942_world_forges.sql +++ b/db/db-sqlx-sqlite/migrations/20220405113942_world_forges.sql @@ -24,6 +24,9 @@ CREATE TABLE IF NOT EXISTS starchart_users ( hostname_id INTEGER NOT NULL REFERENCES starchart_forges(ID) ON DELETE CASCADE, username TEXT NOT NULL, html_url TEXT NOT NULL UNIQUE, + profile_photo_html_url TEXT DEFAULT NULL UNIQUE, + added_on INTEGER NOT NULL, + last_crawl_on INTEGER NOT NULL, ID INTEGER PRIMARY KEY NOT NULL ); diff --git a/db/db-sqlx-sqlite/sqlx-data.json b/db/db-sqlx-sqlite/sqlx-data.json index dbb7a20..97158d7 100644 --- a/db/db-sqlx-sqlite/sqlx-data.json +++ b/db/db-sqlx-sqlite/sqlx-data.json @@ -46,6 +46,16 @@ }, "query": "INSERT INTO\n starchart_forges (hostname, verified_on, forge_type ) \n VALUES ($1, $2, (SELECT ID FROM starchart_forge_type WHERE name = $3))" }, + "b4985ad11fafa367302ca9c0126b95bc70f6ae387f9de649aabb2ef424f676db": { + "describe": { + "columns": [], + "nullable": [], + "parameters": { + "Right": 6 + } + }, + "query": "INSERT INTO \n starchart_users (\n hostname_id, username, html_url,\n profile_photo_html_url, added_on, last_crawl_on\n ) \n VALUES (\n (SELECT ID FROM starchart_forges WHERE hostname = $1), $2, $3, $4, $5, $6)" + }, "f52cde89ec10d5ca2151c9df6ae273ee0d52af9f79bb776765cfa716aad6af53": { "describe": { "columns": [], diff --git a/db/db-sqlx-sqlite/src/lib.rs b/db/db-sqlx-sqlite/src/lib.rs index 33f697d..320fe3f 100644 --- a/db/db-sqlx-sqlite/src/lib.rs +++ b/db/db-sqlx-sqlite/src/lib.rs @@ -162,6 +162,47 @@ impl SCDatabase for Database { Err(e) => Err(DBError::DBError(Box::new(e)).into()), } } + + /// add new user to database + async fn add_user(&self, u: &AddUser) -> DBResult<()> { + let now = now_unix_time_stamp(); + sqlx::query!( + "INSERT INTO + starchart_users ( + hostname_id, username, html_url, + profile_photo_html_url, added_on, last_crawl_on + ) + VALUES ( + (SELECT ID FROM starchart_forges WHERE hostname = $1), $2, $3, $4, $5, $6)", + u.hostname, + u.username, + u.html_link, + u.profile_photo, + now, + now + ) + .execute(&self.pool) + .await + .map_err(map_register_err)?; + + Ok(()) + } + + /// 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 { + match sqlx::query!( + "SELECT ID FROM starchart_forges WHERE hostname = $1", + hostname + ) + .fetch_one(&self.pool) + .await + { + Ok(_) => Ok(true), + Err(Error::RowNotFound) => Ok(false), + Err(e) => Err(DBError::DBError(Box::new(e).into())), + } + } } fn now_unix_time_stamp() -> i64 { diff --git a/db/db-sqlx-sqlite/src/tests.rs b/db/db-sqlx-sqlite/src/tests.rs index d94a389..f48b546 100644 --- a/db/db-sqlx-sqlite/src/tests.rs +++ b/db/db-sqlx-sqlite/src/tests.rs @@ -24,16 +24,34 @@ use db_core::tests::*; #[actix_rt::test] async fn everything_works() { const HOSTNAME: &str = "test-gitea.example.com"; - let msg = CreateForge { + const HTML_PROFILE_URL: &str = "https://test-gitea.example.com/user1"; + const HTML_PROFILE_PHOTO_URL_2: &str = "https://test-gitea.example.com/profile-photo/user2"; + const USERNAME: &str = "user1"; + const USERNAME2: &str = "user2"; + let create_forge_msg = CreateForge { hostname: HOSTNAME, forge_type: ForgeImplementation::Gitea, }; + + let add_user_msg = AddUser { + hostname: HOSTNAME, + html_link: HTML_PROFILE_URL, + profile_photo: None, + username: USERNAME, + }; + + let add_user_msg_2 = AddUser { + hostname: HOSTNAME, + html_link: HTML_PROFILE_PHOTO_URL_2, + profile_photo: Some(HTML_PROFILE_PHOTO_URL_2), + username: USERNAME2, + }; let url = env::var("SQLITE_DATABASE_URL").expect("Set SQLITE_DATABASE_URL env var"); let pool_options = SqlitePoolOptions::new().max_connections(2); let connection_options = ConnectionOptions::Fresh(Fresh { pool_options, url }); let db = connection_options.connect().await.unwrap(); - adding_forge_works(&db, msg).await; + adding_forge_works(&db, create_forge_msg, add_user_msg, add_user_msg_2).await; } #[actix_rt::test]