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>, AppError> { let users = User::list().await?; Ok(Json(users)) } /// Create an user. Checks Authorization token async fn create_user( Json(payload): Json, _: Claims, ) -> Result, 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, AppError> { match User::find_by_id(claims.user_id).await { Ok(user) => Ok(Json(user)), Err(_) => Err(AppError::NotFound), } }