2026-04-02 22:29:08 +07:00
|
|
|
use super::dto::{
|
|
|
|
|
EventsCreateRequestDto, EventsDetailItemDto, EventsListItemDto,
|
|
|
|
|
EventsUpdateRequestDto,
|
|
|
|
|
};
|
|
|
|
|
use crate::events::domain::EventService;
|
|
|
|
|
use axum::{
|
|
|
|
|
Extension,
|
|
|
|
|
extract::Path,
|
|
|
|
|
http::HeaderMap,
|
|
|
|
|
response::{IntoResponse, Response},
|
|
|
|
|
};
|
2026-04-02 13:39:52 +07:00
|
|
|
use imphnen_entities::ResponseSuccessDto;
|
|
|
|
|
use imphnen_iam::{PermissionsEnum, require_permissions};
|
2026-04-02 22:29:08 +07:00
|
|
|
use imphnen_libs::{AppState, ValidatedJson};
|
2026-04-02 13:39:52 +07:00
|
|
|
use imphnen_utils::AppError;
|
2026-04-02 22:29:08 +07:00
|
|
|
use imphnen_utils::{ApiMessage, ApiPaginated, ApiSuccess};
|
|
|
|
|
use paginator_axum::PaginationQuery;
|
|
|
|
|
use paginator_utils::PaginatorResponse;
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
use uuid::Uuid;
|
2026-04-02 13:39:52 +07:00
|
|
|
|
|
|
|
|
#[utoipa::path(
|
|
|
|
|
get,
|
2026-04-03 00:26:29 +07:00
|
|
|
path = "/v1/landing/cms/events",
|
2026-04-02 13:39:52 +07:00
|
|
|
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(
|
2026-04-02 22:29:08 +07:00
|
|
|
Extension(service): Extension<Arc<dyn EventService>>,
|
|
|
|
|
PaginationQuery(params): PaginationQuery,
|
2026-04-02 13:39:52 +07:00
|
|
|
) -> Response {
|
2026-04-02 22:29:08 +07:00
|
|
|
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(),
|
|
|
|
|
}
|
2026-04-02 13:39:52 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[utoipa::path(
|
|
|
|
|
get,
|
2026-04-03 00:26:29 +07:00
|
|
|
path = "/v1/landing/cms/events/detail/{id}",
|
2026-04-02 13:39:52 +07:00
|
|
|
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(
|
2026-04-02 22:29:08 +07:00
|
|
|
Extension(service): Extension<Arc<dyn EventService>>,
|
|
|
|
|
Path(id): Path<String>,
|
2026-04-02 13:39:52 +07:00
|
|
|
) -> Response {
|
2026-04-02 22:29:08 +07:00
|
|
|
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(),
|
|
|
|
|
}
|
2026-04-02 13:39:52 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[utoipa::path(
|
|
|
|
|
post,
|
|
|
|
|
security(("Bearer" = [])),
|
2026-04-03 00:26:29 +07:00
|
|
|
path = "/v1/landing/cms/events/create",
|
2026-04-02 13:39:52 +07:00
|
|
|
request_body = EventsCreateRequestDto,
|
|
|
|
|
responses(
|
|
|
|
|
(status = 201, description = "[ADMIN] Create new event")
|
|
|
|
|
),
|
|
|
|
|
tag = "Events"
|
|
|
|
|
)]
|
|
|
|
|
pub async fn post_create_event(
|
2026-04-02 22:29:08 +07:00
|
|
|
headers: HeaderMap,
|
|
|
|
|
Extension(state): Extension<AppState>,
|
|
|
|
|
Extension(service): Extension<Arc<dyn EventService>>,
|
|
|
|
|
ValidatedJson(payload): ValidatedJson<EventsCreateRequestDto>,
|
2026-04-02 13:39:52 +07:00
|
|
|
) -> Result<impl IntoResponse, AppError> {
|
2026-04-02 22:29:08 +07:00
|
|
|
require_permissions!(headers, state, [PermissionsEnum::Administrator], {
|
|
|
|
|
let entity = payload.into();
|
|
|
|
|
service.create(entity).await?;
|
|
|
|
|
Ok(ApiMessage::created("Event created"))
|
|
|
|
|
})
|
2026-04-02 13:39:52 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[utoipa::path(
|
|
|
|
|
patch,
|
|
|
|
|
security(("Bearer" = [])),
|
2026-04-03 00:26:29 +07:00
|
|
|
path = "/v1/landing/cms/events/update/{id}",
|
2026-04-02 13:39:52 +07:00
|
|
|
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(
|
2026-04-02 22:29:08 +07:00
|
|
|
headers: HeaderMap,
|
|
|
|
|
Extension(state): Extension<AppState>,
|
|
|
|
|
Extension(service): Extension<Arc<dyn EventService>>,
|
|
|
|
|
Path(id): Path<String>,
|
|
|
|
|
ValidatedJson(payload): ValidatedJson<EventsUpdateRequestDto>,
|
2026-04-02 13:39:52 +07:00
|
|
|
) -> Result<impl IntoResponse, AppError> {
|
2026-04-02 22:29:08 +07:00
|
|
|
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"))
|
|
|
|
|
})
|
2026-04-02 13:39:52 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[utoipa::path(
|
|
|
|
|
delete,
|
|
|
|
|
security(("Bearer" = [])),
|
2026-04-03 00:26:29 +07:00
|
|
|
path = "/v1/landing/cms/events/delete/{id}",
|
2026-04-02 13:39:52 +07:00
|
|
|
params(
|
|
|
|
|
("id" = String, Path, description = "Event ID")
|
|
|
|
|
),
|
|
|
|
|
responses(
|
|
|
|
|
(status = 200, description = "[ADMIN] Soft delete event")
|
|
|
|
|
),
|
|
|
|
|
tag = "Events"
|
|
|
|
|
)]
|
|
|
|
|
pub async fn delete_event(
|
2026-04-02 22:29:08 +07:00
|
|
|
headers: HeaderMap,
|
|
|
|
|
Extension(state): Extension<AppState>,
|
|
|
|
|
Extension(service): Extension<Arc<dyn EventService>>,
|
|
|
|
|
Path(id): Path<String>,
|
2026-04-02 13:39:52 +07:00
|
|
|
) -> Result<impl IntoResponse, AppError> {
|
2026-04-02 22:29:08 +07:00
|
|
|
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"))
|
|
|
|
|
})
|
2026-04-02 13:39:52 +07:00
|
|
|
}
|