feat: gacha roll, user auth

This commit is contained in:
Maulana Sodiqin
2025-03-18 14:13:53 +07:00
parent 0c28d34d57
commit 12043f027c
17 changed files with 174 additions and 86 deletions
+22 -5
View File
@@ -1,23 +1,23 @@
use super::{GachaClaimRequestDto, GachaService};
use super::{GachaCreateClaimRequestDto, GachaCreateRollRequestDto, GachaService};
use crate::{v1::GachaCreateItemRequestDto, AppState, MessageResponseDto};
use axum::{http::HeaderMap, response::IntoResponse, Extension, Json};
#[utoipa::path(
post,
path = "/v1/gacha/create/claims",
request_body = GachaClaimRequestDto,
request_body = GachaCreateClaimRequestDto,
responses(
(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_claims(
pub async fn post_create_gacha_claim(
header: HeaderMap,
Extension(state): Extension<AppState>,
Json(payload): Json<GachaClaimRequestDto>,
Json(payload): Json<GachaCreateClaimRequestDto>,
) -> impl IntoResponse {
GachaService::mutation_create_gacha_claims(payload, &state, header).await
GachaService::mutation_create_gacha_claim(payload, &state, header).await
}
#[utoipa::path(
@@ -36,3 +36,20 @@ pub async fn post_create_gacha_item(
) -> impl IntoResponse {
GachaService::mutation_create_gacha_item(payload, &state).await
}
#[utoipa::path(
post,
path = "/v1/gacha/create/roll",
request_body = GachaCreateRollRequestDto,
responses(
(status = 200, description = "Create gacha roll successful", body = MessageResponseDto),
(status = 401, description = "Create gacha roll failed", body = MessageResponseDto)
),
tag = "Gacha"
)]
pub async fn post_create_gacha_roll(
Extension(state): Extension<AppState>,
Json(payload): Json<GachaCreateRollRequestDto>,
) -> impl IntoResponse {
GachaService::mutation_create_gacha_roll(payload, &state).await
}
+7 -2
View File
@@ -4,7 +4,7 @@ use utoipa::ToSchema;
use crate::v1::UsersItemDto;
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
pub struct GachaClaimRequestDto {
pub struct GachaCreateClaimRequestDto {
pub transaction_number: String,
}
@@ -16,7 +16,7 @@ pub struct GachaCreateItemRequestDto {
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
pub struct GachaCreateRollRequestDto {
pub item_id: String,
pub item_name: String,
pub weight: String,
}
@@ -31,3 +31,8 @@ pub struct GachaClaimResponseDto {
pub transaction_number: String,
pub user: UsersItemDto,
}
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
pub struct GachaRollResponseDto {
pub item: GachaItemResponseDto,
}
+40 -19
View File
@@ -1,6 +1,7 @@
use super::{
GachaClaimRequestDto, GachaClaimResponseDto, GachaCreateItemRequestDto,
GachaItemSchema, GachaSchema,
GachaClaimResponseDto, GachaClaimSchema, GachaCreateClaimRequestDto,
GachaCreateItemRequestDto, GachaCreateRollRequestDto, GachaItemResponseDto,
GachaItemSchema, GachaRollSchema,
};
use crate::{v1::AuthRepository, AppState, ResourceEnum};
use anyhow::{bail, Result};
@@ -15,25 +16,41 @@ impl<'a> GachaRepository<'a> {
Self { state }
}
pub async fn query_gacha_by_transaction_number(
pub async fn query_gacha_claim_by_transaction_number(
&self,
transaction_number: String,
) -> Result<GachaClaimResponseDto> {
let db = &self.state.surrealdb;
let result = db
.select((ResourceEnum::Gacha.to_string(), transaction_number))
.select((ResourceEnum::GachaClaims.to_string(), transaction_number))
.await?;
match result {
Some(response) => Ok(response),
None => bail!("Gacha not found"),
None => bail!("Gacha claim not found"),
}
}
pub async fn query_create_gacha_claims(
pub async fn query_gacha_item_by_name(
&self,
data: GachaClaimRequestDto,
name: String,
) -> Result<GachaItemResponseDto> {
let db = &self.state.surrealdb;
let result = db
.select((ResourceEnum::GachaItems.to_string(), name))
.await?;
match result {
Some(response) => Ok(response),
None => bail!("Gacha item not found"),
}
}
pub async fn query_create_gacha_claim(
&self,
data: GachaCreateClaimRequestDto,
email: String,
) -> Result<String> {
let auth_repository = AuthRepository::new(self.state);
@@ -44,12 +61,12 @@ impl<'a> GachaRepository<'a> {
let user_thing =
Thing::from((ResourceEnum::Users.to_string(), Id::String(user.email)));
let record: Option<GachaSchema> = db
let record: Option<GachaClaimSchema> = db
.create((
ResourceEnum::GachaClaims.to_string(),
&data.transaction_number,
))
.content(GachaSchema {
.content(GachaClaimSchema {
transaction_number: data.transaction_number.clone(),
user: user_thing,
})
@@ -68,7 +85,7 @@ impl<'a> GachaRepository<'a> {
let db = &self.state.surrealdb;
let record: Option<GachaItemSchema> = db
.create((ResourceEnum::Gacha.to_string(), data.item_name.clone()))
.create((ResourceEnum::GachaItems.to_string(), data.item_name.clone()))
.content(GachaItemSchema {
item_name: data.item_name.clone(),
item_image: data.item_image.clone(),
@@ -77,27 +94,31 @@ impl<'a> GachaRepository<'a> {
match record {
Some(_) => Ok("Gacha item successfully created".to_string()),
None => bail!("Failed to create gacha item record"),
None => bail!("Failed to create gacha item"),
}
}
pub async fn query_create_gacha_roll(
&self,
data: GachaCreateItemRequestDto,
data: GachaCreateRollRequestDto,
) -> Result<String> {
let db = &self.state.surrealdb;
let item_thing = Thing::from((
ResourceEnum::GachaItems.to_string(),
Id::String(data.item_name.clone()),
));
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(),
let record: Option<GachaRollSchema> = db
.create((ResourceEnum::GachaRolls.to_string(), data.item_name.clone()))
.content(GachaRollSchema {
weight: data.weight.clone(),
item: item_thing,
})
.await?;
match record {
Some(_) => Ok("Gacha item successfully created".to_string()),
None => bail!("Failed to create gacha item record"),
Some(_) => Ok("Gacha roll successfully created".to_string()),
None => bail!("Failed to create gacha roll"),
}
}
}
+7 -1
View File
@@ -2,11 +2,17 @@ use serde::{Deserialize, Serialize};
use surrealdb::sql::Thing;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct GachaSchema {
pub struct GachaClaimSchema {
pub transaction_number: String,
pub user: Thing,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct GachaRollSchema {
pub weight: String,
pub item: Thing,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct GachaItemSchema {
pub item_image: String,
+19 -4
View File
@@ -1,4 +1,7 @@
use super::{GachaClaimRequestDto, GachaCreateItemRequestDto, GachaRepository};
use super::{
GachaCreateClaimRequestDto, GachaCreateItemRequestDto, GachaCreateRollRequestDto,
GachaRepository,
};
use crate::{common_response, extract_email, AppState};
use axum::{
http::{HeaderMap, StatusCode},
@@ -8,8 +11,8 @@ use axum::{
pub struct GachaService;
impl GachaService {
pub async fn mutation_create_gacha_claims(
payload: GachaClaimRequestDto,
pub async fn mutation_create_gacha_claim(
payload: GachaCreateClaimRequestDto,
state: &AppState,
header: HeaderMap,
) -> Response {
@@ -25,7 +28,7 @@ impl GachaService {
}
};
match repository.query_create_gacha_claims(payload, email).await {
match repository.query_create_gacha_claim(payload, email).await {
Ok(msg) => common_response(StatusCode::CREATED, &msg),
Err(err) => common_response(StatusCode::BAD_REQUEST, &err.to_string()),
}
@@ -42,4 +45,16 @@ impl GachaService {
Err(err) => common_response(StatusCode::BAD_REQUEST, &err.to_string()),
}
}
pub async fn mutation_create_gacha_roll(
payload: GachaCreateRollRequestDto,
state: &AppState,
) -> Response {
let repository = GachaRepository::new(state);
match repository.query_create_gacha_roll(payload).await {
Ok(msg) => common_response(StatusCode::CREATED, &msg),
Err(err) => common_response(StatusCode::BAD_REQUEST, &err.to_string()),
}
}
}
+6 -2
View File
@@ -14,11 +14,15 @@ pub use gacha_service::*;
pub fn gacha_router() -> Router {
Router::new()
.route(
"/create/claims",
post(gacha_controller::post_create_gacha_claims),
"/create/claim",
post(gacha_controller::post_create_gacha_claim),
)
.route(
"/create/item",
post(gacha_controller::post_create_gacha_item),
)
.route(
"/create/roll",
post(gacha_controller::post_create_gacha_roll),
)
}