test-helpers out of production, decompose get_user_profile_html, dedup secure_flag, remove vestigial social_query, drop PersistedImportSession, LoginCommand rename, DeleteAccountDeps, PersonId macro, ReviewSortBy rename, TUI Command for fs::read, generic PaginatedResponse<T>, noop ports for production fallbacks
51 lines
1.2 KiB
Rust
51 lines
1.2 KiB
Rust
use domain::{
|
|
errors::DomainError,
|
|
events::DomainEvent,
|
|
models::{Goal, GoalType, GoalWithProgress},
|
|
value_objects::UserId,
|
|
};
|
|
|
|
use super::{commands::CreateGoalCommand, deps::GoalCommandDeps};
|
|
|
|
pub async fn execute(
|
|
deps: &GoalCommandDeps,
|
|
cmd: CreateGoalCommand,
|
|
) -> Result<GoalWithProgress, DomainError> {
|
|
let user_id = UserId::from_uuid(cmd.user_id);
|
|
|
|
let existing = deps.goal_query.find_by_user_and_year(&user_id, cmd.year).await?;
|
|
if existing.is_some() {
|
|
return Err(DomainError::ValidationError(
|
|
"Goal already exists for this year".into(),
|
|
));
|
|
}
|
|
|
|
let g = Goal::new(
|
|
user_id.clone(),
|
|
cmd.year,
|
|
cmd.target_count,
|
|
GoalType::Movies,
|
|
)?;
|
|
deps.goal_command.save(&g).await?;
|
|
|
|
let current_count = deps.stats.count_reviews_in_year(&user_id, cmd.year).await?;
|
|
|
|
deps.event_publisher
|
|
.publish(&DomainEvent::GoalCreated {
|
|
goal_id: g.id().clone(),
|
|
user_id,
|
|
year: cmd.year,
|
|
target_count: cmd.target_count,
|
|
})
|
|
.await?;
|
|
|
|
Ok(GoalWithProgress {
|
|
goal: g,
|
|
current_count,
|
|
})
|
|
}
|
|
|
|
#[cfg(test)]
|
|
#[path = "tests/create.rs"]
|
|
mod tests;
|