Files
imphnen-backend-service/src/apps/v1/auth/auth_repository.rs
T

165 lines
4.8 KiB
Rust
Raw Normal View History

2025-03-19 23:17:50 +07:00
use super::{
AuthActiveInactiveRequestDto, AuthRegisterRequestDto, AuthSetNewPasswordRequestDto,
};
2025-03-18 14:13:53 +07:00
use crate::{
v1::{users_schema::UsersSchema, UsersItemDto},
AppState, RedisKeyEnum, ResourceEnum,
};
2025-03-19 23:17:50 +07:00
use anyhow::{anyhow, bail, Result};
2025-03-14 17:50:37 +07:00
use redis::Commands;
2025-02-08 00:58:54 +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 17:50:37 +07:00
pub fn query_store_user_data(
&self,
user: AuthRegisterRequestDto,
) -> Result<String> {
let redis_key = format!("{}:{}", RedisKeyEnum::User, user.email.clone());
match &self.state.redisdb.get_connection().and_then(|mut conn| {
conn.set_ex::<_, String, ()>(
&redis_key,
serde_json::to_string(&user).unwrap_or_default(),
86400,
)
}) {
Ok(_) => Ok("Success store user data".to_string()),
Err(err) => Ok(format!("Redis storage failed: {}", err)),
}
}
pub fn query_get_stored_user(&self, email: String) -> Result<UsersItemDto> {
let redis_key = format!("{}:{}", RedisKeyEnum::User, email);
let mut conn = self.state.redisdb.get_connection()?;
let data: Option<String> = conn.get(&redis_key)?;
match data {
Some(user_json) => {
let user: UsersItemDto = serde_json::from_str(&user_json)?;
Ok(user)
}
2025-03-18 14:13:53 +07:00
None => bail!("No stored user data found"),
2025-03-14 17:50:37 +07:00
}
}
2025-03-19 23:17:50 +07:00
pub fn query_get_stored_otp(&self, email: String) -> Result<u32> {
let redis_key = format!("{}:{}", RedisKeyEnum::Otp, email);
let mut conn = match self.state.redisdb.get_connection() {
Ok(conn) => conn,
Err(e) => {
return Err(anyhow::anyhow!("Failed to get Redis connection: {}", e))
}
};
let data: Option<String> = match conn.get(&redis_key) {
Ok(data) => data,
Err(e) => return Err(anyhow::anyhow!("Failed to get data from Redis: {}", e)),
};
match data {
Some(otp_str) => match otp_str.parse::<u32>() {
Ok(otp) => Ok(otp),
Err(e) => Err(anyhow::anyhow!("Failed to parse OTP as u64: {}", e)),
},
None => Err(anyhow::anyhow!("No stored OTP found")),
}
}
pub fn query_store_otp(&self, email: String, otp: u32) -> Result<String> {
let redis_key: String = format!("{}:{}", RedisKeyEnum::Otp, email);
let mut conn = match self.state.redisdb.get_connection() {
Ok(conn) => conn,
Err(e) => return Err(anyhow!("Failed to get Redis connection: {}", e)),
};
let otp_str: String = otp.to_string();
match conn.set_ex::<_, _, ()>(&redis_key, &otp_str, 300) {
Ok(_) => Ok("Success store otp".to_string()),
Err(e) => Err(anyhow!("Failed to store OTP in Redis: {}", e)),
}
}
pub fn query_delete_stored_otp(&self, email: String) -> Result<String> {
let redis_key = format!("{}:{}", RedisKeyEnum::Otp, email);
let mut conn = match self.state.redisdb.get_connection() {
Ok(conn) => conn,
Err(e) => return Err(anyhow!("Failed to get Redis connection: {}", e)),
};
match conn.del::<_, ()>(&redis_key) {
Ok(_) => Ok("Successfully deleted OTP".to_string()),
Err(e) => Err(anyhow!("Failed to delete OTP from Redis: {}", e)),
}
}
2025-03-18 14:13:53 +07:00
pub async fn query_user_by_email(&self, email: String) -> Result<UsersSchema> {
2025-03-14 09:53:20 +07:00
let db = &self.state.surrealdb;
2025-03-19 23:17:50 +07:00
let result = db
.select((ResourceEnum::Users.to_string(), email.clone()))
.await?;
2025-03-14 09:53:20 +07:00
match result {
2025-03-14 17:50:37 +07:00
Some(response) => Ok(response),
2025-03-19 23:17:50 +07:00
None => {
bail!("User not found")
}
2025-03-14 09:53:20 +07:00
}
}
pub async fn query_create_user(
&self,
data: AuthRegisterRequestDto,
2025-03-14 17:50:37 +07:00
) -> Result<String> {
2025-03-14 09:53:20 +07:00
let db = &self.state.surrealdb;
let record: Option<UsersItemDto> = db
.create((ResourceEnum::Users.to_string(), &data.email))
2025-03-19 05:34:39 +07:00
.content(UsersSchema {
fullname: data.fullname.clone(),
email: data.email.clone(),
password: data.password.clone(),
is_active: false,
})
2025-03-14 09:53:20 +07:00
.await?;
match record {
Some(_) => Ok("Success create user".into()),
2025-03-14 17:50:37 +07:00
None => bail!("Failed to create user"),
2025-03-14 09:53:20 +07:00
}
}
2025-03-18 14:13:53 +07:00
pub async fn query_active_inactive_user(
&self,
data: AuthActiveInactiveRequestDto,
) -> Result<String> {
let db = &self.state.surrealdb;
2025-03-19 23:17:50 +07:00
let record: Option<AuthActiveInactiveRequestDto> = db
2025-03-18 14:13:53 +07:00
.update((ResourceEnum::Users.to_string(), &data.email))
2025-03-19 23:17:50 +07:00
.merge(AuthActiveInactiveRequestDto {
email: data.email.clone(),
is_active: data.is_active.clone(),
})
2025-03-18 14:13:53 +07:00
.await?;
match record {
Some(_) => Ok("Success update user".into()),
None => bail!("Failed to update user"),
}
}
2025-03-19 23:17:50 +07:00
pub async fn query_update_password_user(
&self,
data: AuthSetNewPasswordRequestDto,
) -> Result<String> {
let db = &self.state.surrealdb;
let record: Option<AuthSetNewPasswordRequestDto> = db
.update((ResourceEnum::Users.to_string(), &data.email))
.merge(AuthSetNewPasswordRequestDto {
email: data.email.clone(),
password: data.password.clone(),
})
.await?;
match record {
Some(_) => Ok("Success update password user".into()),
None => bail!("Failed to update password user"),
}
}
2025-02-26 15:45:05 +07:00
}