Moved domain logic out of the database layer, to the user app route.

This commit is contained in:
2026-10-09 14:50:39 +02:00
parent 0517ddd02e
commit 4a84c8330a
11 changed files with 141 additions and 161 deletions
+11 -10
View File
@@ -12,18 +12,19 @@ impl<T> Upsert<T> {
}
}
pub enum GuardedUpsert<T> {
Created(T),
Updated(T),
Rejected,
pub struct Upserted<T> {
pub current: T,
pub previous: Option<T>,
}
impl<T> GuardedUpsert<T> {
pub fn try_map<U, E>(self, f: impl FnOnce(T) -> Result<U, E>) -> Result<GuardedUpsert<U>, E> {
Ok(match self {
Self::Created(value) => GuardedUpsert::Created(f(value)?),
Self::Updated(value) => GuardedUpsert::Updated(f(value)?),
Self::Rejected => GuardedUpsert::Rejected,
impl<T> Upserted<T> {
pub fn try_map<U, E>(self, mut f: impl FnMut(T) -> Result<U, E>) -> Result<Upserted<U>, E> {
Ok(Upserted {
current: f(self.current)?,
previous: match self.previous {
Some(value) => Some(f(value)?),
None => None,
},
})
}
}
+29 -42
View File
@@ -2,7 +2,7 @@ use uuid::Uuid;
use super::{
DatabaseError,
dtos::{Delete, GuardedUpsert, UserAppRow},
dtos::{Upserted, UserAppRow},
};
pub struct Query {
@@ -34,22 +34,13 @@ where
.map_err(DatabaseError::from)
}
pub async fn get_by_id<'e, E>(
executor: E,
user_id: Uuid,
id: Uuid,
) -> Result<Option<UserAppRow>, DatabaseError>
pub async fn get_by_id<'e, E>(executor: E, id: Uuid) -> Result<Option<UserAppRow>, DatabaseError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
sqlx::query_as!(
UserAppRow,
r#"
SELECT id, user_id
FROM user_apps
WHERE user_id = $1 AND id = $2
"#,
user_id,
"SELECT id, user_id FROM user_apps WHERE id = $1",
id,
)
.fetch_optional(executor)
@@ -65,7 +56,7 @@ where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
sqlx::query_scalar!(
"SELECT user_id FROM user_apps WHERE id = $1 FOR SHARE",
"SELECT user_id FROM user_apps WHERE id = $1 FOR KEY SHARE",
app_id,
)
.fetch_optional(executor)
@@ -76,7 +67,7 @@ where
pub async fn upsert<'e, E>(
executor: E,
app: UserAppRow,
) -> Result<GuardedUpsert<UserAppRow>, DatabaseError>
) -> Result<Upserted<UserAppRow>, DatabaseError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
@@ -85,56 +76,52 @@ where
INSERT INTO user_apps (id, user_id)
VALUES ($1, $2)
ON CONFLICT (id) DO UPDATE SET
id = EXCLUDED.id
WHERE user_apps.user_id = $2
user_id = user_apps.user_id
RETURNING WITH (OLD AS old_row, NEW AS new_row)
old_row.id AS "old_id?",
COALESCE(old_row.user_id, new_row.user_id) AS "old_user_id!",
new_row.id AS "id!",
new_row.user_id AS "user_id!"
"#,
app.id,
app.user_id,
)
.fetch_optional(executor)
.fetch_one(executor)
.await
.map_err(DatabaseError::from)?;
let Some(row) = row else {
return Ok(GuardedUpsert::Rejected);
};
// `old_id` indicates whether there was a previous row before the upsert, other old fields are
// coalesced with the new row's values in case there was no previous row. This enables static
// typing for the previous row instead of having to check nullability for each old field
// individually.
let previous = row.old_id.map(|id| UserAppRow {
id,
user_id: row.old_user_id,
});
let app = UserAppRow {
id: row.id,
user_id: row.user_id,
};
Ok(if row.old_id.is_some() {
GuardedUpsert::Updated(app)
} else {
GuardedUpsert::Created(app)
Ok(Upserted {
previous,
current: UserAppRow {
id: row.id,
user_id: row.user_id,
},
})
}
pub async fn delete_by_id<'e, E>(
executor: E,
user_id: Uuid,
id: Uuid,
) -> Result<Delete, DatabaseError>
pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Option<UserAppRow>, DatabaseError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let deleted = sqlx::query!(
"DELETE FROM user_apps WHERE user_id = $1 AND id = $2 RETURNING id",
user_id,
let row = sqlx::query!(
"DELETE FROM user_apps WHERE id = $1 RETURNING id, user_id",
id,
)
.fetch_optional(executor)
.await
.map_err(DatabaseError::from)?;
Ok(if deleted.is_some() {
Delete::Deleted
} else {
Delete::NotFound
})
Ok(row.map(|row| UserAppRow {
id: row.id,
user_id: row.user_id,
}))
}