users: migrations, queries, users crate: create user, get first admin user
This commit is contained in:
parent
8af226cd05
commit
3e168c19bc
13 changed files with 283 additions and 1 deletions
|
|
@ -1,3 +1,5 @@
|
|||
mod settings;
|
||||
mod users;
|
||||
|
||||
pub use settings::Settings;
|
||||
pub use users::Users;
|
||||
|
|
|
|||
44
crates/database/src/tables/users.rs
Normal file
44
crates/database/src/tables/users.rs
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
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(()))
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue