From cb9b8d6d0ddebb045d289c8b8df8f5796d85616b Mon Sep 17 00:00:00 2001 From: realaravinth Date: Sat, 30 Apr 2022 23:50:59 +0530 Subject: [PATCH] fix: user_exists was a copy of hostname_exists, now fixed --- db/db-sqlx-sqlite/sqlx-data.json | 36 ++++++++++++++++++++++++++++++++ db/db-sqlx-sqlite/src/lib.rs | 35 ++++++++++++++++++++++--------- 2 files changed, 61 insertions(+), 10 deletions(-) diff --git a/db/db-sqlx-sqlite/sqlx-data.json b/db/db-sqlx-sqlite/sqlx-data.json index 97158d7..922d4cc 100644 --- a/db/db-sqlx-sqlite/sqlx-data.json +++ b/db/db-sqlx-sqlite/sqlx-data.json @@ -18,6 +18,24 @@ }, "query": "SELECT ID FROM starchart_forges WHERE hostname = $1" }, + "2afb17ba3753aa440465a836b46b7a1466f25791cfc4d0acdd38bc2755ae3e86": { + "describe": { + "columns": [ + { + "name": "ID", + "ordinal": 0, + "type_info": "Int64" + } + ], + "nullable": [ + false + ], + "parameters": { + "Right": 1 + } + }, + "query": "SELECT ID FROM starchart_users WHERE username = $1" + }, "30de2d37dd1bd602249cd2adfab499e41105249c20dc58cb360f539d6a782fa1": { "describe": { "columns": [ @@ -46,6 +64,24 @@ }, "query": "INSERT INTO\n starchart_forges (hostname, verified_on, forge_type ) \n VALUES ($1, $2, (SELECT ID FROM starchart_forge_type WHERE name = $3))" }, + "a81dd4b5df666e22fac211092e7b8425d838dd9023aa2b17659352f30831944d": { + "describe": { + "columns": [ + { + "name": "ID", + "ordinal": 0, + "type_info": "Int64" + } + ], + "nullable": [ + false + ], + "parameters": { + "Right": 2 + } + }, + "query": "SELECT ID FROM starchart_users WHERE username = $1 AND \n hostname_id = (SELECT ID FROM starchart_forges WHERE hostname = $2)" + }, "b4985ad11fafa367302ca9c0126b95bc70f6ae387f9de649aabb2ef424f676db": { "describe": { "columns": [], diff --git a/db/db-sqlx-sqlite/src/lib.rs b/db/db-sqlx-sqlite/src/lib.rs index 320fe3f..947cf9f 100644 --- a/db/db-sqlx-sqlite/src/lib.rs +++ b/db/db-sqlx-sqlite/src/lib.rs @@ -191,16 +191,31 @@ impl SCDatabase for Database { /// 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())), + match hostname { + Some(hostname) => match sqlx::query!( + "SELECT ID FROM starchart_users WHERE username = $1 AND + hostname_id = (SELECT ID FROM starchart_forges WHERE hostname = $2)", + username, + hostname, + ) + .fetch_one(&self.pool) + .await + { + Ok(_) => Ok(true), + Err(Error::RowNotFound) => Ok(false), + Err(e) => Err(DBError::DBError(Box::new(e).into())), + }, + None => match sqlx::query!( + "SELECT ID FROM starchart_users WHERE username = $1", + username + ) + .fetch_one(&self.pool) + .await + { + Ok(_) => Ok(true), + Err(Error::RowNotFound) => Ok(false), + Err(e) => Err(DBError::DBError(Box::new(e).into())), + }, } } }