Route structure: /v1/iam/auth/* (was /v1/auth/*) /v1/iam/users/* (was /v1/users/*) /v1/iam/roles/* (was /v1/roles/*) /v1/iam/permissions/* (was /v1/permissions/*) /v1/landing/cms/events/* (was /v1/cms/landing/events/*) /v1/landing/cms/testimonials/* (was /v1/cms/landing/testimonials/*) /v1/dimentorin/mentors/* (was /v1/mentors/*) /v1/dimentorin/sessions/* (was /v1/sessions/*) /v1/gacha/* (unchanged) /v1/hackathon/* (unchanged) /v1/qr/* (unchanged) Swagger: add gacha_credits endpoints which were missing from OpenAPI spec. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
181 lines
5.2 KiB
Rust
181 lines
5.2 KiB
Rust
use super::dto::{
|
|
EventsCreateRequestDto, EventsDetailItemDto, EventsListItemDto,
|
|
EventsUpdateRequestDto,
|
|
};
|
|
use crate::events::domain::EventService;
|
|
use axum::{
|
|
Extension,
|
|
extract::Path,
|
|
http::HeaderMap,
|
|
response::{IntoResponse, Response},
|
|
};
|
|
use imphnen_entities::ResponseSuccessDto;
|
|
use imphnen_iam::{PermissionsEnum, require_permissions};
|
|
use imphnen_libs::{AppState, ValidatedJson};
|
|
use imphnen_utils::AppError;
|
|
use imphnen_utils::{ApiMessage, ApiPaginated, ApiSuccess};
|
|
use paginator_axum::PaginationQuery;
|
|
use paginator_utils::PaginatorResponse;
|
|
use std::sync::Arc;
|
|
use uuid::Uuid;
|
|
|
|
#[utoipa::path(
|
|
get,
|
|
path = "/v1/landing/cms/events",
|
|
params(
|
|
("page" = Option<i64>, Query, description = "Page number"),
|
|
("per_page" = Option<i64>, Query, description = "Items per page"),
|
|
("search" = Option<String>, Query, description = "Search keyword"),
|
|
("sort_by" = Option<String>, Query, description = "Sort by field"),
|
|
("order" = Option<String>, Query, description = "Order ASC or DESC"),
|
|
),
|
|
responses(
|
|
(status = 200, description = "[PUBLIC] Get event list")
|
|
),
|
|
tag = "Events"
|
|
)]
|
|
pub async fn get_event_list(
|
|
Extension(service): Extension<Arc<dyn EventService>>,
|
|
PaginationQuery(params): PaginationQuery,
|
|
) -> Response {
|
|
match service.list(params).await {
|
|
Ok(result) => {
|
|
let mapped = PaginatorResponse {
|
|
data: result
|
|
.data
|
|
.into_iter()
|
|
.map(EventsListItemDto::from)
|
|
.collect::<Vec<_>>(),
|
|
meta: result.meta,
|
|
};
|
|
ApiPaginated(mapped).into_response()
|
|
}
|
|
Err(e) => ApiMessage::new(axum::http::StatusCode::BAD_REQUEST, e.to_string())
|
|
.into_response(),
|
|
}
|
|
}
|
|
|
|
#[utoipa::path(
|
|
get,
|
|
path = "/v1/landing/cms/events/detail/{id}",
|
|
params(
|
|
("id" = String, Path, description = "Event ID")
|
|
),
|
|
responses(
|
|
(status = 200, description = "[PUBLIC] Get event by ID", body = ResponseSuccessDto<EventsDetailItemDto>)
|
|
),
|
|
tag = "Events"
|
|
)]
|
|
pub async fn get_event_by_id(
|
|
Extension(service): Extension<Arc<dyn EventService>>,
|
|
Path(id): Path<String>,
|
|
) -> Response {
|
|
let uuid = match Uuid::parse_str(&id) {
|
|
Ok(u) => u,
|
|
Err(e) => {
|
|
return ApiMessage::new(
|
|
axum::http::StatusCode::BAD_REQUEST,
|
|
format!("Invalid UUID: {e}"),
|
|
)
|
|
.into_response();
|
|
}
|
|
};
|
|
match service.get(uuid).await {
|
|
Ok(event) => ApiSuccess(EventsDetailItemDto::from(event)).into_response(),
|
|
Err(e) => ApiMessage::new(axum::http::StatusCode::NOT_FOUND, e.to_string())
|
|
.into_response(),
|
|
}
|
|
}
|
|
|
|
#[utoipa::path(
|
|
post,
|
|
security(("Bearer" = [])),
|
|
path = "/v1/landing/cms/events/create",
|
|
request_body = EventsCreateRequestDto,
|
|
responses(
|
|
(status = 201, description = "[ADMIN] Create new event")
|
|
),
|
|
tag = "Events"
|
|
)]
|
|
pub async fn post_create_event(
|
|
headers: HeaderMap,
|
|
Extension(state): Extension<AppState>,
|
|
Extension(service): Extension<Arc<dyn EventService>>,
|
|
ValidatedJson(payload): ValidatedJson<EventsCreateRequestDto>,
|
|
) -> Result<impl IntoResponse, AppError> {
|
|
require_permissions!(headers, state, [PermissionsEnum::Administrator], {
|
|
let entity = payload.into();
|
|
service.create(entity).await?;
|
|
Ok(ApiMessage::created("Event created"))
|
|
})
|
|
}
|
|
|
|
#[utoipa::path(
|
|
patch,
|
|
security(("Bearer" = [])),
|
|
path = "/v1/landing/cms/events/update/{id}",
|
|
params(
|
|
("id" = String, Path, description = "Event ID")
|
|
),
|
|
request_body = EventsUpdateRequestDto,
|
|
responses(
|
|
(status = 200, description = "[ADMIN] Update event")
|
|
),
|
|
tag = "Events"
|
|
)]
|
|
pub async fn patch_update_event(
|
|
headers: HeaderMap,
|
|
Extension(state): Extension<AppState>,
|
|
Extension(service): Extension<Arc<dyn EventService>>,
|
|
Path(id): Path<String>,
|
|
ValidatedJson(payload): ValidatedJson<EventsUpdateRequestDto>,
|
|
) -> Result<impl IntoResponse, AppError> {
|
|
require_permissions!(headers, state, [PermissionsEnum::Administrator], {
|
|
let uuid = Uuid::parse_str(&id)
|
|
.map_err(|e| AppError::BadRequestError(format!("Invalid UUID: {e}")))?;
|
|
let existing = service.get(uuid).await?;
|
|
let entity = crate::events::domain::EventEntity {
|
|
id: existing.id,
|
|
name: payload.name,
|
|
description: payload.description,
|
|
detail_link: payload.detail_link,
|
|
price: payload.price,
|
|
is_online: payload.is_online,
|
|
location: payload.location,
|
|
start_date: payload.start_date,
|
|
end_date: payload.end_date,
|
|
is_deleted: existing.is_deleted,
|
|
created_at: existing.created_at,
|
|
updated_at: chrono::Utc::now(),
|
|
};
|
|
service.update(entity).await?;
|
|
Ok(ApiMessage::ok("Event updated"))
|
|
})
|
|
}
|
|
|
|
#[utoipa::path(
|
|
delete,
|
|
security(("Bearer" = [])),
|
|
path = "/v1/landing/cms/events/delete/{id}",
|
|
params(
|
|
("id" = String, Path, description = "Event ID")
|
|
),
|
|
responses(
|
|
(status = 200, description = "[ADMIN] Soft delete event")
|
|
),
|
|
tag = "Events"
|
|
)]
|
|
pub async fn delete_event(
|
|
headers: HeaderMap,
|
|
Extension(state): Extension<AppState>,
|
|
Extension(service): Extension<Arc<dyn EventService>>,
|
|
Path(id): Path<String>,
|
|
) -> Result<impl IntoResponse, AppError> {
|
|
require_permissions!(headers, state, [PermissionsEnum::Administrator], {
|
|
let uuid = Uuid::parse_str(&id)
|
|
.map_err(|e| AppError::BadRequestError(format!("Invalid UUID: {e}")))?;
|
|
service.delete(uuid).await?;
|
|
Ok(ApiMessage::ok("Event deleted"))
|
|
})
|
|
}
|