feat: implement add_user and user_exists for sqlite
This commit is contained in:
parent
5102531a70
commit
6f21a8b49e
4 changed files with 74 additions and 2 deletions
|
@ -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
|
||||
);
|
||||
|
||||
|
|
|
@ -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": [],
|
||||
|
|
|
@ -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<bool> {
|
||||
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 {
|
||||
|
|
|
@ -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]
|
||||
|
|
Loading…
Reference in a new issue