From f9c49d44b54cfe45141205fd8229d393736ab1e6 Mon Sep 17 00:00:00 2001 From: Maulana Sodiqin Date: Wed, 19 Mar 2025 05:34:39 +0700 Subject: [PATCH] feat: gacha roll --- src/apps/mod.rs | 5 +---- src/apps/v1/auth/auth_repository.rs | 7 ++++++- src/apps/v1/gacha/gacha_controller.rs | 6 +++--- src/apps/v1/gacha/gacha_dto.rs | 1 + src/apps/v1/gacha/gacha_repository.rs | 6 ++++++ src/apps/v1/gacha/gacha_schema.rs | 1 + src/apps/v1/mod.rs | 8 +++++++- 7 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/apps/mod.rs b/src/apps/mod.rs index 646ad6e..e49a135 100644 --- a/src/apps/mod.rs +++ b/src/apps/mod.rs @@ -1,6 +1,5 @@ use crate::{AppState, RedisClient, SurrealClient}; -use axum::{response::Redirect, routing::get, Extension, Router}; -use utoipa_swagger_ui::SwaggerUi; +use axum::{Extension, Router}; pub mod v1; pub mod v2; @@ -8,9 +7,7 @@ pub mod v2; pub async fn apps(surrealdb: SurrealClient, redisdb: RedisClient) -> Router { let state = AppState { surrealdb, redisdb }; Router::new() - .route("/", get(Redirect::to("/docs"))) .nest("/v1", v1::routes().await) .nest("/v2", v2::routes().await) - .merge(SwaggerUi::new("/docs").url("/openapi.json", v1::docs_router())) .layer(Extension(state)) } diff --git a/src/apps/v1/auth/auth_repository.rs b/src/apps/v1/auth/auth_repository.rs index b40733c..7fa3d8c 100644 --- a/src/apps/v1/auth/auth_repository.rs +++ b/src/apps/v1/auth/auth_repository.rs @@ -61,7 +61,12 @@ impl<'a> AuthRepository<'a> { let db = &self.state.surrealdb; let record: Option = db .create((ResourceEnum::Users.to_string(), &data.email)) - .content(data) + .content(UsersSchema { + fullname: data.fullname.clone(), + email: data.email.clone(), + password: data.password.clone(), + is_active: false, + }) .await?; match record { Some(_) => Ok("Success create user".into()), diff --git a/src/apps/v1/gacha/gacha_controller.rs b/src/apps/v1/gacha/gacha_controller.rs index 40197c6..13255f6 100644 --- a/src/apps/v1/gacha/gacha_controller.rs +++ b/src/apps/v1/gacha/gacha_controller.rs @@ -4,11 +4,11 @@ use axum::{http::HeaderMap, response::IntoResponse, Extension, Json}; #[utoipa::path( post, - path = "/v1/gacha/create/claims", + path = "/v1/gacha/create/claim", request_body = GachaCreateClaimRequestDto, responses( - (status = 200, description = "Create gacha claims successful", body = MessageResponseDto), - (status = 401, description = "Create gacha claims failed", body = MessageResponseDto) + (status = 200, description = "Create gacha claim successful", body = MessageResponseDto), + (status = 401, description = "Create gacha claim failed", body = MessageResponseDto) ), tag = "Gacha" )] diff --git a/src/apps/v1/gacha/gacha_dto.rs b/src/apps/v1/gacha/gacha_dto.rs index 418dac2..28d7bd9 100644 --- a/src/apps/v1/gacha/gacha_dto.rs +++ b/src/apps/v1/gacha/gacha_dto.rs @@ -5,6 +5,7 @@ use crate::v1::UsersItemDto; #[derive(Clone, Debug, Serialize, Deserialize, ToSchema)] pub struct GachaCreateClaimRequestDto { + pub item_name: String, pub transaction_number: String, } diff --git a/src/apps/v1/gacha/gacha_repository.rs b/src/apps/v1/gacha/gacha_repository.rs index 1efef81..45ff0e4 100644 --- a/src/apps/v1/gacha/gacha_repository.rs +++ b/src/apps/v1/gacha/gacha_repository.rs @@ -61,6 +61,11 @@ impl<'a> GachaRepository<'a> { let user_thing = Thing::from((ResourceEnum::Users.to_string(), Id::String(user.email))); + let item_thing = Thing::from(( + ResourceEnum::GachaItems.to_string(), + Id::String(data.item_name.clone()), + )); + let record: Option = db .create(( ResourceEnum::GachaClaims.to_string(), @@ -68,6 +73,7 @@ impl<'a> GachaRepository<'a> { )) .content(GachaClaimSchema { transaction_number: data.transaction_number.clone(), + item: item_thing, user: user_thing, }) .await?; diff --git a/src/apps/v1/gacha/gacha_schema.rs b/src/apps/v1/gacha/gacha_schema.rs index 4b1d0f6..12a2e6b 100644 --- a/src/apps/v1/gacha/gacha_schema.rs +++ b/src/apps/v1/gacha/gacha_schema.rs @@ -4,6 +4,7 @@ use surrealdb::sql::Thing; #[derive(Clone, Debug, Serialize, Deserialize)] pub struct GachaClaimSchema { pub transaction_number: String, + pub item: Thing, pub user: Thing, } diff --git a/src/apps/v1/mod.rs b/src/apps/v1/mod.rs index b6dafb4..3d374bc 100644 --- a/src/apps/v1/mod.rs +++ b/src/apps/v1/mod.rs @@ -8,11 +8,17 @@ pub use auth::*; pub use docs::*; pub use gacha::*; pub use users::*; +use utoipa_swagger_ui::SwaggerUi; pub async fn routes() -> Router { let public_routes = Router::new().nest("/auth", auth_router()); + let protected_routes = Router::new() .nest("/gacha", gacha_router()) .layer(from_fn(auth::auth_middleware::auth_middleware)); - Router::new().merge(public_routes).merge(protected_routes) + + Router::new() + .merge(public_routes) + .merge(protected_routes) + .merge(SwaggerUi::new("/v1/docs").url("/openapi.json", docs_router())) }