40 lines
921 B
Rust
40 lines
921 B
Rust
use anyhow::Result;
|
|
use sqlx::PgPool;
|
|
use uuid::Uuid;
|
|
|
|
use crate::models::AuditLogEntry;
|
|
|
|
pub async fn list_for_user(
|
|
pool: &PgPool,
|
|
user_id: Uuid,
|
|
limit: i64,
|
|
offset: i64,
|
|
) -> Result<Vec<AuditLogEntry>> {
|
|
let limit = limit.clamp(1, 200);
|
|
let offset = offset.max(0);
|
|
|
|
let rows = sqlx::query_as(
|
|
"SELECT id, user_id, action, folder, type, name, detail, created_at \
|
|
FROM audit_log \
|
|
WHERE user_id = $1 \
|
|
ORDER BY created_at DESC, id DESC \
|
|
LIMIT $2 OFFSET $3",
|
|
)
|
|
.bind(user_id)
|
|
.bind(limit)
|
|
.bind(offset)
|
|
.fetch_all(pool)
|
|
.await?;
|
|
|
|
Ok(rows)
|
|
}
|
|
|
|
pub async fn count_for_user(pool: &PgPool, user_id: Uuid) -> Result<i64> {
|
|
let count: i64 =
|
|
sqlx::query_scalar("SELECT COUNT(*)::bigint FROM audit_log WHERE user_id = $1")
|
|
.bind(user_id)
|
|
.fetch_one(pool)
|
|
.await?;
|
|
Ok(count)
|
|
}
|