This repository has been archived on 2024-08-23. You can view files and clone it, but cannot push or open issues or pull requests.
project/src/db/users.rs

104 lines
2.6 KiB
Rust
Raw Normal View History

2024-07-30 17:21:33 +00:00
use futures::{stream::BoxStream, Stream, TryStreamExt};
use serde::Serialize;
use uuid::Uuid;
use crate::Pool;
/// Creates user and returns its id
2024-08-03 12:49:40 +00:00
pub async fn create_user(
user_name: &str,
user_email: &str,
hashed_password: &[u8],
pool: &Pool,
2024-08-04 06:48:41 +00:00
) -> sqlx::Result<Option<i32>> {
let Some(record) = sqlx::query!(
2024-08-03 12:49:40 +00:00
"INSERT INTO users(username, email, hashed_password) VALUES ($1, $2, $3) RETURNING user_id",
2024-07-30 17:21:33 +00:00
user_name,
2024-08-03 12:49:40 +00:00
user_email,
hashed_password
2024-07-30 17:21:33 +00:00
)
2024-08-04 06:48:41 +00:00
.fetch_optional(pool)
2024-07-30 17:21:33 +00:00
.await?
2024-08-04 06:48:41 +00:00
else {
return Ok(None);
};
let id = record.user_id;
2024-07-30 17:21:33 +00:00
sqlx::query!(
"INSERT INTO folders(owner_id, folder_name) VALUES ($1, $2)",
id,
"ROOT"
)
.execute(pool)
.await?;
2024-08-04 06:48:41 +00:00
Ok(Some(id))
2024-07-30 17:21:33 +00:00
}
/// Deletes the user and returns the files that must be deleted
pub fn delete_user(user_id: i32, pool: &Pool) -> impl Stream<Item = sqlx::Result<Uuid>> + '_ {
sqlx::query_file!("sql/delete_user.sql", user_id)
.fetch(pool)
.map_ok(|record| record.file_id)
}
#[derive(Serialize, Debug)]
pub struct UserInfo {
user_id: i32,
username: String,
email: String,
}
pub async fn update(
user_id: i32,
username: &str,
email: &str,
pool: &Pool,
) -> sqlx::Result<UserInfo> {
sqlx::query_as!(
UserInfo,
2024-08-03 12:49:40 +00:00
"UPDATE users SET username = $2, email = $3 WHERE user_id = $1 RETURNING user_id, username, email",
2024-07-30 17:21:33 +00:00
user_id,
username,
email
)
.fetch_one(pool)
.await
}
pub async fn exists(user_id: i32, pool: &Pool) -> sqlx::Result<bool> {
sqlx::query!(
"SELECT EXISTS(SELECT user_id FROM users WHERE user_id = $1)",
user_id
)
.fetch_one(pool)
.await
.map(|record| record.exists.unwrap_or(false))
}
pub async fn get(user_id: i32, pool: &Pool) -> sqlx::Result<Option<UserInfo>> {
2024-07-30 17:21:33 +00:00
sqlx::query_as!(
UserInfo,
"SELECT user_id, username, email FROM users WHERE user_id = $1",
user_id
)
.fetch_optional(pool)
2024-07-30 17:21:33 +00:00
.await
}
2024-08-03 12:49:40 +00:00
/// Gets the hashed password field by either the email or th username
pub async fn get_hash(search_string: &str, pool: &Pool) -> sqlx::Result<Option<(i32, Vec<u8>)>> {
let record = sqlx::query!(
"SELECT user_id, hashed_password FROM users WHERE username = $1 OR email = $1",
search_string
)
.fetch_optional(pool)
.await?;
Ok(record.map(|record| (record.user_id, record.hashed_password)))
}
2024-07-30 17:21:33 +00:00
pub fn search_for_user<'a>(
search_string: &str,
pool: &'a Pool,
) -> BoxStream<'a, sqlx::Result<UserInfo>> {
sqlx::query_file_as!(UserInfo, "sql/search_for_user.sql", search_string).fetch(pool)
}