diff --git a/crates/adapters/sqlite/src/models.rs b/crates/adapters/sqlite/src/models.rs index cdb9983..cd239e8 100644 --- a/crates/adapters/sqlite/src/models.rs +++ b/crates/adapters/sqlite/src/models.rs @@ -1,7 +1,7 @@ use chrono::NaiveDateTime; use domain::{ errors::DomainError, - models::{DiaryEntry, Movie, Review}, + models::{DiaryEntry, FeedEntry, Movie, Review, UserSummary}, value_objects::{ Comment, ExternalMetadataId, MovieId, MovieTitle, PosterPath, Rating, ReleaseYear, ReviewId, UserId, @@ -111,6 +111,91 @@ impl DiaryRow { } } +// Like DiaryRow but includes user_email from JOIN with users table +#[derive(sqlx::FromRow)] +pub(crate) struct FeedRow { + pub id: String, + pub external_metadata_id: Option, + pub title: String, + pub release_year: i64, + pub director: Option, + pub poster_path: Option, + pub review_id: String, + pub movie_id: String, + pub user_id: String, + pub rating: i64, + pub comment: Option, + pub watched_at: String, + pub created_at: String, + pub user_email: String, +} + +impl FeedRow { + pub fn to_domain(self) -> Result { + let diary = DiaryRow { + id: self.id, + external_metadata_id: self.external_metadata_id, + title: self.title, + release_year: self.release_year, + director: self.director, + poster_path: self.poster_path, + review_id: self.review_id, + movie_id: self.movie_id, + user_id: self.user_id, + rating: self.rating, + comment: self.comment, + watched_at: self.watched_at, + created_at: self.created_at, + } + .to_domain()?; + Ok(FeedEntry::new(diary, self.user_email)) + } +} + +#[derive(sqlx::FromRow)] +pub(crate) struct UserSummaryRow { + pub id: String, + pub email: String, + pub total_movies: i64, + pub avg_rating: Option, +} + +impl UserSummaryRow { + pub fn to_domain(self) -> Result { + Ok(UserSummary { + user_id: UserId::from_uuid(parse_uuid(&self.id)?), + email: self.email, + total_movies: self.total_movies, + avg_rating: self.avg_rating, + }) + } +} + +#[derive(sqlx::FromRow)] +pub(crate) struct UserTotalsRow { + pub total: i64, + pub avg_rating: Option, +} + +#[derive(sqlx::FromRow)] +pub(crate) struct DirectorCountRow { + pub director: String, + pub count: i64, +} + +#[derive(sqlx::FromRow)] +pub(crate) struct MonthCountRow { + pub month: String, + pub count: i64, +} + +#[derive(sqlx::FromRow)] +pub(crate) struct MonthlyRatingRow { + pub month: String, + pub avg_rating: f64, + pub count: i64, +} + pub(crate) fn parse_uuid(s: &str) -> Result { Uuid::parse_str(s) .map_err(|e| DomainError::InfrastructureError(format!("Invalid UUID '{}': {}", s, e)))