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:
@@ -58,12 +58,13 @@ pub async fn get_profile(
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
let base_url = &state.app_ctx.config.base_url;
|
||||
Ok(Json(ProfileResponse {
|
||||
username: profile.username,
|
||||
display_name: profile.display_name,
|
||||
bio: profile.bio,
|
||||
avatar_url: profile.avatar_url,
|
||||
banner_url: profile.banner_url,
|
||||
avatar_url: profile.avatar_path.map(|p| format!("{}/images/{}", base_url, p)),
|
||||
banner_url: profile.banner_path.map(|p| format!("{}/images/{}", base_url, p)),
|
||||
also_known_as: profile.also_known_as,
|
||||
fields: profile
|
||||
.fields
|
||||
@@ -286,8 +287,8 @@ pub async fn get_user_profile(
|
||||
offset: p.offset,
|
||||
});
|
||||
|
||||
let history = profile.history.map(|months| {
|
||||
months
|
||||
let history = profile.history.map(|entries| {
|
||||
crate::mappers::users::group_by_month(entries)
|
||||
.into_iter()
|
||||
.map(|m| MonthActivityDto {
|
||||
year_month: m.year_month,
|
||||
@@ -542,8 +543,10 @@ pub async fn get_user_profile_html(
|
||||
.most_active_month
|
||||
.clone()
|
||||
.unwrap_or_else(|| "\u{2014}".to_string());
|
||||
let heatmap = profile
|
||||
let history = profile
|
||||
.history
|
||||
.map(crate::mappers::users::group_by_month);
|
||||
let heatmap = history
|
||||
.as_deref()
|
||||
.map(build_heatmap)
|
||||
.unwrap_or_default();
|
||||
@@ -594,7 +597,7 @@ pub async fn get_user_profile_html(
|
||||
current_offset: offset,
|
||||
has_more,
|
||||
limit,
|
||||
history: profile.history.as_ref(),
|
||||
history: history.as_ref(),
|
||||
trends: profile.trends.as_ref(),
|
||||
monthly_rating_rows,
|
||||
heatmap,
|
||||
@@ -618,7 +621,7 @@ pub async fn get_user_profile_html(
|
||||
current_offset: offset,
|
||||
has_more,
|
||||
limit,
|
||||
history: profile.history.as_ref(),
|
||||
history: history.as_ref(),
|
||||
trends: profile.trends.as_ref(),
|
||||
monthly_rating_rows,
|
||||
heatmap,
|
||||
|
||||
@@ -178,19 +178,39 @@ pub async fn get_watchlist_page(
|
||||
let ctx = build_page_context(&state, viewer_id.clone(), csrf.0).await;
|
||||
let is_owner = viewer_id.map(|u| u.value() == owner_id).unwrap_or(false);
|
||||
|
||||
let result = match application::watchlist::get_page::execute(
|
||||
&state.app_ctx,
|
||||
application::watchlist::queries::GetWatchlistQuery {
|
||||
user_id: owner_id,
|
||||
limit: params.limit.or(Some(20)),
|
||||
offset: params.offset.or(Some(0)),
|
||||
},
|
||||
is_owner,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(r) => r,
|
||||
Err(e) => return crate::errors::domain_error_response(e),
|
||||
let user_id = domain::value_objects::UserId::from_uuid(owner_id);
|
||||
let is_local = state
|
||||
.app_ctx
|
||||
.repos
|
||||
.user
|
||||
.find_by_id(&user_id)
|
||||
.await
|
||||
.map(|u| u.is_some())
|
||||
.unwrap_or(false);
|
||||
|
||||
let result = if is_local {
|
||||
match get_watchlist::execute(
|
||||
&state.app_ctx,
|
||||
application::watchlist::queries::GetWatchlistQuery {
|
||||
user_id: owner_id,
|
||||
limit: params.limit.or(Some(20)),
|
||||
offset: params.offset.or(Some(0)),
|
||||
},
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(page) => crate::mappers::watchlist::build_watchlist_page(page, is_owner),
|
||||
Err(e) => return crate::errors::domain_error_response(e),
|
||||
}
|
||||
} else {
|
||||
let remote_entries = state
|
||||
.app_ctx
|
||||
.repos
|
||||
.remote_watchlist
|
||||
.get_by_derived_uuid(owner_id)
|
||||
.await
|
||||
.unwrap_or_default();
|
||||
crate::mappers::watchlist::build_remote_watchlist_page(remote_entries)
|
||||
};
|
||||
|
||||
render_page(WatchlistTemplate {
|
||||
|
||||
@@ -3,3 +3,4 @@ pub mod import;
|
||||
pub mod integrations;
|
||||
pub mod movies;
|
||||
pub mod users;
|
||||
pub mod watchlist;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use application::users::get_profile::PendingFollowerView;
|
||||
use domain::models::UserSummary;
|
||||
use chrono::Datelike;
|
||||
use domain::models::{DiaryEntry, MonthActivity, UserSummary};
|
||||
use domain::ports::RemoteActorInfo;
|
||||
use template_askama::{RemoteActorData, RemoteActorDisplay, UserSummaryView};
|
||||
|
||||
@@ -43,3 +44,49 @@ pub fn pending_follower_data(p: &PendingFollowerView) -> RemoteActorData {
|
||||
avatar_url: p.avatar_url.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
pub 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])
|
||||
}
|
||||
|
||||
65
crates/presentation/src/mappers/watchlist.rs
Normal file
65
crates/presentation/src/mappers/watchlist.rs
Normal file
@@ -0,0 +1,65 @@
|
||||
use domain::models::{RemoteWatchlistEntry, WatchlistWithMovie, collections::Paginated};
|
||||
use template_askama::WatchlistDisplayEntry;
|
||||
|
||||
pub struct WatchlistPageResult {
|
||||
pub display_entries: Vec<WatchlistDisplayEntry>,
|
||||
pub has_more: bool,
|
||||
pub current_offset: u32,
|
||||
pub limit: u32,
|
||||
}
|
||||
|
||||
pub fn build_watchlist_page(
|
||||
page: Paginated<WatchlistWithMovie>,
|
||||
is_owner: bool,
|
||||
) -> WatchlistPageResult {
|
||||
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();
|
||||
WatchlistPageResult {
|
||||
display_entries,
|
||||
has_more,
|
||||
current_offset: page.offset,
|
||||
limit: page.limit,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn build_remote_watchlist_page(entries: Vec<RemoteWatchlistEntry>) -> WatchlistPageResult {
|
||||
let len = entries.len() as u32;
|
||||
let display_entries = 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();
|
||||
WatchlistPageResult {
|
||||
display_entries,
|
||||
has_more: false,
|
||||
current_offset: 0,
|
||||
limit: len,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user