59 lines
1.4 KiB
Rust
59 lines
1.4 KiB
Rust
use sqlx::SqlitePool;
|
|
use uuid::Uuid;
|
|
|
|
use crate::error::AppError;
|
|
use crate::models::User;
|
|
|
|
pub struct UserRepository;
|
|
|
|
impl UserRepository {
|
|
pub async fn create(
|
|
pool: &SqlitePool,
|
|
email: &str,
|
|
password_hash: &str,
|
|
) -> Result<User, AppError> {
|
|
let id = Uuid::new_v4().to_string();
|
|
|
|
sqlx::query_as::<_, User>(
|
|
r#"
|
|
INSERT INTO users (id, email, password_hash)
|
|
VALUES (?, ?, ?)
|
|
RETURNING *
|
|
"#,
|
|
)
|
|
.bind(&id)
|
|
.bind(email)
|
|
.bind(password_hash)
|
|
.fetch_one(pool)
|
|
.await
|
|
.map_err(AppError::from)
|
|
}
|
|
|
|
pub async fn find_by_email(pool: &SqlitePool, email: &str) -> Result<Option<User>, AppError> {
|
|
sqlx::query_as::<_, User>(
|
|
r#"
|
|
SELECT * FROM users
|
|
WHERE email = ? AND deleted_at IS NULL
|
|
"#,
|
|
)
|
|
.bind(email)
|
|
.fetch_optional(pool)
|
|
.await
|
|
.map_err(AppError::from)
|
|
}
|
|
|
|
#[allow(dead_code)]
|
|
pub async fn find_by_id(pool: &SqlitePool, id: &str) -> Result<Option<User>, AppError> {
|
|
sqlx::query_as::<_, User>(
|
|
r#"
|
|
SELECT * FROM users
|
|
WHERE id = ? AND deleted_at IS NULL
|
|
"#,
|
|
)
|
|
.bind(id)
|
|
.fetch_optional(pool)
|
|
.await
|
|
.map_err(AppError::from)
|
|
}
|
|
}
|