refactor: split domain models, move presentation logic out of app layer
Some checks failed
CI / Check / Test (push) Failing after 47s

Split domain/models/mod.rs (630 lines) into focused files:
movie.rs, review.rs, user.rs, stats.rs, enrichment.rs, feed.rs.

Move URL/date formatting from application use cases to
presentation mappers — use cases now return raw domain data.

Delete watchlist/get_page.rs (was pure presentation logic),
replace with presentation/mappers/watchlist.rs.

Document signature conventions in CONTRIBUTING.md.
This commit is contained in:
2026-06-09 02:29:11 +02:00
parent ac03182aa6
commit 70b3ca0f5c
23 changed files with 761 additions and 1150 deletions

View File

@@ -29,16 +29,6 @@ impl HtmlPageContext {
}
}
#[derive(Clone, Debug)]
pub struct WatchlistDisplayEntry {
pub poster_url: Option<String>,
pub movie_title: String,
pub release_year: u16,
pub movie_url: Option<String>,
pub added_at: String,
pub remove_url: Option<String>,
}
pub trait RssFeedRenderer: Send + Sync {
fn render_feed(&self, entries: &[DiaryEntry], title: &str) -> Result<String, String>;
}

View File

@@ -11,8 +11,8 @@ pub struct CurrentProfileData {
pub username: String,
pub display_name: Option<String>,
pub bio: Option<String>,
pub avatar_url: Option<String>,
pub banner_url: Option<String>,
pub avatar_path: Option<String>,
pub banner_path: Option<String>,
pub also_known_as: Option<String>,
pub fields: Vec<ProfileFieldData>,
pub role: String,
@@ -30,13 +30,6 @@ pub async fn execute(
.await?
.ok_or_else(|| DomainError::NotFound("User not found".into()))?;
let avatar_url = user
.avatar_path()
.map(|path| format!("{}/images/{}", ctx.config.base_url, path));
let banner_url = user
.banner_path()
.map(|path| format!("{}/images/{}", ctx.config.base_url, path));
let fields = user
.profile_fields()
.iter()
@@ -50,8 +43,8 @@ pub async fn execute(
username: user.username().value().to_string(),
display_name: user.display_name().map(|s| s.to_string()),
bio: user.bio().map(|s| s.to_string()),
avatar_url,
banner_url,
avatar_path: user.avatar_path().map(|s| s.to_string()),
banner_path: user.banner_path().map(|s| s.to_string()),
also_known_as: user.also_known_as().map(|s| s.to_string()),
fields,
role: user.role().as_str().into(),

View File

@@ -2,11 +2,10 @@ use crate::{
context::AppContext,
users::queries::{GetUserProfileQuery, ProfileView},
};
use chrono::Datelike;
use domain::{
errors::DomainError,
models::{
DiaryEntry, DiaryFilter, MonthActivity, SortDirection, UserStats, UserTrends,
DiaryEntry, DiaryFilter, SortDirection, UserStats, UserTrends,
collections::{PageParams, Paginated},
},
ports::FeedSortBy,
@@ -23,7 +22,7 @@ pub struct PendingFollowerView {
pub struct UserProfileData {
pub stats: UserStats,
pub entries: Option<Paginated<DiaryEntry>>,
pub history: Option<Vec<MonthActivity>>,
pub history: Option<Vec<DiaryEntry>>,
pub trends: Option<UserTrends>,
pub following_count: usize,
pub followers_count: usize,
@@ -53,8 +52,7 @@ pub async fn execute(
match query.view {
ProfileView::History => {
let all_entries = ctx.repos.diary.get_user_history(&user_id).await?;
let history = group_by_month(all_entries);
Ok(base(None, Some(history), None))
Ok(base(None, Some(all_entries), None))
}
ProfileView::Trends => {
let trends = ctx.repos.stats.get_user_trends(&user_id).await?;
@@ -138,52 +136,6 @@ fn paged_user_filter(
})
}
fn group_by_month(entries: Vec<DiaryEntry>) -> Vec<MonthActivity> {
use std::collections::BTreeMap;
let mut map: BTreeMap<(i32, u32), Vec<DiaryEntry>> = BTreeMap::new();
for entry in entries {
let watched_at = entry.review().watched_at();
let year = watched_at.year();
let month = watched_at.month();
map.entry((year, month)).or_default().push(entry);
}
map.into_iter()
.rev()
.map(|((year, month), entries)| {
let year_month = format!("{:04}-{:02}", year, month);
MonthActivity {
month_label: format_year_month_long(&year_month),
count: entries.len() as i64,
entries,
year_month,
}
})
.collect()
}
fn format_year_month_long(ym: &str) -> String {
let parts: Vec<&str> = ym.splitn(2, '-').collect();
if parts.len() != 2 {
return ym.to_string();
}
let month = match parts[1] {
"01" => "January",
"02" => "February",
"03" => "March",
"04" => "April",
"05" => "May",
"06" => "June",
"07" => "July",
"08" => "August",
"09" => "September",
"10" => "October",
"11" => "November",
"12" => "December",
_ => parts[1],
};
format!("{} {}", month, parts[0])
}
#[cfg(test)]
#[path = "tests/get_profile.rs"]
mod tests;
@@ -192,28 +144,6 @@ mod tests;
mod helper_tests {
use super::*;
#[test]
fn format_year_month_long_all_months() {
assert_eq!(format_year_month_long("2024-01"), "January 2024");
assert_eq!(format_year_month_long("2024-02"), "February 2024");
assert_eq!(format_year_month_long("2024-03"), "March 2024");
assert_eq!(format_year_month_long("2024-04"), "April 2024");
assert_eq!(format_year_month_long("2024-05"), "May 2024");
assert_eq!(format_year_month_long("2024-06"), "June 2024");
assert_eq!(format_year_month_long("2024-07"), "July 2024");
assert_eq!(format_year_month_long("2024-08"), "August 2024");
assert_eq!(format_year_month_long("2024-09"), "September 2024");
assert_eq!(format_year_month_long("2024-10"), "October 2024");
assert_eq!(format_year_month_long("2024-11"), "November 2024");
assert_eq!(format_year_month_long("2024-12"), "December 2024");
}
#[test]
fn format_year_month_long_invalid() {
assert_eq!(format_year_month_long("invalid"), "invalid");
assert_eq!(format_year_month_long("2024-99"), "99 2024");
}
#[test]
fn feed_sort_to_direction_all_variants() {
use domain::ports::FeedSortBy;
@@ -235,74 +165,6 @@ mod helper_tests {
));
}
#[test]
fn group_by_month_empty() {
assert!(group_by_month(vec![]).is_empty());
}
#[test]
fn group_by_month_groups_entries() {
use chrono::NaiveDateTime;
use domain::models::{Movie, Review};
use domain::value_objects::{MovieId, MovieTitle, Rating, ReleaseYear, UserId};
let movie = Movie::from_persistence(
MovieId::generate(),
None,
MovieTitle::new("Test".into()).unwrap(),
ReleaseYear::new(2024).unwrap(),
None,
None,
);
let uid = UserId::from_uuid(uuid::Uuid::new_v4());
let jan =
NaiveDateTime::parse_from_str("2024-01-15 10:00:00", "%Y-%m-%d %H:%M:%S").unwrap();
let jan2 =
NaiveDateTime::parse_from_str("2024-01-20 10:00:00", "%Y-%m-%d %H:%M:%S").unwrap();
let mar =
NaiveDateTime::parse_from_str("2024-03-05 10:00:00", "%Y-%m-%d %H:%M:%S").unwrap();
let r1 = Review::new(
movie.id().clone(),
uid.clone(),
Rating::new(4).unwrap(),
None,
jan,
)
.unwrap();
let r2 = Review::new(
movie.id().clone(),
uid.clone(),
Rating::new(3).unwrap(),
None,
jan2,
)
.unwrap();
let r3 = Review::new(
movie.id().clone(),
uid.clone(),
Rating::new(5).unwrap(),
None,
mar,
)
.unwrap();
let entries = vec![
DiaryEntry::new(movie.clone(), r1),
DiaryEntry::new(movie.clone(), r2),
DiaryEntry::new(movie.clone(), r3),
];
let result = group_by_month(entries);
// Reversed: March first, then January
assert_eq!(result.len(), 2);
assert_eq!(result[0].month_label, "March 2024");
assert_eq!(result[0].count, 1);
assert_eq!(result[1].month_label, "January 2024");
assert_eq!(result[1].count, 2);
}
#[test]
fn paged_user_filter_builds_correctly() {
let uid = UserId::from_uuid(uuid::Uuid::new_v4());

View File

@@ -105,10 +105,8 @@ async fn returns_profile_with_avatar_banner_and_fields() {
assert_eq!(profile.username, "fulluser");
assert_eq!(profile.display_name.as_deref(), Some("Full Name"));
assert_eq!(profile.bio.as_deref(), Some("My bio"));
assert!(profile.avatar_url.is_some());
assert!(profile.avatar_url.unwrap().contains("avatars/abc123"));
assert!(profile.banner_url.is_some());
assert!(profile.banner_url.unwrap().contains("banners/def456"));
assert_eq!(profile.avatar_path.as_deref(), Some("avatars/abc123"));
assert_eq!(profile.banner_path.as_deref(), Some("banners/def456"));
assert_eq!(profile.fields.len(), 1);
assert_eq!(profile.fields[0].name, "Website");
assert_eq!(profile.fields[0].value, "https://example.com");

View File

@@ -1,90 +0,0 @@
use domain::{errors::DomainError, value_objects::UserId};
use crate::{
context::AppContext, ports::WatchlistDisplayEntry, watchlist::queries::GetWatchlistQuery,
};
pub struct WatchlistPageResult {
pub display_entries: Vec<WatchlistDisplayEntry>,
pub has_more: bool,
pub current_offset: u32,
pub limit: u32,
}
pub async fn execute(
ctx: &AppContext,
query: GetWatchlistQuery,
is_owner: bool,
) -> Result<WatchlistPageResult, DomainError> {
let user_id = UserId::from_uuid(query.user_id);
let is_local = ctx.repos.user.find_by_id(&user_id).await?.is_some();
if is_local {
let page = crate::watchlist::get::execute(ctx, query).await?;
let has_more = page.offset + page.limit < page.total_count as u32;
let display_entries = page
.items
.iter()
.map(|w| {
let remove_url = if is_owner {
Some(format!("/watchlist/{}/remove", w.movie.id().value()))
} else {
None
};
WatchlistDisplayEntry {
poster_url: w
.movie
.poster_path()
.map(|p| format!("/images/{}", p.value())),
movie_title: w.movie.title().value().to_string(),
release_year: w.movie.release_year().value(),
movie_url: Some(format!("/movies/{}", w.movie.id().value())),
added_at: w.entry.added_at.format("%b %-d, %Y").to_string(),
remove_url,
}
})
.collect();
Ok(WatchlistPageResult {
display_entries,
has_more,
current_offset: page.offset,
limit: page.limit,
})
} else {
load_remote_watchlist(ctx, query.user_id).await
}
}
async fn load_remote_watchlist(
ctx: &AppContext,
user_id: uuid::Uuid,
) -> Result<WatchlistPageResult, DomainError> {
let remote_entries = ctx
.repos
.remote_watchlist
.get_by_derived_uuid(user_id)
.await
.unwrap_or_default();
let len = remote_entries.len() as u32;
let display_entries = remote_entries
.into_iter()
.map(|e| WatchlistDisplayEntry {
poster_url: e.poster_url,
movie_title: e.movie_title,
release_year: e.release_year,
movie_url: None,
added_at: e.added_at.format("%b %-d, %Y").to_string(),
remove_url: None,
})
.collect();
Ok(WatchlistPageResult {
display_entries,
has_more: false,
current_offset: 0,
limit: len,
})
}
#[cfg(test)]
#[path = "tests/get_page.rs"]
mod tests;

View File

@@ -1,7 +1,6 @@
pub mod add;
pub mod commands;
pub mod get;
pub mod get_page;
pub mod is_on;
pub mod queries;
pub mod remove;

View File

@@ -1,311 +0,0 @@
use std::sync::Arc;
use async_trait::async_trait;
use domain::errors::DomainError;
use domain::models::collections::{PageParams, Paginated};
use domain::models::watchlist::{WatchlistEntry, WatchlistWithMovie};
use domain::models::{Movie, UserRole};
use domain::ports::WatchlistRepository;
use domain::value_objects::{Email, MovieId, MovieTitle, PosterPath, ReleaseYear, UserId};
use crate::auth::commands::RegisterCommand;
use crate::auth::register;
use crate::test_helpers::TestContextBuilder;
use crate::watchlist::get_page;
use crate::watchlist::queries::GetWatchlistQuery;
struct FakeWatchlistWithItems {
user_id: UserId,
items: Vec<WatchlistWithMovie>,
}
#[async_trait]
impl WatchlistRepository for FakeWatchlistWithItems {
async fn add(&self, _entry: &WatchlistEntry) -> Result<(), DomainError> {
Ok(())
}
async fn remove(&self, _user_id: &UserId, _movie_id: &MovieId) -> Result<(), DomainError> {
Ok(())
}
async fn remove_if_present(
&self,
_user_id: &UserId,
_movie_id: &MovieId,
) -> Result<bool, DomainError> {
Ok(false)
}
async fn get_for_user(
&self,
user_id: &UserId,
_page: &PageParams,
) -> Result<Paginated<WatchlistWithMovie>, DomainError> {
if user_id == &self.user_id {
Ok(Paginated {
total_count: self.items.len() as u64,
limit: 20,
offset: 0,
items: self.items.clone(),
})
} else {
Ok(Paginated {
items: vec![],
total_count: 0,
limit: 20,
offset: 0,
})
}
}
async fn contains(&self, _user_id: &UserId, _movie_id: &MovieId) -> Result<bool, DomainError> {
Ok(false)
}
}
#[tokio::test]
async fn returns_empty_for_local_user() {
let ctx = TestContextBuilder::new().build();
register::execute(
&ctx,
RegisterCommand {
email: "wl@test.com".into(),
username: "wluser".into(),
password: "password123".into(),
role: UserRole::Standard,
},
)
.await
.unwrap();
let email = Email::new("wl@test.com".into()).unwrap();
let user = ctx.repos.user.find_by_email(&email).await.unwrap().unwrap();
let uid = user.id().value();
let result = get_page::execute(
&ctx,
GetWatchlistQuery {
user_id: uid,
limit: None,
offset: None,
},
true,
)
.await
.unwrap();
assert!(result.display_entries.is_empty());
}
#[tokio::test]
async fn returns_display_entries_for_local_user_with_items() {
let ctx = TestContextBuilder::new().build();
register::execute(
&ctx,
RegisterCommand {
email: "wl2@test.com".into(),
username: "wluser2".into(),
password: "password123".into(),
role: UserRole::Standard,
},
)
.await
.unwrap();
let email = Email::new("wl2@test.com".into()).unwrap();
let user = ctx.repos.user.find_by_email(&email).await.unwrap().unwrap();
let uid = user.id().value();
let result = get_page::execute(
&ctx,
GetWatchlistQuery {
user_id: uid,
limit: Some(20),
offset: Some(0),
},
true,
)
.await
.unwrap();
// InMemory get_for_user returns empty, but the local-user branch is exercised
assert!(!result.has_more);
assert_eq!(result.current_offset, 0);
}
#[tokio::test]
async fn returns_remote_watchlist_for_unknown_user() {
let ctx = TestContextBuilder::new().build();
let unknown_uid = uuid::Uuid::new_v4();
let result = get_page::execute(
&ctx,
GetWatchlistQuery {
user_id: unknown_uid,
limit: None,
offset: None,
},
false,
)
.await
.unwrap();
// NoopRemoteWatchlistRepository returns empty
assert!(result.display_entries.is_empty());
assert!(!result.has_more);
assert_eq!(result.current_offset, 0);
}
#[tokio::test]
async fn maps_display_entries_for_owner() {
let uid = uuid::Uuid::new_v4();
let user_id = UserId::from_uuid(uid);
let movie_id = MovieId::generate();
let movie = Movie::from_persistence(
movie_id.clone(),
None,
MovieTitle::new("Blade Runner".into()).unwrap(),
ReleaseYear::new(1982).unwrap(),
None,
Some(PosterPath::new("poster123.jpg".into()).unwrap()),
);
let entry = WatchlistEntry::new(user_id.clone(), movie_id.clone());
let fake_wl = Arc::new(FakeWatchlistWithItems {
user_id: user_id.clone(),
items: vec![WatchlistWithMovie {
entry,
movie: movie.clone(),
}],
});
let ctx = TestContextBuilder::new()
.with_watchlist(fake_wl as _)
.build();
// register user so find_by_id returns Some
register::execute(
&ctx,
RegisterCommand {
email: "wlmap@test.com".into(),
username: "wlmapuser".into(),
password: "password123".into(),
role: UserRole::Standard,
},
)
.await
.unwrap();
let email = Email::new("wlmap@test.com".into()).unwrap();
let user = ctx.repos.user.find_by_email(&email).await.unwrap().unwrap();
let real_uid = user.id().value();
// Rebuild with the real user_id in the fake
let movie_id2 = MovieId::generate();
let movie2 = Movie::from_persistence(
movie_id2.clone(),
None,
MovieTitle::new("Blade Runner".into()).unwrap(),
ReleaseYear::new(1982).unwrap(),
None,
Some(PosterPath::new("poster123.jpg".into()).unwrap()),
);
let entry2 = WatchlistEntry::new(UserId::from_uuid(real_uid), movie_id2.clone());
let fake_wl2 = Arc::new(FakeWatchlistWithItems {
user_id: UserId::from_uuid(real_uid),
items: vec![WatchlistWithMovie {
entry: entry2,
movie: movie2.clone(),
}],
});
let ctx2 = TestContextBuilder::new()
.with_watchlist(fake_wl2 as _)
.with_users(ctx.repos.user.clone())
.build();
let result = get_page::execute(
&ctx2,
GetWatchlistQuery {
user_id: real_uid,
limit: Some(20),
offset: Some(0),
},
true,
)
.await
.unwrap();
assert_eq!(result.display_entries.len(), 1);
let de = &result.display_entries[0];
assert_eq!(de.movie_title, "Blade Runner");
assert_eq!(de.release_year, 1982);
assert_eq!(de.poster_url.as_deref(), Some("/images/poster123.jpg"));
assert!(de.movie_url.is_some());
assert!(de.remove_url.is_some()); // owner can remove
}
#[tokio::test]
async fn maps_display_entries_for_non_owner() {
let ctx = TestContextBuilder::new().build();
register::execute(
&ctx,
RegisterCommand {
email: "wlno@test.com".into(),
username: "wlnoowner".into(),
password: "password123".into(),
role: UserRole::Standard,
},
)
.await
.unwrap();
let email = Email::new("wlno@test.com".into()).unwrap();
let user = ctx.repos.user.find_by_email(&email).await.unwrap().unwrap();
let real_uid = user.id().value();
let movie_id = MovieId::generate();
let movie = Movie::from_persistence(
movie_id.clone(),
None,
MovieTitle::new("Alien".into()).unwrap(),
ReleaseYear::new(1979).unwrap(),
None,
None,
);
let entry = WatchlistEntry::new(UserId::from_uuid(real_uid), movie_id.clone());
let fake_wl = Arc::new(FakeWatchlistWithItems {
user_id: UserId::from_uuid(real_uid),
items: vec![WatchlistWithMovie {
entry,
movie: movie.clone(),
}],
});
let ctx2 = TestContextBuilder::new()
.with_watchlist(fake_wl as _)
.with_users(ctx.repos.user.clone())
.build();
let result = get_page::execute(
&ctx2,
GetWatchlistQuery {
user_id: real_uid,
limit: Some(20),
offset: Some(0),
},
false, // not owner
)
.await
.unwrap();
assert_eq!(result.display_entries.len(), 1);
let de = &result.display_entries[0];
assert_eq!(de.movie_title, "Alien");
assert!(de.poster_url.is_none()); // no poster
assert!(de.remove_url.is_none()); // not owner
}