feat: user relation
This commit is contained in:
@@ -1,7 +1,19 @@
|
||||
use super::{AuthLoginRequestDto, AuthRegisterRequestDto, AuthService};
|
||||
use crate::AppState;
|
||||
use crate::{v1::AuthLoginResponsetDto, AppState};
|
||||
use axum::{response::IntoResponse, Extension, Json};
|
||||
|
||||
use crate::{MessageResponseDto, ResponseSuccessDto};
|
||||
|
||||
#[utoipa::path(
|
||||
post,
|
||||
path = "/v1/auth/login",
|
||||
request_body = AuthLoginRequestDto,
|
||||
responses(
|
||||
(status = 200, description = "Login successful", body = ResponseSuccessDto<AuthLoginResponsetDto>),
|
||||
(status = 401, description = "Unauthorized", body = MessageResponseDto)
|
||||
),
|
||||
tag = "Authentication"
|
||||
)]
|
||||
pub async fn post_login(
|
||||
Extension(state): Extension<AppState>,
|
||||
Json(payload): Json<AuthLoginRequestDto>,
|
||||
@@ -9,6 +21,16 @@ pub async fn post_login(
|
||||
AuthService::mutation_login(payload, &state).await
|
||||
}
|
||||
|
||||
#[utoipa::path(
|
||||
post,
|
||||
path = "/v1/auth/register",
|
||||
request_body = AuthRegisterRequestDto,
|
||||
responses(
|
||||
(status = 200, description = "Login successful", body = MessageResponseDto),
|
||||
(status = 401, description = "Unauthorized", body = MessageResponseDto)
|
||||
),
|
||||
tag = "Authentication"
|
||||
)]
|
||||
pub async fn post_register(
|
||||
Extension(state): Extension<AppState>,
|
||||
Json(payload): Json<AuthRegisterRequestDto>,
|
||||
|
||||
@@ -27,3 +27,10 @@ pub struct AuthRegisterRequestDto {
|
||||
pub password: String,
|
||||
pub fullname: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
|
||||
pub struct AuthQueryByEmailResponse {
|
||||
pub email: String,
|
||||
pub fullname: String,
|
||||
pub password: String,
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
use crate::{v1::UsersItemDto, AppState, ResourceEnum};
|
||||
use std::error::Error;
|
||||
use crate::{v1::UsersItemDto, AppState, RedisKeyEnum, ResourceEnum};
|
||||
use anyhow::{bail, Result};
|
||||
use redis::Commands;
|
||||
|
||||
use super::AuthRegisterRequestDto;
|
||||
use super::{AuthQueryByEmailResponse, AuthRegisterRequestDto};
|
||||
|
||||
pub struct AuthRepository<'a> {
|
||||
state: &'a AppState,
|
||||
@@ -12,24 +13,56 @@ impl<'a> AuthRepository<'a> {
|
||||
Self { state }
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
None => bail!("No stored user data found for email"),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn query_user_by_email(
|
||||
&self,
|
||||
email: String,
|
||||
) -> Result<AuthRegisterRequestDto, Box<dyn Error>> {
|
||||
) -> Result<AuthQueryByEmailResponse> {
|
||||
let db = &self.state.surrealdb;
|
||||
|
||||
let result = db.select((ResourceEnum::Users.to_string(), email)).await?;
|
||||
|
||||
match result {
|
||||
Some(user) => Ok(user),
|
||||
None => Err("User not found for email".into()),
|
||||
Some(response) => Ok(response),
|
||||
None => bail!("User not found"),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn query_create_user(
|
||||
&self,
|
||||
data: AuthRegisterRequestDto,
|
||||
) -> Result<String, Box<dyn Error>> {
|
||||
) -> Result<String> {
|
||||
let db = &self.state.surrealdb;
|
||||
|
||||
let record: Option<UsersItemDto> = db
|
||||
@@ -39,7 +72,7 @@ impl<'a> AuthRepository<'a> {
|
||||
|
||||
match record {
|
||||
Some(_) => Ok("Success create user".into()),
|
||||
None => Err("Failed to create user".into()),
|
||||
None => bail!("Failed to create user"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
use axum::{http::StatusCode, response::Response};
|
||||
use redis::Commands;
|
||||
|
||||
use super::{
|
||||
AuthLoginRequestDto, AuthLoginResponsetDto, AuthRegisterRequestDto,
|
||||
@@ -55,22 +54,21 @@ impl AuthService {
|
||||
},
|
||||
};
|
||||
|
||||
let redis_key =
|
||||
format!("authenticated_users_data:{}", payload.email.clone());
|
||||
|
||||
match state.redisdb.get_connection().and_then(|mut conn| {
|
||||
conn.set_ex::<_, String, ()>(
|
||||
&redis_key,
|
||||
serde_json::to_string(&user).unwrap_or_default(),
|
||||
86400,
|
||||
)
|
||||
}) {
|
||||
Ok(_) => success_response(response),
|
||||
Err(err) => common_response(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
&format!("Redis storage failed: {}", err),
|
||||
),
|
||||
if !repository
|
||||
.query_store_user_data(AuthRegisterRequestDto {
|
||||
fullname: user.fullname,
|
||||
password: user.password,
|
||||
email: user.email,
|
||||
})
|
||||
.is_ok()
|
||||
{
|
||||
return common_response(
|
||||
StatusCode::BAD_REQUEST,
|
||||
"Failed to store data",
|
||||
);
|
||||
}
|
||||
|
||||
success_response(response)
|
||||
}
|
||||
Err(err) => common_response(StatusCode::UNAUTHORIZED, &err.to_string()),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user