88 lines
2.5 KiB
Rust
88 lines
2.5 KiB
Rust
use crate::error::{handle_error, Error};
|
|
use sqlx::sqlite::SqliteQueryResult;
|
|
use sqlx::types::chrono::{DateTime, Utc};
|
|
use sqlx::{FromRow, SqliteExecutor};
|
|
|
|
#[derive(FromRow)]
|
|
pub struct Users {
|
|
pub id: String,
|
|
pub created_at: DateTime<Utc>,
|
|
pub updated_at: DateTime<Utc>,
|
|
pub is_admin: bool,
|
|
pub username: String,
|
|
pub name: Option<String>,
|
|
pub email: Option<String>,
|
|
pub password: Option<String>,
|
|
pub password_recover: Option<String>,
|
|
pub paper_key: Option<String>,
|
|
pub is_archived: bool,
|
|
}
|
|
|
|
impl Users {
|
|
pub async fn get_initial_admin(conn: impl SqliteExecutor<'_>) -> Result<Option<Self>, Error> {
|
|
sqlx::query_file_as!(Self, "queries/users/get_initial_admin.sql")
|
|
.fetch_optional(conn)
|
|
.await
|
|
.map_err(handle_error)
|
|
}
|
|
|
|
pub async fn insert(
|
|
conn: impl SqliteExecutor<'_>,
|
|
id: &str,
|
|
is_admin: bool,
|
|
username: &str,
|
|
password: Option<&str>,
|
|
) -> Result<Option<()>, Error> {
|
|
let query: SqliteQueryResult =
|
|
sqlx::query_file!("queries/users/insert.sql", id, is_admin, username, password)
|
|
.execute(conn)
|
|
.await
|
|
.map_err(handle_error)?;
|
|
|
|
Ok((query.rows_affected() == 1).then_some(()))
|
|
}
|
|
|
|
pub async fn get_one_by_id(
|
|
conn: impl SqliteExecutor<'_>,
|
|
id: &str,
|
|
) -> Result<Option<Self>, Error> {
|
|
sqlx::query_file_as!(Self, "queries/users/get_one_by_id.sql", id)
|
|
.fetch_optional(conn)
|
|
.await
|
|
.map_err(handle_error)
|
|
}
|
|
|
|
pub async fn get_one_by_email(
|
|
conn: impl SqliteExecutor<'_>,
|
|
email: &str,
|
|
) -> Result<Option<Self>, Error> {
|
|
sqlx::query_file_as!(Self, "queries/users/get_one_by_email.sql", email)
|
|
.fetch_optional(conn)
|
|
.await
|
|
.map_err(handle_error)
|
|
}
|
|
|
|
pub async fn get_one_by_username(
|
|
conn: impl SqliteExecutor<'_>,
|
|
username: &str,
|
|
) -> Result<Option<Self>, Error> {
|
|
sqlx::query_file_as!(Self, "queries/users/get_one_by_username.sql", username)
|
|
.fetch_optional(conn)
|
|
.await
|
|
.map_err(handle_error)
|
|
}
|
|
|
|
pub async fn get_one_from_authorization_code(
|
|
conn: impl SqliteExecutor<'_>,
|
|
code: &str,
|
|
) -> Result<Option<Self>, Error> {
|
|
sqlx::query_file_as!(
|
|
Self,
|
|
"queries/users/get_one_from_authorization_code.sql",
|
|
code
|
|
)
|
|
.fetch_optional(conn)
|
|
.await
|
|
.map_err(handle_error)
|
|
}
|
|
}
|