2025-03-14 09:53:20 +07:00
|
|
|
use crate::{v1::UsersItemDto, AppState, ResourceEnum};
|
2025-03-13 23:40:16 +07:00
|
|
|
use std::error::Error;
|
2025-02-08 00:58:54 +07:00
|
|
|
|
2025-03-13 23:40:16 +07:00
|
|
|
use super::AuthRegisterRequestDto;
|
2025-02-25 02:00:19 +07:00
|
|
|
|
2025-03-14 09:53:20 +07:00
|
|
|
pub struct AuthRepository<'a> {
|
|
|
|
|
state: &'a AppState,
|
2025-02-08 00:58:54 +07:00
|
|
|
}
|
2025-02-26 15:45:05 +07:00
|
|
|
|
2025-03-14 09:53:20 +07:00
|
|
|
impl<'a> AuthRepository<'a> {
|
|
|
|
|
pub fn new(state: &'a AppState) -> Self {
|
|
|
|
|
Self { state }
|
|
|
|
|
}
|
2025-02-26 15:45:05 +07:00
|
|
|
|
2025-03-14 09:53:20 +07:00
|
|
|
pub async fn query_user_by_email(
|
|
|
|
|
&self,
|
|
|
|
|
email: String,
|
|
|
|
|
) -> Result<AuthRegisterRequestDto, Box<dyn Error>> {
|
|
|
|
|
let db = &self.state.surrealdb;
|
2025-02-26 15:45:05 +07:00
|
|
|
|
2025-03-14 09:53:20 +07:00
|
|
|
let result = db.select((ResourceEnum::Users.to_string(), email)).await?;
|
|
|
|
|
|
|
|
|
|
match result {
|
|
|
|
|
Some(user) => Ok(user),
|
|
|
|
|
None => Err("User not found for email".into()),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn query_create_user(
|
|
|
|
|
&self,
|
|
|
|
|
data: AuthRegisterRequestDto,
|
|
|
|
|
) -> Result<String, Box<dyn Error>> {
|
|
|
|
|
let db = &self.state.surrealdb;
|
|
|
|
|
|
|
|
|
|
let record: Option<UsersItemDto> = db
|
|
|
|
|
.create((ResourceEnum::Users.to_string(), &data.email))
|
|
|
|
|
.content(data)
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
match record {
|
|
|
|
|
Some(_) => Ok("Success create user".into()),
|
|
|
|
|
None => Err("Failed to create user".into()),
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-02-26 15:45:05 +07:00
|
|
|
}
|