feat: create gacha item
This commit is contained in:
@@ -29,10 +29,10 @@ pub async fn auth_middleware(
|
|||||||
|
|
||||||
let token_data = match decode_access_token(token) {
|
let token_data = match decode_access_token(token) {
|
||||||
Ok(data) => data,
|
Ok(data) => data,
|
||||||
Err(err) => {
|
Err(_) => {
|
||||||
return Ok(common_response(
|
return Ok(common_response(
|
||||||
StatusCode::UNAUTHORIZED,
|
StatusCode::UNAUTHORIZED,
|
||||||
&format!("Invalid or expired token: {}", &err.to_string()),
|
&format!("Invalid or expired token"),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use super::{GachaRequestDto, GachaService};
|
use super::{GachaRequestDto, GachaService};
|
||||||
use crate::{AppState, MessageResponseDto};
|
use crate::{v1::GachaCreateItemRequestDto, AppState, MessageResponseDto};
|
||||||
use axum::{response::IntoResponse, Extension, Json};
|
use axum::{response::IntoResponse, Extension, Json};
|
||||||
|
|
||||||
#[utoipa::path(
|
#[utoipa::path(
|
||||||
@@ -18,3 +18,20 @@ pub async fn post_create_gacha(
|
|||||||
) -> impl IntoResponse {
|
) -> impl IntoResponse {
|
||||||
GachaService::mutation_create_gacha(payload, &state).await
|
GachaService::mutation_create_gacha(payload, &state).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
post,
|
||||||
|
path = "/v1/gacha/create/item",
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -10,6 +10,18 @@ pub struct GachaRequestDto {
|
|||||||
pub transaction_number: 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 GachaItemResponseDto {
|
||||||
|
pub item_name: String,
|
||||||
|
pub item_image: String,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
|
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
|
||||||
pub struct GachaResponseDto {
|
pub struct GachaResponseDto {
|
||||||
pub transaction_number: String,
|
pub transaction_number: String,
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
use super::{GachaRequestDto, GachaResponseDto, GachaSchema};
|
use super::{
|
||||||
|
GachaCreateItemRequestDto, GachaItemSchema, GachaRequestDto, GachaResponseDto,
|
||||||
|
GachaSchema,
|
||||||
|
};
|
||||||
use crate::{v1::AuthRepository, AppState, ResourceEnum};
|
use crate::{v1::AuthRepository, AppState, ResourceEnum};
|
||||||
use anyhow::{bail, Result};
|
use anyhow::{bail, Result};
|
||||||
use surrealdb::sql::{Id, Thing};
|
use surrealdb::sql::{Id, Thing};
|
||||||
@@ -40,7 +43,10 @@ impl<'a> GachaRepository<'a> {
|
|||||||
Thing::from((ResourceEnum::Users.to_string(), Id::String(user.email)));
|
Thing::from((ResourceEnum::Users.to_string(), Id::String(user.email)));
|
||||||
|
|
||||||
let record: Option<GachaSchema> = db
|
let record: Option<GachaSchema> = db
|
||||||
.create((ResourceEnum::Gacha.to_string(), &data.transaction_number))
|
.create((
|
||||||
|
ResourceEnum::GachaClaims.to_string(),
|
||||||
|
&data.transaction_number,
|
||||||
|
))
|
||||||
.content(GachaSchema {
|
.content(GachaSchema {
|
||||||
transaction_number: data.transaction_number.clone(),
|
transaction_number: data.transaction_number.clone(),
|
||||||
user: user_thing,
|
user: user_thing,
|
||||||
@@ -52,4 +58,24 @@ impl<'a> GachaRepository<'a> {
|
|||||||
None => bail!("Failed to create gacha record"),
|
None => bail!("Failed to create gacha record"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn query_create_gacha_item(
|
||||||
|
&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"),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,3 +6,9 @@ pub struct GachaSchema {
|
|||||||
pub transaction_number: String,
|
pub transaction_number: String,
|
||||||
pub user: Thing,
|
pub user: Thing,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
|
pub struct GachaItemSchema {
|
||||||
|
pub item_image: String,
|
||||||
|
pub item_name: String,
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use super::{GachaRepository, GachaRequestDto};
|
use super::{GachaCreateItemRequestDto, GachaRepository, GachaRequestDto};
|
||||||
use crate::{common_response, AppState};
|
use crate::{common_response, AppState};
|
||||||
use axum::{http::StatusCode, response::Response};
|
use axum::{http::StatusCode, response::Response};
|
||||||
|
|
||||||
@@ -16,4 +16,16 @@ impl GachaService {
|
|||||||
Err(err) => common_response(StatusCode::BAD_REQUEST, &err.to_string()),
|
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()),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,5 +12,10 @@ pub use gacha_schema::*;
|
|||||||
pub use gacha_service::*;
|
pub use gacha_service::*;
|
||||||
|
|
||||||
pub fn gacha_router() -> Router {
|
pub fn gacha_router() -> Router {
|
||||||
Router::new().route("/create", post(gacha_controller::post_create_gacha))
|
Router::new()
|
||||||
|
.route("/create", post(gacha_controller::post_create_gacha))
|
||||||
|
.route(
|
||||||
|
"/create/item",
|
||||||
|
post(gacha_controller::post_create_gacha_item),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ use std::fmt;
|
|||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub enum ResourceEnum {
|
pub enum ResourceEnum {
|
||||||
Gacha,
|
Gacha,
|
||||||
|
GachaClaims,
|
||||||
Users,
|
Users,
|
||||||
Roles,
|
Roles,
|
||||||
Permissions,
|
Permissions,
|
||||||
@@ -15,6 +16,7 @@ impl fmt::Display for ResourceEnum {
|
|||||||
ResourceEnum::Roles => "app_roles",
|
ResourceEnum::Roles => "app_roles",
|
||||||
ResourceEnum::Permissions => "app_permissions",
|
ResourceEnum::Permissions => "app_permissions",
|
||||||
ResourceEnum::Gacha => "app_gacha",
|
ResourceEnum::Gacha => "app_gacha",
|
||||||
|
ResourceEnum::GachaClaims => "app_gacha_claims",
|
||||||
};
|
};
|
||||||
write!(f, "{}", str)
|
write!(f, "{}", str)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user