From 85e254fee20268edd7c5f654628f0f853e62134f Mon Sep 17 00:00:00 2001 From: Gabriel Kaszewski Date: Mon, 4 May 2026 18:40:58 +0200 Subject: [PATCH] feat: impl UserRepository::list_with_stats --- crates/adapters/sqlite/src/users.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/crates/adapters/sqlite/src/users.rs b/crates/adapters/sqlite/src/users.rs index 4c48f60..7f6ee39 100644 --- a/crates/adapters/sqlite/src/users.rs +++ b/crates/adapters/sqlite/src/users.rs @@ -8,6 +8,7 @@ use domain::{ ports::UserRepository, value_objects::{Email, PasswordHash, UserId}, }; +use super::models::UserSummaryRow; pub struct SqliteUserRepository { pool: SqlitePool, @@ -97,6 +98,29 @@ impl UserRepository for SqliteUserRepository { } } } + + async fn list_with_stats(&self) -> Result, DomainError> { + sqlx::query_as!( + UserSummaryRow, + r#"SELECT u.id, + u.email, + COUNT(r.id) AS "total_movies!: i64", + AVG(CAST(r.rating AS REAL)) AS "avg_rating: Option" + FROM users u + LEFT JOIN reviews r ON r.user_id = u.id + GROUP BY u.id, u.email + ORDER BY u.email ASC"# + ) + .fetch_all(&self.pool) + .await + .map_err(|e| { + tracing::error!("Database error: {:?}", e); + DomainError::InfrastructureError("Database operation failed".into()) + })? + .into_iter() + .map(UserSummaryRow::to_domain) + .collect() + } } #[cfg(test)]