feat: add cargo
This commit is contained in:
@@ -6,6 +6,7 @@ pkgs.mkShell {
|
|||||||
rustfmt
|
rustfmt
|
||||||
crate2nix
|
crate2nix
|
||||||
clippy
|
clippy
|
||||||
|
surrealdb
|
||||||
|
|
||||||
(writeScriptBin "helpme" ''
|
(writeScriptBin "helpme" ''
|
||||||
__usage="
|
__usage="
|
||||||
|
|||||||
@@ -6,3 +6,17 @@ pub struct AuthLoginRequestDto {
|
|||||||
pub email: String,
|
pub email: String,
|
||||||
pub password: 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,
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
use super::auth_dto::AuthLoginRequestDto;
|
use super::{auth_dto::AuthLoginRequestDto, AuthRegisterRequestDto};
|
||||||
use crate::{success_response, AppState, ResponseSuccessDto};
|
use crate::{
|
||||||
use axum::response::Response;
|
common_response, hash_password, success_response, v1::UsersItemDto, AppState,
|
||||||
|
ResponseSuccessDto,
|
||||||
|
};
|
||||||
|
use axum::{http::StatusCode, response::Response};
|
||||||
|
use serde_json;
|
||||||
|
|
||||||
const USERS_KEY: &str = "users";
|
const USERS_KEY: &str = "users";
|
||||||
|
|
||||||
@@ -8,28 +12,96 @@ pub async fn mutation_login(
|
|||||||
params: AuthLoginRequestDto,
|
params: AuthLoginRequestDto,
|
||||||
state: &AppState,
|
state: &AppState,
|
||||||
) -> Response {
|
) -> Response {
|
||||||
let _users: Option<AuthLoginRequestDto> = state
|
let user: Option<AuthLoginRequestDto> = match state
|
||||||
.surrealdb
|
.surrealdb
|
||||||
.select((USERS_KEY, &*params.email))
|
.select((USERS_KEY, params.email.as_str()))
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
{
|
||||||
|
Ok(user) => user,
|
||||||
let mut redis_conn = state
|
Err(err) => {
|
||||||
.redisdb
|
return common_response(
|
||||||
.get_connection()
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
.expect("failed to get connection");
|
&err.to_string(),
|
||||||
|
);
|
||||||
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,
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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)
|
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<UsersItemDto> =
|
||||||
|
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")
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
use axum::Router;
|
use axum::Router;
|
||||||
|
|
||||||
pub mod auth;
|
pub mod auth;
|
||||||
|
pub mod users;
|
||||||
|
|
||||||
|
pub use auth::*;
|
||||||
|
pub use users::*;
|
||||||
|
|
||||||
pub async fn routes() -> Router {
|
pub async fn routes() -> Router {
|
||||||
Router::new().nest("/auth", auth::auth_router())
|
Router::new().nest("/auth", auth::auth_router())
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
pub mod users_dto;
|
||||||
|
|
||||||
|
pub use users_dto::*;
|
||||||
@@ -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,
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user