164 lines
3.7 KiB
Rust
164 lines
3.7 KiB
Rust
use uuid::Uuid;
|
|
|
|
use super::{
|
|
DatabaseError,
|
|
dtos::{Delete, Upsert, UserAppSecretRow},
|
|
};
|
|
|
|
pub struct Query {
|
|
pub app_id: Uuid,
|
|
pub limit: Option<i64>,
|
|
pub offset: Option<i64>,
|
|
}
|
|
|
|
impl Default for Query {
|
|
fn default() -> Self {
|
|
Self {
|
|
app_id: Uuid::nil(),
|
|
limit: None,
|
|
offset: None,
|
|
}
|
|
}
|
|
}
|
|
|
|
pub async fn query<'e, E>(executor: E, query: Query) -> Result<Vec<UserAppSecretRow>, DatabaseError>
|
|
where
|
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
|
{
|
|
sqlx::query_as!(
|
|
UserAppSecretRow,
|
|
r#"
|
|
SELECT id, app_id, secret_hash, expires_at, created_at
|
|
FROM user_app_secrets
|
|
WHERE app_id = $1
|
|
ORDER BY created_at DESC, id DESC
|
|
LIMIT $2
|
|
OFFSET $3
|
|
"#,
|
|
query.app_id,
|
|
query.limit,
|
|
query.offset,
|
|
)
|
|
.fetch_all(executor)
|
|
.await
|
|
.map_err(DatabaseError::from)
|
|
}
|
|
|
|
pub async fn get_by_id<'e, E>(
|
|
executor: E,
|
|
app_id: Uuid,
|
|
id: Uuid,
|
|
) -> Result<Option<UserAppSecretRow>, DatabaseError>
|
|
where
|
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
|
{
|
|
sqlx::query_as!(
|
|
UserAppSecretRow,
|
|
r#"
|
|
SELECT id, app_id, secret_hash, expires_at, created_at
|
|
FROM user_app_secrets
|
|
WHERE app_id = $1 AND id = $2
|
|
"#,
|
|
app_id,
|
|
id,
|
|
)
|
|
.fetch_optional(executor)
|
|
.await
|
|
.map_err(DatabaseError::from)
|
|
}
|
|
|
|
pub async fn upsert<'e, E>(
|
|
executor: E,
|
|
secret: UserAppSecretRow,
|
|
) -> Result<Upsert<UserAppSecretRow>, DatabaseError>
|
|
where
|
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
|
{
|
|
let row = sqlx::query!(
|
|
r#"
|
|
INSERT INTO user_app_secrets (id, app_id, secret_hash, expires_at)
|
|
VALUES ($1, $2, $3, $4)
|
|
ON CONFLICT (id) DO UPDATE SET
|
|
secret_hash = EXCLUDED.secret_hash,
|
|
expires_at = EXCLUDED.expires_at
|
|
WHERE user_app_secrets.app_id = EXCLUDED.app_id
|
|
RETURNING WITH (OLD AS old_row, NEW AS new_row)
|
|
old_row.id AS "old_id?",
|
|
new_row.id AS "id!",
|
|
new_row.app_id AS "app_id!",
|
|
new_row.secret_hash AS "secret_hash!",
|
|
new_row.expires_at AS "expires_at!",
|
|
new_row.created_at AS "created_at!"
|
|
"#,
|
|
secret.id,
|
|
secret.app_id,
|
|
secret.secret_hash,
|
|
secret.expires_at,
|
|
)
|
|
.fetch_one(executor)
|
|
.await
|
|
.map_err(DatabaseError::from)?;
|
|
|
|
let secret = UserAppSecretRow {
|
|
id: row.id,
|
|
app_id: row.app_id,
|
|
secret_hash: row.secret_hash,
|
|
expires_at: row.expires_at,
|
|
created_at: row.created_at,
|
|
};
|
|
|
|
Ok(if row.old_id.is_some() {
|
|
Upsert::Updated(secret)
|
|
} else {
|
|
Upsert::Created(secret)
|
|
})
|
|
}
|
|
|
|
pub async fn verify<'e, E>(
|
|
executor: E,
|
|
app_id: Uuid,
|
|
secret_hash: &[u8],
|
|
) -> Result<bool, DatabaseError>
|
|
where
|
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
|
{
|
|
sqlx::query_scalar!(
|
|
r#"
|
|
SELECT EXISTS (
|
|
SELECT 1
|
|
FROM user_app_secrets
|
|
WHERE app_id = $1 AND secret_hash = $2 AND expires_at > now()
|
|
) AS "is_valid!"
|
|
"#,
|
|
app_id,
|
|
secret_hash,
|
|
)
|
|
.fetch_one(executor)
|
|
.await
|
|
.map_err(DatabaseError::from)
|
|
}
|
|
|
|
pub async fn delete_by_id<'e, E>(
|
|
executor: E,
|
|
app_id: Uuid,
|
|
id: Uuid,
|
|
) -> Result<Delete, DatabaseError>
|
|
where
|
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
|
{
|
|
let deleted = sqlx::query!(
|
|
"DELETE FROM user_app_secrets WHERE app_id = $1 AND id = $2 RETURNING id",
|
|
app_id,
|
|
id,
|
|
)
|
|
.fetch_optional(executor)
|
|
.await
|
|
.map_err(DatabaseError::from)?;
|
|
|
|
Ok(if deleted.is_some() {
|
|
Delete::Deleted
|
|
} else {
|
|
Delete::NotFound
|
|
})
|
|
}
|