refactor: move DateRange validation to value object, add delete/cleanup
Some checks failed
CI / Check / Test (push) Failing after 40s

This commit is contained in:
2026-06-03 00:58:07 +02:00
parent 3a66f89609
commit fc086de7f7
8 changed files with 46 additions and 33 deletions

View File

@@ -282,7 +282,7 @@ impl WrapUpStatsQuery for PostgresWrapUpStatsQuery {
ORDER BY r.watched_at ASC"
);
let mut q = sqlx::query(&sql).bind(range.start).bind(range.end);
let mut q = sqlx::query(&sql).bind(range.start()).bind(range.end());
if let Some(ref uid) = scope_bind {
q = q.bind(uid);
}

View File

@@ -274,8 +274,8 @@ impl WrapUpStatsQuery for SqliteWrapUpStatsQuery {
scope: &WrapUpScope,
range: &DateRange,
) -> Result<Vec<WrapUpMovieRow>, DomainError> {
let start_str = range.start.format("%Y-%m-%d").to_string();
let end_str = range.end.format("%Y-%m-%d").to_string();
let start_str = range.start().format("%Y-%m-%d").to_string();
let end_str = range.end().format("%Y-%m-%d").to_string();
// 1) Main query
let (scope_clause, scope_bind) = match scope {

View File

@@ -277,8 +277,8 @@ impl SlideRenderer {
let year_label = format!(
"{} - {}",
report.date_range.start.format("%b %Y"),
report.date_range.end.format("%b %Y")
report.date_range.start().format("%b %Y"),
report.date_range.end().format("%b %Y")
);
self.draw_centered(&mut img, &year_label, (h / 6) as i32, 48.0, DIM);
self.draw_centered(

View File

@@ -1,24 +1,15 @@
use chrono::Utc;
use domain::errors::DomainError;
use domain::events::DomainEvent;
use domain::models::wrapup::WrapUpStatus;
use domain::models::wrapup::{DateRange, WrapUpStatus};
use domain::value_objects::{UserId, WrapUpId};
use crate::context::AppContext;
use crate::wrapup::commands::RequestWrapUpCommand;
pub async fn execute(ctx: &AppContext, cmd: RequestWrapUpCommand) -> Result<WrapUpId, DomainError> {
if cmd.end_date <= cmd.start_date {
return Err(DomainError::ValidationError(
"end_date must be after start_date".into(),
));
}
let days = (cmd.end_date - cmd.start_date).num_days();
if days > 366 {
return Err(DomainError::ValidationError(
"date range cannot exceed 366 days".into(),
));
}
let date_range = DateRange::new(cmd.start_date, cmd.end_date)?;
if cmd.end_date > Utc::now().date_naive() {
return Err(DomainError::ValidationError(
"end_date cannot be in the future".into(),
@@ -28,7 +19,7 @@ pub async fn execute(ctx: &AppContext, cmd: RequestWrapUpCommand) -> Result<Wrap
let existing = ctx
.repos
.wrapup_repo
.find_existing(cmd.user_id, cmd.start_date, cmd.end_date)
.find_existing(cmd.user_id, date_range.start(), date_range.end())
.await?;
if let Some(ref rec) = existing {
@@ -45,8 +36,8 @@ pub async fn execute(ctx: &AppContext, cmd: RequestWrapUpCommand) -> Result<Wrap
let record = domain::models::wrapup::WrapUpRecord {
id: id.clone(),
user_id: cmd.user_id,
start_date: cmd.start_date,
end_date: cmd.end_date,
start_date: date_range.start(),
end_date: date_range.end(),
status: WrapUpStatus::Pending,
report_json: None,
error_message: None,
@@ -60,8 +51,8 @@ pub async fn execute(ctx: &AppContext, cmd: RequestWrapUpCommand) -> Result<Wrap
.publish(&DomainEvent::WrapUpRequested {
wrapup_id: id.clone(),
user_id: cmd.user_id.map(UserId::from_uuid),
start_date: cmd.start_date,
end_date: cmd.end_date,
start_date: date_range.start(),
end_date: date_range.end(),
})
.await?;

View File

@@ -24,10 +24,7 @@ pub async fn execute(
};
let query = ComputeWrapUpQuery {
scope,
date_range: DateRange {
start: start_date,
end: end_date,
},
date_range: DateRange::new(start_date, end_date)?,
};
match compute::execute(ctx, query).await {

View File

@@ -32,10 +32,11 @@ fn make_row(title: &str, rating: u8, watched_at: &str) -> WrapUpMovieRow {
}
fn year_2024_range() -> DateRange {
DateRange {
start: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
end: NaiveDate::from_ymd_opt(2025, 1, 1).unwrap(),
}
DateRange::new(
NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
NaiveDate::from_ymd_opt(2025, 1, 1).unwrap(),
)
.unwrap()
}
#[tokio::test]

View File

@@ -6,8 +6,32 @@ use crate::value_objects::WrapUpId;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct DateRange {
pub start: NaiveDate,
pub end: NaiveDate,
start: NaiveDate,
end: NaiveDate,
}
impl DateRange {
pub fn new(start: NaiveDate, end: NaiveDate) -> Result<Self, crate::errors::DomainError> {
if end <= start {
return Err(crate::errors::DomainError::ValidationError(
"end_date must be after start_date".into(),
));
}
if (end - start).num_days() > 366 {
return Err(crate::errors::DomainError::ValidationError(
"date range cannot exceed 366 days".into(),
));
}
Ok(Self { start, end })
}
pub fn start(&self) -> NaiveDate {
self.start
}
pub fn end(&self) -> NaiveDate {
self.end
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]

View File

@@ -1047,7 +1047,7 @@ impl crate::ports::WrapUpStatsQuery for InMemoryWrapUpStatsQuery {
.iter()
.filter(|r| {
let date = r.watched_at.date();
date >= range.start && date < range.end
date >= range.start() && date < range.end()
})
.filter(|r| match scope {
crate::models::wrapup::WrapUpScope::User(uid) => r.user_id == *uid,