summaryrefslogtreecommitdiff
path: root/server/src/routes/user.rs
blob: d0aa056c0f496c79acf501a9cb1dd4e9263cc6cb (plain)
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
use crate::errors::AppError;
use crate::models::{
    auth::Claims,
    user::{User, UserList},
};
use axum::{routing::get, Json, Router};

/// Create routes for `/v1/users/` namespace
pub fn create_route() -> Router {
    Router::new()
        .route("/", get(list_users))
        .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))
}

/// 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("User not found".to_string())),
    }
}