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
|
use crate::errors::AppError;
use crate::models::{
auth::Claims,
user::{User, UserCreate, UserList},
};
use axum::{extract::Path, routing::get, Json, Router};
/// Create routes for `/v1/users/` namespace
pub fn create_route() -> Router {
Router::new()
.route("/", get(list_users).post(create_user))
.route("/me", get(get_user))
}
/// List users. Checks Authorization token
async fn list_users(_: Claims) -> Result<Json<Vec<UserList>>, AppError> {
let users = User::list().await?;
Ok(Json(users))
}
/// Create an user. Checks Authorization token
async fn create_user(
Json(payload): Json<UserCreate>,
_: Claims,
) -> Result<Json<UserList>, AppError> {
let user = User::new(payload.email, payload.password);
let user_new = User::create(user).await?;
Ok(Json(user_new))
}
/// Get the user from the `Authorization` header token
async fn get_user(claims: Claims) -> Result<Json<UserList>, AppError> {
match User::find_by_id(claims.user_id).await {
Ok(user) => Ok(Json(user)),
Err(_) => Err(AppError::NotFound),
}
}
|