refactor: split domain models, move presentation logic out of app layer
Some checks failed
CI / Check / Test (push) Failing after 47s
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:
@@ -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());
|
||||
|
||||
Reference in New Issue
Block a user