2025-03-20 22:41:20 +07:00
|
|
|
use crate::{AppState, RedisKeyEnum, UsersSchema};
|
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-20 22:41:20 +07:00
|
|
|
pub fn query_store_user_data(&self, user: UsersSchema) -> Result<String> {
|
2025-03-14 17:50:37 +07:00
|
|
|
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)),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-20 22:41:20 +07:00
|
|
|
pub fn query_get_stored_user(&self, email: String) -> Result<UsersSchema> {
|
2025-03-14 17:50:37 +07:00
|
|
|
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) => {
|
2025-03-20 22:41:20 +07:00
|
|
|
let user: UsersSchema = serde_json::from_str(&user_json)?;
|
2025-03-14 17:50:37 +07:00
|
|
|
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-02-26 15:45:05 +07:00
|
|
|
}
|