Standardized the upsert and delete pattern across the repo.
This commit is contained in:
+12
-12
@@ -9,10 +9,7 @@ use super::{
|
||||
};
|
||||
use crate::{
|
||||
error::AppError,
|
||||
repository::{
|
||||
self,
|
||||
dtos::{Delete, Upsert},
|
||||
},
|
||||
repository::{self},
|
||||
};
|
||||
|
||||
pub fn create_router() -> axum::Router<PgPool> {
|
||||
@@ -74,12 +71,14 @@ async fn upsert_user(
|
||||
) -> Result<(StatusCode, Json<UserResponse>), AppError> {
|
||||
let user = user.into_domain(id)?;
|
||||
|
||||
let (status, user) = match repository::users::upsert(&pool, user).await? {
|
||||
Upsert::Created(user) => (StatusCode::CREATED, user),
|
||||
Upsert::Updated(user) => (StatusCode::OK, user),
|
||||
let upserted = repository::users::upsert(&pool, user).await?;
|
||||
let status = if upserted.previous.is_some() {
|
||||
StatusCode::OK
|
||||
} else {
|
||||
StatusCode::CREATED
|
||||
};
|
||||
|
||||
Ok((status, Json(user.into())))
|
||||
Ok((status, Json(upserted.current.into())))
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
@@ -88,8 +87,9 @@ async fn delete_user(
|
||||
Path(id): Path<Uuid>,
|
||||
State(pool): State<PgPool>,
|
||||
) -> Result<StatusCode, AppError> {
|
||||
match repository::users::delete_by_id(&pool, id).await? {
|
||||
Delete::Deleted => Ok(StatusCode::NO_CONTENT),
|
||||
Delete::NotFound => Err(AppError::NotFound),
|
||||
}
|
||||
repository::users::delete_by_id(&pool, id)
|
||||
.await?
|
||||
.ok_or(AppError::NotFound)?;
|
||||
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user