feat: initial commit

This commit is contained in:
Maulana Sodiqin
2025-03-20 17:33:57 +07:00
parent 8ff532bb67
commit ea5ece9ab2
12 changed files with 61 additions and 461 deletions
+5 -12
View File
@@ -1,8 +1,7 @@
use crate::{
v1::{
auth, gacha, AuthLoginRequestDto, AuthLoginResponsetDto,
AuthResendOtpRequestDto, AuthVerifyEmailRequestDto, GachaCreateClaimRequestDto,
GachaCreateItemRequestDto, GachaCreateRollRequestDto,
auth, AuthLoginRequestDto, AuthLoginResponsetDto, AuthResendOtpRequestDto,
AuthVerifyEmailRequestDto,
},
MessageResponseDto, MetaRequestDto, MetaResponseDto, ResponseSuccessDto,
};
@@ -18,10 +17,7 @@ use utoipa::{
auth::auth_controller::post_login,
auth::auth_controller::post_register,
auth::auth_controller::post_verify_email,
auth::auth_controller::post_resend_otp,
gacha::gacha_controller::post_create_gacha_claim,
gacha::gacha_controller::post_create_gacha_item,
gacha::gacha_controller::post_create_gacha_roll
auth::auth_controller::post_resend_otp
),
components(
schemas(
@@ -33,14 +29,11 @@ use utoipa::{
AuthVerifyEmailRequestDto,
AuthResendOtpRequestDto,
ResponseSuccessDto<AuthLoginResponsetDto>,
GachaCreateClaimRequestDto,
GachaCreateItemRequestDto,
GachaCreateRollRequestDto
)
),
info(
title = "IMPHNEN API",
description = "IMPHNEN API Documentation",
title = "Axum SurrealDB Boilerplate",
description = "Axum SurrealDB Documentation",
version = "0.1.0",
contact(
name = "Maulana Sodiqin",
-55
View File
@@ -1,55 +0,0 @@
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/claims/create",
request_body = GachaCreateClaimRequestDto,
responses(
(status = 200, description = "Create gacha claim successful", body = MessageResponseDto),
(status = 401, description = "Create gacha claim failed", body = MessageResponseDto)
),
tag = "Gacha"
)]
pub async fn post_create_gacha_claim(
header: HeaderMap,
Extension(state): Extension<AppState>,
Json(payload): Json<GachaCreateClaimRequestDto>,
) -> impl IntoResponse {
GachaService::mutation_create_gacha_claim(payload, &state, header).await
}
#[utoipa::path(
post,
path = "/v1/gacha/items/create",
request_body = GachaCreateItemRequestDto,
responses(
(status = 200, description = "Create gacha item successful", body = MessageResponseDto),
(status = 401, description = "Create gacha item failed", body = MessageResponseDto)
),
tag = "Gacha"
)]
pub async fn post_create_gacha_item(
Extension(state): Extension<AppState>,
Json(payload): Json<GachaCreateItemRequestDto>,
) -> impl IntoResponse {
GachaService::mutation_create_gacha_item(payload, &state).await
}
#[utoipa::path(
post,
path = "/v1/gacha/rolls/create",
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
}
-39
View File
@@ -1,39 +0,0 @@
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
use crate::v1::UsersItemDto;
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
pub struct GachaCreateClaimRequestDto {
pub item_name: String,
pub transaction_number: String,
}
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
pub struct GachaCreateItemRequestDto {
pub item_name: String,
pub item_image: String,
}
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
pub struct GachaCreateRollRequestDto {
pub item_name: String,
pub weight: String,
}
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
pub struct GachaItemResponseDto {
pub item_name: String,
pub item_image: String,
}
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
pub struct GachaClaimResponseDto {
pub transaction_number: String,
pub user: UsersItemDto,
}
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
pub struct GachaRollResponseDto {
pub item: GachaItemResponseDto,
}
-146
View File
@@ -1,146 +0,0 @@
use super::{
GachaClaimResponseDto, GachaClaimSchema, GachaCreateClaimRequestDto,
GachaCreateItemRequestDto, GachaCreateRollRequestDto, GachaItemResponseDto,
GachaItemSchema, GachaRollSchema,
};
use crate::{v1::AuthRepository, AppState, ResourceEnum};
use anyhow::{bail, Result};
use surrealdb::sql::{Id, Thing};
pub struct GachaRepository<'a> {
pub state: &'a AppState,
}
impl<'a> GachaRepository<'a> {
pub fn new(state: &'a AppState) -> Self {
Self { state }
}
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::GachaClaims.to_string(), transaction_number))
.await?;
match result {
Some(response) => Ok(response),
None => bail!("Gacha claim not found"),
}
}
pub async fn query_gacha_item_by_name(
&self,
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_gacha_claim_by_name(
&self,
name: String,
) -> Result<GachaItemResponseDto> {
let db = &self.state.surrealdb;
let result = db
.select((ResourceEnum::GachaClaims.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);
let db = &self.state.surrealdb;
let user = auth_repository.query_user_by_email(email).await?;
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<GachaClaimSchema> = db
.create((
ResourceEnum::GachaClaims.to_string(),
&data.transaction_number,
))
.content(GachaClaimSchema {
transaction_number: data.transaction_number.clone(),
item: item_thing,
user: user_thing,
})
.await?;
match record {
Some(_) => Ok("Gacha claims successfully created".to_string()),
None => bail!("Failed to create gacha claims"),
}
}
pub async fn query_create_gacha_item(
&self,
data: GachaCreateItemRequestDto,
) -> Result<String> {
let db = &self.state.surrealdb;
let record: Option<GachaItemSchema> = db
.create((ResourceEnum::GachaItems.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"),
}
}
pub async fn query_create_gacha_roll(
&self,
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<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 roll successfully created".to_string()),
None => bail!("Failed to create gacha roll"),
}
}
}
-21
View File
@@ -1,21 +0,0 @@
use serde::{Deserialize, Serialize};
use surrealdb::sql::Thing;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct GachaClaimSchema {
pub transaction_number: String,
pub item: Thing,
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,
pub item_name: String,
}
-60
View File
@@ -1,60 +0,0 @@
use super::{
GachaCreateClaimRequestDto, GachaCreateItemRequestDto, GachaCreateRollRequestDto,
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_claim(
payload: GachaCreateClaimRequestDto,
state: &AppState,
header: HeaderMap,
) -> Response {
let repository = GachaRepository::new(state);
let email = match extract_email(&header) {
Some(email) => email,
None => {
return common_response(
StatusCode::UNAUTHORIZED,
"Invalid or expired token",
);
}
};
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()),
}
}
pub async fn mutation_create_gacha_item(
payload: GachaCreateItemRequestDto,
state: &AppState,
) -> Response {
let repository = GachaRepository::new(state);
match repository.query_create_gacha_item(payload).await {
Ok(msg) => common_response(StatusCode::CREATED, &msg),
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()),
}
}
}
-28
View File
@@ -1,28 +0,0 @@
use axum::{routing::post, Router};
pub mod gacha_controller;
pub mod gacha_dto;
pub mod gacha_repository;
pub mod gacha_schema;
pub mod gacha_service;
pub use gacha_dto::*;
pub use gacha_repository::*;
pub use gacha_schema::*;
pub use gacha_service::*;
pub fn gacha_router() -> Router {
Router::new()
.route(
"/claims/create",
post(gacha_controller::post_create_gacha_claim),
)
.route(
"/items/create",
post(gacha_controller::post_create_gacha_item),
)
.route(
"/rolls/create",
post(gacha_controller::post_create_gacha_roll),
)
}
+2 -5
View File
@@ -1,20 +1,17 @@
use axum::{middleware::from_fn, Router};
pub mod auth;
pub mod docs;
pub mod gacha;
pub mod users;
pub use auth::*;
pub use docs::*;
pub use gacha::*;
pub use users::*;
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));
let protected_routes =
Router::new().layer(from_fn(auth::auth_middleware::auth_middleware));
Router::new().merge(public_routes).merge(protected_routes)
}