Implement users list and get-by-id endpoints
This commit is contained in:
parent
78ed205081
commit
b504db6c03
1 changed files with 26 additions and 0 deletions
26
src/api/users.rs
Normal file
26
src/api/users.rs
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
use axum::{extract::{Path, State}, http::StatusCode, Json};
|
||||
use uuid::Uuid;
|
||||
use crate::models::user::User;
|
||||
|
||||
pub async fn list_users(
|
||||
State(pool): State<sqlx::PgPool>,
|
||||
) -> Result<Json<Vec<User>>, StatusCode> {
|
||||
let users = sqlx::query_as::<_, User>("SELECT id, username, email FROM users ORDER BY created_at DESC")
|
||||
.fetch_all(&pool)
|
||||
.await
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
Ok(Json(users))
|
||||
}
|
||||
|
||||
pub async fn get_user(
|
||||
State(pool): State<sqlx::PgPool>,
|
||||
Path(id): Path<Uuid>,
|
||||
) -> Result<Json<User>, StatusCode> {
|
||||
sqlx::query_as::<_, User>("SELECT id, username, email FROM users WHERE id = $1")
|
||||
.bind(id)
|
||||
.fetch_optional(&pool)
|
||||
.await
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
|
||||
.map(Json)
|
||||
.ok_or(StatusCode::NOT_FOUND)
|
||||
}
|
||||
Loading…
Reference in a new issue