diff options
author | Santo Cariotti <santo@dcariotti.me> | 2022-08-23 18:19:06 +0200 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2022-08-23 18:19:06 +0200 |
commit | c0c45a3c581405d24c0a3051a3a13d102214556a (patch) | |
tree | f3cc608ffd9e063a18bfec70c3bf46a3cf1ffa5f /server/src/routes | |
parent | eedc536c23c3230c6ebeac8b495b2cfef0317830 (diff) |
Endpoint to get user
Url /v1/users/:id
Diffstat (limited to 'server/src/routes')
-rw-r--r-- | server/src/routes/user.rs | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/server/src/routes/user.rs b/server/src/routes/user.rs index 15a67be..4103ccb 100644 --- a/server/src/routes/user.rs +++ b/server/src/routes/user.rs @@ -1,9 +1,11 @@ use crate::errors::AppError; use crate::models::user::{User, UserCreate, UserList}; -use axum::{routing::get, Json, Router}; +use axum::{extract::Path, routing::get, Json, Router}; pub fn create_route() -> Router { - Router::new().route("/", get(list_users).post(create_user)) + Router::new() + .route("/", get(list_users).post(create_user)) + .route("/:id", get(get_user)) } async fn list_users() -> Result<Json<Vec<UserList>>, AppError> { @@ -18,3 +20,10 @@ async fn create_user(Json(payload): Json<UserCreate>) -> Result<Json<UserList>, Ok(Json(user_new)) } + +async fn get_user(Path(user_id): Path<i32>) -> Result<Json<UserList>, AppError> { + match User::find_by_id(user_id).await { + Ok(user) => Ok(Json(user)), + Err(_) => Err(AppError::NotFound), + } +} |