ruskit/app/controllers/
user_controller.rs1use crate::framework::prelude::*;
3use crate::app::entities::User;
5use crate::app::dtos::user::{CreateUserRequest, UserResponse, UserListResponse};
6
7pub struct UserController;
9
10impl UserController {
11    pub async fn index() -> impl IntoResponse {
13        match User::all().await {
14            Ok(users) => Json(UserListResponse::from(users)),
15            Err(e) => panic!("Database error: {}", e), }
17    }
18
19    pub async fn show(Path(id): Path<i64>) -> impl IntoResponse {
21        match User::find(id).await {
22            Ok(Some(user)) => Json(Some(UserResponse::from(user))),
23            Ok(None) => Json(None::<UserResponse>),
24            Err(e) => panic!("Database error: {}", e), }
26    }
27
28    pub async fn store(Json(payload): Json<CreateUserRequest>) -> Json<UserResponse> {
30        let user: User = payload.into();
31        match User::create(user).await {
32            Ok(user) => Json(UserResponse::from(user)),
33            Err(e) => panic!("Database error: {}", e), }
35    }
36
37    pub async fn recent() -> impl IntoResponse {
38        match User::recent(10).await {
39            Ok(users) => Json(UserListResponse::from(users)),
40            Err(e) => panic!("Database error: {}", e), }
42    }
43}