feat: user relation

This commit is contained in:
Maulana Sodiqin
2025-03-14 17:50:37 +07:00
parent 9ffe808797
commit 7ab036f5f7
22 changed files with 358 additions and 28 deletions
+20
View File
@@ -0,0 +1,20 @@
use super::{GachaRequestDto, GachaService};
use crate::{AppState, MessageResponseDto};
use axum::{response::IntoResponse, Extension, Json};
#[utoipa::path(
post,
path = "/v1/gacha/create",
request_body = GachaRequestDto,
responses(
(status = 200, description = "Create gacha successful", body = MessageResponseDto),
(status = 401, description = "Create gacha failed", body = MessageResponseDto)
),
tag = "Gacha"
)]
pub async fn post_create_gacha(
Extension(state): Extension<AppState>,
Json(payload): Json<GachaRequestDto>,
) -> impl IntoResponse {
GachaService::mutation_create_gacha(payload, &state).await
}
+17
View File
@@ -0,0 +1,17 @@
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
use crate::v1::UsersItemDto;
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
pub struct GachaRequestDto {
pub email: String,
pub fullname: String,
pub transaction_number: String,
}
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
pub struct GachaResponseDto {
pub transaction_number: String,
pub user: UsersItemDto,
}
+55
View File
@@ -0,0 +1,55 @@
use super::{GachaRequestDto, GachaResponseDto, GachaSchema};
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_by_transaction_number(
&self,
transaction_number: String,
) -> Result<GachaResponseDto> {
let db = &self.state.surrealdb;
let result = db
.select((ResourceEnum::Gacha.to_string(), transaction_number))
.await?;
match result {
Some(response) => Ok(response),
None => bail!("Gacha not found"),
}
}
pub async fn query_create_gacha(&self, data: GachaRequestDto) -> 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_thing =
Thing::from((ResourceEnum::Users.to_string(), Id::String(user.email)));
let record: Option<GachaSchema> = db
.create((ResourceEnum::Gacha.to_string(), &data.transaction_number))
.content(GachaSchema {
transaction_number: data.transaction_number.clone(),
user: user_thing,
})
.await?;
match record {
Some(_) => Ok("Gacha successfully created".to_string()),
None => bail!("Failed to create gacha record"),
}
}
}
+8
View File
@@ -0,0 +1,8 @@
use serde::{Deserialize, Serialize};
use surrealdb::sql::Thing;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct GachaSchema {
pub transaction_number: String,
pub user: Thing,
}
+19
View File
@@ -0,0 +1,19 @@
use super::{GachaRepository, GachaRequestDto};
use crate::{common_response, AppState};
use axum::{http::StatusCode, response::Response};
pub struct GachaService;
impl GachaService {
pub async fn mutation_create_gacha(
payload: GachaRequestDto,
state: &AppState,
) -> Response {
let repository = GachaRepository::new(state);
match repository.query_create_gacha(payload).await {
Ok(msg) => common_response(StatusCode::CREATED, &msg),
Err(err) => common_response(StatusCode::BAD_REQUEST, &err.to_string()),
}
}
}
+16
View File
@@ -0,0 +1,16 @@
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("/create", post(gacha_controller::post_create_gacha))
}