//! HTTP handlers for users. Business rules live in `UserService`.
use axum::Json;
use axum::extract::{Path, State};
use axum::http::StatusCode;
use crate::base::auth::{AdminUser, AuthUser};
use crate::base::error::{AppError, AppResult};
use crate::modules::user::dto::user::UserResponse;
use crate::modules::user::services::user_service::UserService;
/// `GET /users` (admin only).
pub async fn index(
_admin: AdminUser,
State(service): State<UserService>,
) -> AppResult<Json<Vec<UserResponse>>> {
let users = service.list().await?;
Ok(Json(users.into_iter().map(UserResponse::from).collect()))
}
/// `GET /users/{id}`: admins can view anyone, other users only themselves.
pub async fn show(
user: AuthUser,
State(service): State<UserService>,
Path(id): Path<i64>,
) -> AppResult<Json<UserResponse>> {
if user.id != id && !user.is_admin() {
return Err(AppError::Forbidden);
}
Ok(Json(service.find(id).await?.into()))
}
/// `DELETE /users/{id}` (admin only).
pub async fn destroy(
AdminUser(admin): AdminUser,
State(service): State<UserService>,
Path(id): Path<i64>,
) -> AppResult<StatusCode> {
service.delete(id, admin.id).await?;
Ok(StatusCode::NO_CONTENT)
}