feat: Enhance validation and permissions handling across controllers

- Added `ValidatedJson` extractor for automatic JSON validation in `events_controller.rs`, `testimonials_controller.rs`, `mentors_controller.rs`, `gacha_items_controller.rs`, and `hackathon_controller.rs`.
- Replaced manual permission checks with `require_permissions!` and `require_auth!` macros in relevant controllers to streamline permission handling.
- Introduced `sanitization` utilities in `sanitization.rs` for improved input sanitization.
- Added `permission_macros.rs` to encapsulate permission checking logic and reduce boilerplate.
- Updated dependencies in `Cargo.toml` to include `serde_json` and `validator`.
- Implemented error handling improvements in `notification_service.rs` for better response management.
This commit is contained in:
MythEclipse
2025-10-28 14:04:41 +07:00
parent d4a6c4c9ea
commit b9a51ce6cc
17 changed files with 551 additions and 225 deletions
@@ -10,7 +10,8 @@ use super::hackathon_schema::SubmissionStatus;
use crate::v1::hackathon::HackathonRepository;
use crate::{AppState, ResponseSuccessDto, ErrorDto};
use imphnen_entities::{PermissionsEnum, UsersDetailQueryDto};
use imphnen_libs::{MetaRequestDto, ResponseListSuccessDto};
use imphnen_libs::{MetaRequestDto, ResponseListSuccessDto, ValidatedJson};
use imphnen_iam::require_permissions;
use axum::{
extract::{Extension, Path, Query},
http::StatusCode,
@@ -4,14 +4,14 @@ use super::notification_dto::{
UnreadCountResponseDto,
};
use super::notification_repository::Repository;
use super::notification_schema::NotificationSchema;
use axum::http::{Response, StatusCode};
use axum::response::IntoResponse;
use axum::body::Body;
use imphnen_entities::common_dto::ResponseSuccessDto;
use imphnen_libs::AppState;
use imphnen_utils::{
extract_id, make_thing, response_format::success_response, validator::validate_request,
extract_id, make_thing, response_format::success_response, error_response,
validator::validate_request, AppError,
};
pub struct Service<'a> {
@@ -28,8 +28,8 @@ impl<'a> Service<'a> {
user_email: &str,
query: NotificationListQueryDto,
) -> Response<Body> {
if let Err((status, message)) = validate_request(&query) {
return (status, message).into_response();
if let Err((_status, message)) = validate_request(&query) {
return error_response(AppError::ValidationError(message));
}
let user_id = make_thing("users", user_email);
@@ -48,7 +48,7 @@ impl<'a> Service<'a> {
let notifications = match notifications_result {
Ok(notifs) => notifs,
Err(err) => {
return (StatusCode::INTERNAL_SERVER_ERROR, err).into_response();
return error_response(AppError::InternalServerError(err.to_string()));
}
};
@@ -59,7 +59,7 @@ impl<'a> Service<'a> {
let total = match total_result {
Ok(count) => count,
Err(err) => {
return (StatusCode::INTERNAL_SERVER_ERROR, err).into_response();
return error_response(AppError::InternalServerError(err.to_string()));
}
};
@@ -68,7 +68,7 @@ impl<'a> Service<'a> {
let unread_count = match unread_count_result {
Ok(count) => count,
Err(err) => {
return (StatusCode::INTERNAL_SERVER_ERROR, err).into_response();
return error_response(AppError::InternalServerError(err.to_string()));
}
};