1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
use crate::errors::AppError;
use crate::models::{
auth::Claims,
user::{User, UserList},
};
use crate::pagination::Pagination;
use axum::{
extract::{Path, Query},
routing::get,
Json, Router,
};
use serde::Serialize;
/// Create routes for `/v1/users/` namespace
pub fn create_route() -> Router {
Router::new()
.route("/", get(list_users))
.route("/:id", get(get_user))
}
#[derive(Serialize)]
struct UserPagination {
count: i64,
results: Vec<UserList>,
}
/// List users. Checks Authorization token
async fn list_users(
_: Claims,
pagination: Query<Pagination>,
) -> Result<Json<UserPagination>, AppError> {
let page = pagination.0.page.unwrap_or_default();
let results = User::list(page).await?;
let count = User::count().await?;
Ok(Json(UserPagination { count, results }))
}
/// Get an user with id = `user_id`. Checks Authorization token
async fn get_user(Path(user_id): Path<i32>, _: Claims) -> Result<Json<UserList>, AppError> {
match User::find_by_id(user_id).await {
Ok(user) => Ok(Json(user)),
Err(_) => Err(AppError::NotFound("User not found".to_string())),
}
}
|