2025-03-25 20:50:42 +07:00
|
|
|
use axum::{
|
2025-05-20 01:49:50 +07:00
|
|
|
Extension, Json,
|
2025-03-25 20:50:42 +07:00
|
|
|
extract::{Path, Query},
|
|
|
|
|
response::IntoResponse,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
use crate::{
|
2025-05-20 01:49:50 +07:00
|
|
|
AppState, MessageResponseDto, MetaRequestDto, ResponseListSuccessDto,
|
|
|
|
|
ResponseSuccessDto,
|
2025-03-25 20:50:42 +07:00
|
|
|
v1::{
|
2025-08-13 20:40:02 +07:00
|
|
|
permissions_dto::{PermissionsItemDto, PermissionsRequestDto, PermissionsUpdateRequestDto},
|
2025-03-25 20:50:42 +07:00
|
|
|
permissions_service::PermissionsService,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2025-05-20 01:49:50 +07:00
|
|
|
use super::{PermissionsEnum, permissions_guard};
|
2025-04-02 09:35:37 +07:00
|
|
|
|
2025-03-25 20:50:42 +07:00
|
|
|
#[utoipa::path(
|
|
|
|
|
get,
|
|
|
|
|
path = "/v1/permissions",
|
2025-05-20 01:49:50 +07:00
|
|
|
security(
|
|
|
|
|
("Bearer" = [])
|
|
|
|
|
),
|
2025-03-25 20:50:42 +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"),
|
|
|
|
|
("filter" = Option<String>, Query, description = "Filter value"),
|
|
|
|
|
("filter_by" = Option<String>, Query, description = "Field to filter by"),
|
|
|
|
|
),
|
|
|
|
|
responses(
|
|
|
|
|
(status = 200, description = "Get permission list", body = ResponseListSuccessDto<Vec<PermissionsItemDto>>)
|
|
|
|
|
),
|
|
|
|
|
tag = "Permissions"
|
|
|
|
|
)]
|
|
|
|
|
pub async fn get_permission_list(
|
2025-04-02 09:35:37 +07:00
|
|
|
headers: axum::http::HeaderMap,
|
2025-03-25 20:50:42 +07:00
|
|
|
Extension(state): Extension<AppState>,
|
|
|
|
|
Query(meta): Query<MetaRequestDto>,
|
|
|
|
|
) -> impl IntoResponse {
|
2025-04-02 09:35:37 +07:00
|
|
|
match permissions_guard(
|
|
|
|
|
&headers,
|
|
|
|
|
state.clone(),
|
|
|
|
|
vec![PermissionsEnum::ReadListPermissions],
|
|
|
|
|
)
|
|
|
|
|
.await
|
|
|
|
|
{
|
|
|
|
|
Ok(_) => PermissionsService::get_permission_list(&state, meta).await,
|
|
|
|
|
Err(response) => response,
|
|
|
|
|
}
|
2025-03-25 20:50:42 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[utoipa::path(
|
|
|
|
|
get,
|
|
|
|
|
path = "/v1/permissions/detail/{id}",
|
2025-03-26 13:06:34 +07:00
|
|
|
security(
|
|
|
|
|
("Bearer" = [])
|
|
|
|
|
),
|
2025-03-25 20:50:42 +07:00
|
|
|
params(("id" = String, Path, description = "Permission ID")),
|
|
|
|
|
responses(
|
|
|
|
|
(status = 200, description = "Get permission by ID", body = ResponseSuccessDto<PermissionsItemDto>)
|
|
|
|
|
),
|
|
|
|
|
tag = "Permissions"
|
|
|
|
|
)]
|
|
|
|
|
pub async fn get_permission_by_id(
|
2025-04-02 09:35:37 +07:00
|
|
|
headers: axum::http::HeaderMap,
|
2025-03-25 20:50:42 +07:00
|
|
|
Extension(state): Extension<AppState>,
|
|
|
|
|
Path(id): Path<String>,
|
|
|
|
|
) -> impl IntoResponse {
|
2025-04-02 09:35:37 +07:00
|
|
|
match permissions_guard(
|
|
|
|
|
&headers,
|
|
|
|
|
state.clone(),
|
|
|
|
|
vec![PermissionsEnum::ReadDetailPermissions],
|
|
|
|
|
)
|
|
|
|
|
.await
|
|
|
|
|
{
|
|
|
|
|
Ok(_) => PermissionsService::get_permission_by_id(&state, id).await,
|
|
|
|
|
Err(response) => response,
|
|
|
|
|
}
|
2025-03-25 20:50:42 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[utoipa::path(
|
|
|
|
|
post,
|
2025-03-26 13:06:34 +07:00
|
|
|
security(
|
|
|
|
|
("Bearer" = [])
|
|
|
|
|
),
|
2025-03-25 20:50:42 +07:00
|
|
|
path = "/v1/permissions/create",
|
|
|
|
|
request_body = PermissionsRequestDto,
|
|
|
|
|
responses(
|
|
|
|
|
(status = 201, description = "Create new permission", body = MessageResponseDto)
|
|
|
|
|
),
|
|
|
|
|
tag = "Permissions"
|
|
|
|
|
)]
|
|
|
|
|
pub async fn post_create_permission(
|
2025-04-02 09:35:37 +07:00
|
|
|
headers: axum::http::HeaderMap,
|
2025-03-25 20:50:42 +07:00
|
|
|
Extension(state): Extension<AppState>,
|
|
|
|
|
Json(payload): Json<PermissionsRequestDto>,
|
|
|
|
|
) -> impl IntoResponse {
|
2025-04-02 09:35:37 +07:00
|
|
|
match permissions_guard(
|
|
|
|
|
&headers,
|
|
|
|
|
state.clone(),
|
|
|
|
|
vec![PermissionsEnum::CreatePermissions],
|
|
|
|
|
)
|
|
|
|
|
.await
|
|
|
|
|
{
|
|
|
|
|
Ok(_) => PermissionsService::create_role(&state, payload).await,
|
|
|
|
|
Err(response) => response,
|
|
|
|
|
}
|
2025-03-25 20:50:42 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[utoipa::path(
|
|
|
|
|
put,
|
2025-03-26 13:06:34 +07:00
|
|
|
security(
|
|
|
|
|
("Bearer" = [])
|
|
|
|
|
),
|
2025-03-25 20:50:42 +07:00
|
|
|
path = "/v1/permissions/update/{id}",
|
2025-08-13 20:40:02 +07:00
|
|
|
request_body = PermissionsUpdateRequestDto,
|
2025-03-25 20:50:42 +07:00
|
|
|
responses(
|
|
|
|
|
(status = 200, description = "Update permission", body = MessageResponseDto)
|
|
|
|
|
),
|
|
|
|
|
tag = "Permissions"
|
|
|
|
|
)]
|
|
|
|
|
pub async fn put_update_permission(
|
2025-04-02 09:35:37 +07:00
|
|
|
headers: axum::http::HeaderMap,
|
2025-03-25 20:50:42 +07:00
|
|
|
Extension(state): Extension<AppState>,
|
|
|
|
|
Path(id): Path<String>,
|
2025-08-13 20:40:02 +07:00
|
|
|
Json(payload): Json<PermissionsUpdateRequestDto>,
|
2025-03-25 20:50:42 +07:00
|
|
|
) -> impl IntoResponse {
|
2025-04-02 09:35:37 +07:00
|
|
|
match permissions_guard(
|
|
|
|
|
&headers,
|
|
|
|
|
state.clone(),
|
|
|
|
|
vec![PermissionsEnum::UpdatePermissions],
|
|
|
|
|
)
|
|
|
|
|
.await
|
|
|
|
|
{
|
|
|
|
|
Ok(_) => PermissionsService::update_permission(&state, payload, id).await,
|
|
|
|
|
Err(response) => response,
|
|
|
|
|
}
|
2025-03-25 20:50:42 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[utoipa::path(
|
|
|
|
|
delete,
|
2025-03-26 13:06:34 +07:00
|
|
|
security(
|
|
|
|
|
("Bearer" = [])
|
|
|
|
|
),
|
2025-03-25 20:50:42 +07:00
|
|
|
path = "/v1/permissions/delete/{id}",
|
|
|
|
|
responses(
|
|
|
|
|
(status = 200, description = "Delete permission", body = MessageResponseDto)
|
|
|
|
|
),
|
|
|
|
|
tag = "Permissions"
|
|
|
|
|
)]
|
|
|
|
|
pub async fn delete_permission(
|
2025-04-02 09:35:37 +07:00
|
|
|
headers: axum::http::HeaderMap,
|
2025-03-25 20:50:42 +07:00
|
|
|
Extension(state): Extension<AppState>,
|
|
|
|
|
Path(id): Path<String>,
|
|
|
|
|
) -> impl IntoResponse {
|
2025-04-02 09:35:37 +07:00
|
|
|
match permissions_guard(
|
|
|
|
|
&headers,
|
|
|
|
|
state.clone(),
|
|
|
|
|
vec![PermissionsEnum::DeletePermissions],
|
|
|
|
|
)
|
|
|
|
|
.await
|
|
|
|
|
{
|
|
|
|
|
Ok(_) => PermissionsService::delete_permission(&state, id).await,
|
|
|
|
|
Err(response) => response,
|
|
|
|
|
}
|
2025-03-25 20:50:42 +07:00
|
|
|
}
|