summaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
Diffstat (limited to 'server')
-rw-r--r--server/src/models/user.rs17
-rw-r--r--server/src/routes/user.rs13
2 files changed, 28 insertions, 2 deletions
diff --git a/server/src/models/user.rs b/server/src/models/user.rs
index 38ec121..dd96f90 100644
--- a/server/src/models/user.rs
+++ b/server/src/models/user.rs
@@ -81,6 +81,23 @@ impl User {
Ok(rec)
}
+ pub async fn find_by_id(user_id: i32) -> Result<UserList, AppError> {
+ let pool = unsafe { get_client() };
+
+ let rec = sqlx::query_as!(
+ UserList,
+ r#"
+ SELECT id, email, is_staff FROM "users"
+ WHERE id = $1
+ "#,
+ user_id
+ )
+ .fetch_one(pool)
+ .await?;
+
+ Ok(rec)
+ }
+
pub async fn list() -> Result<Vec<UserList>, AppError> {
let pool = unsafe { get_client() };
let rows = sqlx::query_as!(UserList, r#"SELECT id, email, is_staff FROM users"#)
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),
+ }
+}