feat: apply env
This commit is contained in:
@@ -1,9 +1,6 @@
|
||||
use crate::{common_response, decode_access_token, AppState};
|
||||
use crate::{common_response, extract_email, AppState};
|
||||
use axum::{
|
||||
extract::Request,
|
||||
http::{header::AUTHORIZATION, StatusCode},
|
||||
middleware::Next,
|
||||
response::Response,
|
||||
extract::Request, http::StatusCode, middleware::Next, response::Response,
|
||||
Extension,
|
||||
};
|
||||
use std::convert::Infallible;
|
||||
@@ -15,42 +12,30 @@ pub async fn auth_middleware(
|
||||
mut req: Request,
|
||||
next: Next,
|
||||
) -> Result<Response, Infallible> {
|
||||
let auth_header = match req.headers().get(AUTHORIZATION) {
|
||||
Some(h) => h.to_str().unwrap_or_default(),
|
||||
let headers = req.headers();
|
||||
|
||||
let email = match extract_email(headers) {
|
||||
Some(email) => email,
|
||||
None => {
|
||||
return Ok(common_response(
|
||||
StatusCode::UNAUTHORIZED,
|
||||
"You are not authorized",
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
let token = auth_header.strip_prefix("Bearer ").unwrap_or("");
|
||||
|
||||
let token_data = match decode_access_token(token) {
|
||||
Ok(data) => data,
|
||||
Err(_) => {
|
||||
return Ok(common_response(
|
||||
StatusCode::UNAUTHORIZED,
|
||||
&format!("Invalid or expired token"),
|
||||
"Invalid or expired token",
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
let repository = AuthRepository::new(&state);
|
||||
|
||||
let user: Option<AuthQueryByEmailResponse> = match repository
|
||||
.query_user_by_email(token_data.claims.sub.clone())
|
||||
.await
|
||||
{
|
||||
Ok(user) => Some(user),
|
||||
Err(err) => {
|
||||
return Ok(common_response(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
&format!("DB error: {}", err),
|
||||
))
|
||||
}
|
||||
};
|
||||
let user: Option<AuthQueryByEmailResponse> =
|
||||
match repository.query_user_by_email(email).await {
|
||||
Ok(user) => Some(user),
|
||||
Err(err) => {
|
||||
return Ok(common_response(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
&format!("DB error: {}", err),
|
||||
))
|
||||
}
|
||||
};
|
||||
|
||||
if user.is_none() {
|
||||
return Ok(common_response(
|
||||
|
||||
@@ -1,22 +1,23 @@
|
||||
use super::{GachaRequestDto, GachaService};
|
||||
use super::{GachaClaimRequestDto, GachaService};
|
||||
use crate::{v1::GachaCreateItemRequestDto, AppState, MessageResponseDto};
|
||||
use axum::{response::IntoResponse, Extension, Json};
|
||||
use axum::{http::HeaderMap, response::IntoResponse, Extension, Json};
|
||||
|
||||
#[utoipa::path(
|
||||
post,
|
||||
path = "/v1/gacha/create",
|
||||
request_body = GachaRequestDto,
|
||||
path = "/v1/gacha/create/claims",
|
||||
request_body = GachaClaimRequestDto,
|
||||
responses(
|
||||
(status = 200, description = "Create gacha successful", body = MessageResponseDto),
|
||||
(status = 401, description = "Create gacha failed", body = MessageResponseDto)
|
||||
(status = 200, description = "Create gacha claims successful", body = MessageResponseDto),
|
||||
(status = 401, description = "Create gacha claims failed", body = MessageResponseDto)
|
||||
),
|
||||
tag = "Gacha"
|
||||
)]
|
||||
pub async fn post_create_gacha(
|
||||
pub async fn post_create_gacha_claims(
|
||||
header: HeaderMap,
|
||||
Extension(state): Extension<AppState>,
|
||||
Json(payload): Json<GachaRequestDto>,
|
||||
Json(payload): Json<GachaClaimRequestDto>,
|
||||
) -> impl IntoResponse {
|
||||
GachaService::mutation_create_gacha(payload, &state).await
|
||||
GachaService::mutation_create_gacha_claims(payload, &state, header).await
|
||||
}
|
||||
|
||||
#[utoipa::path(
|
||||
|
||||
@@ -4,9 +4,7 @@ use utoipa::ToSchema;
|
||||
use crate::v1::UsersItemDto;
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
|
||||
pub struct GachaRequestDto {
|
||||
pub email: String,
|
||||
pub fullname: String,
|
||||
pub struct GachaClaimRequestDto {
|
||||
pub transaction_number: String,
|
||||
}
|
||||
|
||||
@@ -16,6 +14,12 @@ pub struct GachaCreateItemRequestDto {
|
||||
pub item_image: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
|
||||
pub struct GachaCreateRollRequestDto {
|
||||
pub item_id: String,
|
||||
pub weight: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
|
||||
pub struct GachaItemResponseDto {
|
||||
pub item_name: String,
|
||||
@@ -23,7 +27,7 @@ pub struct GachaItemResponseDto {
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
|
||||
pub struct GachaResponseDto {
|
||||
pub struct GachaClaimResponseDto {
|
||||
pub transaction_number: String,
|
||||
pub user: UsersItemDto,
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use super::{
|
||||
GachaCreateItemRequestDto, GachaItemSchema, GachaRequestDto, GachaResponseDto,
|
||||
GachaSchema,
|
||||
GachaClaimRequestDto, GachaClaimResponseDto, GachaCreateItemRequestDto,
|
||||
GachaItemSchema, GachaSchema,
|
||||
};
|
||||
use crate::{v1::AuthRepository, AppState, ResourceEnum};
|
||||
use anyhow::{bail, Result};
|
||||
@@ -18,7 +18,7 @@ impl<'a> GachaRepository<'a> {
|
||||
pub async fn query_gacha_by_transaction_number(
|
||||
&self,
|
||||
transaction_number: String,
|
||||
) -> Result<GachaResponseDto> {
|
||||
) -> Result<GachaClaimResponseDto> {
|
||||
let db = &self.state.surrealdb;
|
||||
|
||||
let result = db
|
||||
@@ -31,13 +31,15 @@ impl<'a> GachaRepository<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn query_create_gacha(&self, data: GachaRequestDto) -> Result<String> {
|
||||
pub async fn query_create_gacha_claims(
|
||||
&self,
|
||||
data: GachaClaimRequestDto,
|
||||
email: String,
|
||||
) -> Result<String> {
|
||||
let auth_repository = AuthRepository::new(self.state);
|
||||
let db = &self.state.surrealdb;
|
||||
|
||||
let user = auth_repository
|
||||
.query_user_by_email(data.email.clone())
|
||||
.await?;
|
||||
let user = auth_repository.query_user_by_email(email).await?;
|
||||
|
||||
let user_thing =
|
||||
Thing::from((ResourceEnum::Users.to_string(), Id::String(user.email)));
|
||||
@@ -54,8 +56,8 @@ impl<'a> GachaRepository<'a> {
|
||||
.await?;
|
||||
|
||||
match record {
|
||||
Some(_) => Ok("Gacha successfully created".to_string()),
|
||||
None => bail!("Failed to create gacha record"),
|
||||
Some(_) => Ok("Gacha claims successfully created".to_string()),
|
||||
None => bail!("Failed to create gacha claims"),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,4 +80,24 @@ impl<'a> GachaRepository<'a> {
|
||||
None => bail!("Failed to create gacha item record"),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn query_create_gacha_roll(
|
||||
&self,
|
||||
data: GachaCreateItemRequestDto,
|
||||
) -> Result<String> {
|
||||
let db = &self.state.surrealdb;
|
||||
|
||||
let record: Option<GachaItemSchema> = db
|
||||
.create((ResourceEnum::Gacha.to_string(), data.item_name.clone()))
|
||||
.content(GachaItemSchema {
|
||||
item_name: data.item_name.clone(),
|
||||
item_image: data.item_image.clone(),
|
||||
})
|
||||
.await?;
|
||||
|
||||
match record {
|
||||
Some(_) => Ok("Gacha item successfully created".to_string()),
|
||||
None => bail!("Failed to create gacha item record"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,31 @@
|
||||
use super::{GachaCreateItemRequestDto, GachaRepository, GachaRequestDto};
|
||||
use crate::{common_response, AppState};
|
||||
use axum::{http::StatusCode, response::Response};
|
||||
use super::{GachaClaimRequestDto, GachaCreateItemRequestDto, GachaRepository};
|
||||
use crate::{common_response, extract_email, AppState};
|
||||
use axum::{
|
||||
http::{HeaderMap, StatusCode},
|
||||
response::Response,
|
||||
};
|
||||
|
||||
pub struct GachaService;
|
||||
|
||||
impl GachaService {
|
||||
pub async fn mutation_create_gacha(
|
||||
payload: GachaRequestDto,
|
||||
pub async fn mutation_create_gacha_claims(
|
||||
payload: GachaClaimRequestDto,
|
||||
state: &AppState,
|
||||
header: HeaderMap,
|
||||
) -> Response {
|
||||
let repository = GachaRepository::new(state);
|
||||
|
||||
match repository.query_create_gacha(payload).await {
|
||||
let email = match extract_email(&header) {
|
||||
Some(email) => email,
|
||||
None => {
|
||||
return common_response(
|
||||
StatusCode::UNAUTHORIZED,
|
||||
"Invalid or expired token",
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
match repository.query_create_gacha_claims(payload, email).await {
|
||||
Ok(msg) => common_response(StatusCode::CREATED, &msg),
|
||||
Err(err) => common_response(StatusCode::BAD_REQUEST, &err.to_string()),
|
||||
}
|
||||
|
||||
@@ -13,7 +13,10 @@ pub use gacha_service::*;
|
||||
|
||||
pub fn gacha_router() -> Router {
|
||||
Router::new()
|
||||
.route("/create", post(gacha_controller::post_create_gacha))
|
||||
.route(
|
||||
"/create/claims",
|
||||
post(gacha_controller::post_create_gacha_claims),
|
||||
)
|
||||
.route(
|
||||
"/create/item",
|
||||
post(gacha_controller::post_create_gacha_item),
|
||||
|
||||
Reference in New Issue
Block a user