Added initial users API.

This commit is contained in:
2026-10-03 14:22:27 +02:00
parent 75c0ee0084
commit 221c38d364
18 changed files with 531 additions and 1 deletions
+1
View File
@@ -2,3 +2,4 @@ pub mod account;
pub mod direction;
pub mod scheduled_transaction;
pub mod transaction;
pub mod user;
+59
View File
@@ -0,0 +1,59 @@
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use crate::domain;
#[derive(Debug, Clone, Deserialize)]
pub struct UserRequest {}
impl UserRequest {
pub fn into_domain(self, id: Uuid) -> Result<domain::User, domain::DomainError> {
Ok(domain::User { id })
}
}
#[derive(Debug, Clone, Serialize)]
pub struct UserResponse {
pub id: Uuid,
}
impl From<domain::User> for UserResponse {
fn from(user: domain::User) -> Self {
Self { id: user.id }
}
}
#[cfg(test)]
mod tests {
use super::*;
fn request() -> UserRequest {
UserRequest {}
}
#[test]
fn serializes_user_response() {
let id = Uuid::nil();
let user = request().into_domain(id).unwrap();
let response = serde_json::to_value(UserResponse::from(user)).unwrap();
assert_eq!(response["id"], id.to_string());
}
#[test]
fn deserializes_user_response_as_request() {
let user = request().into_domain(Uuid::nil()).unwrap();
let response = serde_json::to_value(UserResponse::from(user)).unwrap();
let _request: UserRequest = serde_json::from_value(response).unwrap();
}
#[test]
fn converts_between_dto_and_domain() {
let id = Uuid::new_v4();
let user = request().into_domain(id).unwrap();
assert_eq!(user.id, id);
let response = UserResponse::from(user);
assert_eq!(response.id, id);
}
}
+102
View File
@@ -0,0 +1,102 @@
use axum::{extract::State, http::StatusCode};
use serde::Deserialize;
use sqlx::PgPool;
use uuid::Uuid;
use super::{
dtos::user::{UserRequest, UserResponse},
extractors::{json::Json, path::Path, query::Query},
};
use crate::{
error::AppError,
repository::{
dtos::{Delete, Upsert},
users as repository,
},
};
pub fn create_router() -> axum::Router<PgPool> {
axum::Router::new()
.route("/", axum::routing::get(list_users))
.route(
"/{id}",
axum::routing::get(get_user)
.put(upsert_user)
.delete(delete_user),
)
}
#[derive(Debug, Deserialize)]
pub struct QueryParams {
pub limit: Option<u64>,
pub offset: Option<u64>,
}
impl TryFrom<QueryParams> for repository::Query {
type Error = AppError;
fn try_from(query: QueryParams) -> Result<Self, Self::Error> {
Ok(repository::Query {
limit: Some(query.limit.unwrap_or(20)),
offset: query.offset,
..Default::default()
})
}
}
#[axum::debug_handler]
#[tracing::instrument(skip(pool))]
async fn list_users(
State(pool): State<PgPool>,
Query(query): Query<QueryParams>,
) -> Result<Json<Vec<UserResponse>>, AppError> {
let users = repository::list(&pool, query.try_into()?)
.await?
.into_iter()
.map(UserResponse::from)
.collect();
Ok(Json(users))
}
#[axum::debug_handler]
#[tracing::instrument(skip(pool))]
async fn get_user(
State(pool): State<PgPool>,
Path(id): Path<Uuid>,
) -> Result<Json<UserResponse>, AppError> {
let user = repository::get(&pool, id)
.await?
.ok_or(AppError::NotFound)?;
Ok(Json(user.into()))
}
#[axum::debug_handler]
#[tracing::instrument(skip(pool, user))]
async fn upsert_user(
State(pool): State<PgPool>,
Path(id): Path<Uuid>,
Json(user): Json<UserRequest>,
) -> Result<(StatusCode, Json<UserResponse>), AppError> {
let user = user.into_domain(id)?;
let (status, user) = match repository::upsert(&pool, user).await? {
Upsert::Created(user) => (StatusCode::CREATED, user),
Upsert::Updated(user) => (StatusCode::OK, user),
};
Ok((status, Json(user.into())))
}
#[axum::debug_handler]
#[tracing::instrument(skip(pool))]
async fn delete_user(
Path(id): Path<Uuid>,
State(pool): State<PgPool>,
) -> Result<StatusCode, AppError> {
match repository::delete(&pool, id).await? {
Delete::Deleted => Ok(StatusCode::NO_CONTENT),
Delete::NotFound => Err(AppError::NotFound),
}
}