From 8b2769706e52444eb7fcda4f0bd8628d2bdf4665 Mon Sep 17 00:00:00 2001 From: Maulana Sodiqin Date: Wed, 26 Feb 2025 15:45:05 +0700 Subject: [PATCH] feat: add cargo --- shell.nix | 1 + src/apps/v1/auth/auth_dto.rs | 14 ++++ src/apps/v1/auth/auth_repository.rs | 118 ++++++++++++++++++++++------ src/apps/v1/mod.rs | 4 + src/apps/v1/users/.gitkeep | 0 src/apps/v1/users/mod.rs | 3 + src/apps/v1/users/users_dto.rs | 8 ++ 7 files changed, 125 insertions(+), 23 deletions(-) delete mode 100644 src/apps/v1/users/.gitkeep create mode 100644 src/apps/v1/users/mod.rs create mode 100644 src/apps/v1/users/users_dto.rs diff --git a/shell.nix b/shell.nix index 1901d30..45b298a 100644 --- a/shell.nix +++ b/shell.nix @@ -6,6 +6,7 @@ pkgs.mkShell { rustfmt crate2nix clippy + surrealdb (writeScriptBin "helpme" '' __usage=" diff --git a/src/apps/v1/auth/auth_dto.rs b/src/apps/v1/auth/auth_dto.rs index 81fbe56..e77ab8f 100644 --- a/src/apps/v1/auth/auth_dto.rs +++ b/src/apps/v1/auth/auth_dto.rs @@ -6,3 +6,17 @@ pub struct AuthLoginRequestDto { pub email: String, pub password: String, } + +#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)] +pub struct AuthRegisterRequestDto { + pub email: String, + pub password: String, + pub fullname: String, +} + +#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)] +pub struct AuthRegisterRequestDto { + pub email: String, + pub password: String, + pub fullname: String, +} diff --git a/src/apps/v1/auth/auth_repository.rs b/src/apps/v1/auth/auth_repository.rs index 8f8ebd4..aafeb2e 100644 --- a/src/apps/v1/auth/auth_repository.rs +++ b/src/apps/v1/auth/auth_repository.rs @@ -1,6 +1,10 @@ -use super::auth_dto::AuthLoginRequestDto; -use crate::{success_response, AppState, ResponseSuccessDto}; -use axum::response::Response; +use super::{auth_dto::AuthLoginRequestDto, AuthRegisterRequestDto}; +use crate::{ + common_response, hash_password, success_response, v1::UsersItemDto, AppState, + ResponseSuccessDto, +}; +use axum::{http::StatusCode, response::Response}; +use serde_json; const USERS_KEY: &str = "users"; @@ -8,28 +12,96 @@ pub async fn mutation_login( params: AuthLoginRequestDto, state: &AppState, ) -> Response { - let _users: Option = state + let user: Option = match state .surrealdb - .select((USERS_KEY, &*params.email)) + .select((USERS_KEY, params.email.as_str())) .await - .unwrap(); - - let mut redis_conn = state - .redisdb - .get_connection() - .expect("failed to get connection"); - - redis::cmd("SET") - .arg("some_key") - .arg("some_value") - .query::<()>(&mut redis_conn) - .expect("failed to set value"); - - let response = ResponseSuccessDto { - data: AuthLoginRequestDto { - email: params.email, - password: params.password, - }, + { + Ok(user) => user, + Err(err) => { + return common_response( + StatusCode::INTERNAL_SERVER_ERROR, + &err.to_string(), + ); + } }; + + let mut redis_conn = match state.redisdb.get_connection() { + Ok(conn) => conn, + Err(err) => { + return common_response( + StatusCode::INTERNAL_SERVER_ERROR, + &err.to_string(), + ); + } + }; + + let user_json = match serde_json::to_string(&user) { + Ok(json) => json, + Err(err) => { + return common_response( + StatusCode::INTERNAL_SERVER_ERROR, + &err.to_string(), + ); + } + }; + + if let Err(err) = redis::cmd("SET") + .arg("users_data") + .arg(user_json) + .query::<()>(&mut redis_conn) + { + return common_response(StatusCode::INTERNAL_SERVER_ERROR, &err.to_string()); + } + + let response = ResponseSuccessDto { data: params }; + success_response(response) } + +pub async fn mutation_register( + params: AuthRegisterRequestDto, + state: &AppState, +) -> Response { + let user_key = format!("{}:{}", USERS_KEY, params.email); + + let existing_user: Option = + match state.surrealdb.select(&user_key).await { + Ok(user) => user, + Err(err) => { + return common_response( + StatusCode::INTERNAL_SERVER_ERROR, + &err.to_string(), + ); + } + }; + + if existing_user.is_some() { + return common_response(StatusCode::CONFLICT, "User already exists"); + } + + let hashed_password = hash_password(¶ms.password); + + let created_user: AuthLoginRequestDto = + match state.surrealdb.create(&user_key, ¶ms).await { + Ok(user) => Some(user), + Err(err) => { + return common_response( + StatusCode::INTERNAL_SERVER_ERROR, + &err.to_string(), + ); + } + }; + + let user_json = match serde_json::to_string(&created_user) { + Ok(json) => json, + Err(err) => { + return common_response( + StatusCode::INTERNAL_SERVER_ERROR, + &err.to_string(), + ); + } + }; + + common_response(StatusCode::CREATED, "Success Register User") +} diff --git a/src/apps/v1/mod.rs b/src/apps/v1/mod.rs index 21d1c08..b9b4e93 100644 --- a/src/apps/v1/mod.rs +++ b/src/apps/v1/mod.rs @@ -1,6 +1,10 @@ use axum::Router; pub mod auth; +pub mod users; + +pub use auth::*; +pub use users::*; pub async fn routes() -> Router { Router::new().nest("/auth", auth::auth_router()) diff --git a/src/apps/v1/users/.gitkeep b/src/apps/v1/users/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/src/apps/v1/users/mod.rs b/src/apps/v1/users/mod.rs new file mode 100644 index 0000000..7b39807 --- /dev/null +++ b/src/apps/v1/users/mod.rs @@ -0,0 +1,3 @@ +pub mod users_dto; + +pub use users_dto::*; diff --git a/src/apps/v1/users/users_dto.rs b/src/apps/v1/users/users_dto.rs new file mode 100644 index 0000000..3828896 --- /dev/null +++ b/src/apps/v1/users/users_dto.rs @@ -0,0 +1,8 @@ +use serde::{Deserialize, Serialize}; +use utoipa::ToSchema; + +#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)] +pub struct UsersItemDto { + pub email: String, + pub fullname: String, +}