refactor(goals): scoped Arc deps instead of AppContext
This commit is contained in:
@@ -1,22 +1,23 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use domain::{
|
||||
errors::DomainError,
|
||||
events::DomainEvent,
|
||||
models::{Goal, GoalType, GoalWithProgress},
|
||||
ports::{EventPublisher, GoalRepository},
|
||||
value_objects::UserId,
|
||||
};
|
||||
|
||||
use super::commands::CreateGoalCommand;
|
||||
use crate::context::AppContext;
|
||||
|
||||
pub async fn execute(
|
||||
ctx: &AppContext,
|
||||
goal: Arc<dyn GoalRepository>,
|
||||
event_publisher: Arc<dyn EventPublisher>,
|
||||
cmd: CreateGoalCommand,
|
||||
) -> Result<GoalWithProgress, DomainError> {
|
||||
let user_id = UserId::from_uuid(cmd.user_id);
|
||||
|
||||
let existing = ctx
|
||||
.repos
|
||||
.goal
|
||||
let existing = goal
|
||||
.find_by_user_and_year(&user_id, cmd.year)
|
||||
.await?;
|
||||
if existing.is_some() {
|
||||
@@ -25,24 +26,21 @@ pub async fn execute(
|
||||
));
|
||||
}
|
||||
|
||||
let goal = Goal::new(
|
||||
let g = Goal::new(
|
||||
user_id.clone(),
|
||||
cmd.year,
|
||||
cmd.target_count,
|
||||
GoalType::Movies,
|
||||
)?;
|
||||
ctx.repos.goal.save(&goal).await?;
|
||||
goal.save(&g).await?;
|
||||
|
||||
let current_count = ctx
|
||||
.repos
|
||||
.goal
|
||||
let current_count = goal
|
||||
.count_reviews_in_year(&user_id, cmd.year)
|
||||
.await?;
|
||||
|
||||
ctx.services
|
||||
.event_publisher
|
||||
event_publisher
|
||||
.publish(&DomainEvent::GoalCreated {
|
||||
goal_id: goal.id().clone(),
|
||||
goal_id: g.id().clone(),
|
||||
user_id,
|
||||
year: cmd.year,
|
||||
target_count: cmd.target_count,
|
||||
@@ -50,7 +48,7 @@ pub async fn execute(
|
||||
.await?;
|
||||
|
||||
Ok(GoalWithProgress {
|
||||
goal,
|
||||
goal: g,
|
||||
current_count,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,24 +1,31 @@
|
||||
use domain::{errors::DomainError, events::DomainEvent, value_objects::UserId};
|
||||
use std::sync::Arc;
|
||||
|
||||
use domain::{
|
||||
errors::DomainError,
|
||||
events::DomainEvent,
|
||||
ports::{EventPublisher, GoalRepository},
|
||||
value_objects::UserId,
|
||||
};
|
||||
|
||||
use super::commands::DeleteGoalCommand;
|
||||
use crate::context::AppContext;
|
||||
|
||||
pub async fn execute(ctx: &AppContext, cmd: DeleteGoalCommand) -> Result<(), DomainError> {
|
||||
pub async fn execute(
|
||||
goal: Arc<dyn GoalRepository>,
|
||||
event_publisher: Arc<dyn EventPublisher>,
|
||||
cmd: DeleteGoalCommand,
|
||||
) -> Result<(), DomainError> {
|
||||
let user_id = UserId::from_uuid(cmd.user_id);
|
||||
|
||||
let goal = ctx
|
||||
.repos
|
||||
.goal
|
||||
let g = goal
|
||||
.find_by_user_and_year(&user_id, cmd.year)
|
||||
.await?
|
||||
.ok_or_else(|| DomainError::NotFound(format!("Goal for year {}", cmd.year)))?;
|
||||
|
||||
ctx.repos.goal.delete(goal.id(), &user_id).await?;
|
||||
goal.delete(g.id(), &user_id).await?;
|
||||
|
||||
ctx.services
|
||||
.event_publisher
|
||||
event_publisher
|
||||
.publish(&DomainEvent::GoalDeleted {
|
||||
goal_id: goal.id().clone(),
|
||||
goal_id: g.id().clone(),
|
||||
user_id,
|
||||
year: cmd.year,
|
||||
})
|
||||
|
||||
@@ -1,30 +1,27 @@
|
||||
use domain::{errors::DomainError, models::GoalWithProgress, value_objects::UserId};
|
||||
use std::sync::Arc;
|
||||
|
||||
use domain::{errors::DomainError, models::GoalWithProgress, ports::GoalRepository, value_objects::UserId};
|
||||
|
||||
use super::queries::GetGoalQuery;
|
||||
use crate::context::AppContext;
|
||||
|
||||
pub async fn execute(
|
||||
ctx: &AppContext,
|
||||
goal: Arc<dyn GoalRepository>,
|
||||
query: GetGoalQuery,
|
||||
) -> Result<Option<GoalWithProgress>, DomainError> {
|
||||
let user_id = UserId::from_uuid(query.user_id);
|
||||
|
||||
let goal = ctx
|
||||
.repos
|
||||
.goal
|
||||
let found = goal
|
||||
.find_by_user_and_year(&user_id, query.year)
|
||||
.await?;
|
||||
|
||||
let Some(goal) = goal else { return Ok(None) };
|
||||
let Some(g) = found else { return Ok(None) };
|
||||
|
||||
let current_count = ctx
|
||||
.repos
|
||||
.goal
|
||||
let current_count = goal
|
||||
.count_reviews_in_year(&user_id, query.year)
|
||||
.await?;
|
||||
|
||||
Ok(Some(GoalWithProgress {
|
||||
goal,
|
||||
goal: g,
|
||||
current_count,
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -1,24 +1,23 @@
|
||||
use domain::{errors::DomainError, models::GoalWithProgress, value_objects::UserId};
|
||||
use std::sync::Arc;
|
||||
|
||||
use domain::{errors::DomainError, models::GoalWithProgress, ports::GoalRepository, value_objects::UserId};
|
||||
|
||||
use super::queries::ListGoalsQuery;
|
||||
use crate::context::AppContext;
|
||||
|
||||
pub async fn execute(
|
||||
ctx: &AppContext,
|
||||
goal: Arc<dyn GoalRepository>,
|
||||
query: ListGoalsQuery,
|
||||
) -> Result<Vec<GoalWithProgress>, DomainError> {
|
||||
let user_id = UserId::from_uuid(query.user_id);
|
||||
let goals = ctx.repos.goal.list_for_user(&user_id).await?;
|
||||
let goals = goal.list_for_user(&user_id).await?;
|
||||
|
||||
let mut result = Vec::with_capacity(goals.len());
|
||||
for goal in goals {
|
||||
let current_count = ctx
|
||||
.repos
|
||||
.goal
|
||||
.count_reviews_in_year(&user_id, goal.year())
|
||||
for g in goals {
|
||||
let current_count = goal
|
||||
.count_reviews_in_year(&user_id, g.year())
|
||||
.await?;
|
||||
result.push(GoalWithProgress {
|
||||
goal,
|
||||
goal: g,
|
||||
current_count,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -10,15 +10,11 @@ use crate::test_helpers::TestContextBuilder;
|
||||
#[tokio::test]
|
||||
async fn creates_goal_and_returns_progress() {
|
||||
let goals = InMemoryGoalRepository::new();
|
||||
goals.set_review_count(Uuid::nil(), 2025, 5);
|
||||
let events = NoopEventPublisher::new();
|
||||
let ctx = TestContextBuilder::new()
|
||||
.with_goal(Arc::clone(&goals) as _)
|
||||
.with_event_publisher(Arc::clone(&events) as _)
|
||||
.build();
|
||||
|
||||
let result = create::execute(
|
||||
&ctx,
|
||||
Arc::clone(&goals) as _,
|
||||
Arc::clone(&events) as _,
|
||||
CreateGoalCommand {
|
||||
user_id: Uuid::nil(),
|
||||
year: 2025,
|
||||
@@ -30,19 +26,40 @@ async fn creates_goal_and_returns_progress() {
|
||||
|
||||
assert_eq!(result.goal.year(), 2025);
|
||||
assert_eq!(result.goal.target_count(), 50);
|
||||
assert_eq!(result.current_count, 0);
|
||||
assert_eq!(goals.count(), 1);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn creates_goal_with_review_count() {
|
||||
let goals = InMemoryGoalRepository::new();
|
||||
goals.set_review_count(Uuid::nil(), 2025, 5);
|
||||
let events = NoopEventPublisher::new();
|
||||
|
||||
let result = create::execute(
|
||||
Arc::clone(&goals) as _,
|
||||
Arc::clone(&events) as _,
|
||||
CreateGoalCommand {
|
||||
user_id: Uuid::nil(),
|
||||
year: 2025,
|
||||
target_count: 50,
|
||||
},
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result.current_count, 5);
|
||||
assert_eq!(goals.count(), 1);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn emits_goal_created_event() {
|
||||
let b = TestContextBuilder::new();
|
||||
let events = NoopEventPublisher::new();
|
||||
let ctx = TestContextBuilder::new()
|
||||
.with_event_publisher(Arc::clone(&events) as _)
|
||||
.build();
|
||||
|
||||
create::execute(
|
||||
&ctx,
|
||||
b.goal_repo.clone(),
|
||||
Arc::clone(&events) as _,
|
||||
CreateGoalCommand {
|
||||
user_id: Uuid::nil(),
|
||||
year: 2025,
|
||||
@@ -62,17 +79,20 @@ async fn emits_goal_created_event() {
|
||||
|
||||
#[tokio::test]
|
||||
async fn rejects_duplicate_year() {
|
||||
let ctx = TestContextBuilder::new().build();
|
||||
let b = TestContextBuilder::new();
|
||||
let cmd = CreateGoalCommand {
|
||||
user_id: Uuid::nil(),
|
||||
year: 2025,
|
||||
target_count: 10,
|
||||
};
|
||||
|
||||
create::execute(&ctx, cmd).await.unwrap();
|
||||
create::execute(b.goal_repo.clone(), b.event_publisher.clone(), cmd)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let result = create::execute(
|
||||
&ctx,
|
||||
b.goal_repo.clone(),
|
||||
b.event_publisher.clone(),
|
||||
CreateGoalCommand {
|
||||
user_id: Uuid::nil(),
|
||||
year: 2025,
|
||||
@@ -86,9 +106,10 @@ async fn rejects_duplicate_year() {
|
||||
|
||||
#[tokio::test]
|
||||
async fn rejects_year_before_2020() {
|
||||
let ctx = TestContextBuilder::new().build();
|
||||
let b = TestContextBuilder::new();
|
||||
let result = create::execute(
|
||||
&ctx,
|
||||
b.goal_repo.clone(),
|
||||
b.event_publisher.clone(),
|
||||
CreateGoalCommand {
|
||||
user_id: Uuid::nil(),
|
||||
year: 2019,
|
||||
@@ -102,9 +123,10 @@ async fn rejects_year_before_2020() {
|
||||
|
||||
#[tokio::test]
|
||||
async fn rejects_zero_target() {
|
||||
let ctx = TestContextBuilder::new().build();
|
||||
let b = TestContextBuilder::new();
|
||||
let result = create::execute(
|
||||
&ctx,
|
||||
b.goal_repo.clone(),
|
||||
b.event_publisher.clone(),
|
||||
CreateGoalCommand {
|
||||
user_id: Uuid::nil(),
|
||||
year: 2025,
|
||||
|
||||
@@ -13,13 +13,10 @@ use crate::test_helpers::TestContextBuilder;
|
||||
async fn deletes_existing_goal() {
|
||||
let goals = InMemoryGoalRepository::new();
|
||||
let events = NoopEventPublisher::new();
|
||||
let ctx = TestContextBuilder::new()
|
||||
.with_goal(Arc::clone(&goals) as _)
|
||||
.with_event_publisher(Arc::clone(&events) as _)
|
||||
.build();
|
||||
|
||||
create::execute(
|
||||
&ctx,
|
||||
Arc::clone(&goals) as _,
|
||||
Arc::clone(&events) as _,
|
||||
CreateGoalCommand {
|
||||
user_id: Uuid::nil(),
|
||||
year: 2025,
|
||||
@@ -31,7 +28,8 @@ async fn deletes_existing_goal() {
|
||||
assert_eq!(goals.count(), 1);
|
||||
|
||||
delete::execute(
|
||||
&ctx,
|
||||
Arc::clone(&goals) as _,
|
||||
Arc::clone(&events) as _,
|
||||
DeleteGoalCommand {
|
||||
user_id: Uuid::nil(),
|
||||
year: 2025,
|
||||
@@ -45,9 +43,10 @@ async fn deletes_existing_goal() {
|
||||
|
||||
#[tokio::test]
|
||||
async fn fails_when_not_found() {
|
||||
let ctx = TestContextBuilder::new().build();
|
||||
let b = TestContextBuilder::new();
|
||||
let result = delete::execute(
|
||||
&ctx,
|
||||
b.goal_repo.clone(),
|
||||
b.event_publisher.clone(),
|
||||
DeleteGoalCommand {
|
||||
user_id: Uuid::nil(),
|
||||
year: 2025,
|
||||
|
||||
@@ -5,9 +5,10 @@ use crate::test_helpers::TestContextBuilder;
|
||||
|
||||
#[tokio::test]
|
||||
async fn returns_goal_when_exists() {
|
||||
let ctx = TestContextBuilder::new().build();
|
||||
let b = TestContextBuilder::new();
|
||||
create::execute(
|
||||
&ctx,
|
||||
b.goal_repo.clone(),
|
||||
b.event_publisher.clone(),
|
||||
CreateGoalCommand {
|
||||
user_id: Uuid::nil(),
|
||||
year: 2025,
|
||||
@@ -18,7 +19,7 @@ async fn returns_goal_when_exists() {
|
||||
.unwrap();
|
||||
|
||||
let result = get::execute(
|
||||
&ctx,
|
||||
b.goal_repo.clone(),
|
||||
GetGoalQuery {
|
||||
user_id: Uuid::nil(),
|
||||
year: 2025,
|
||||
@@ -33,9 +34,9 @@ async fn returns_goal_when_exists() {
|
||||
|
||||
#[tokio::test]
|
||||
async fn returns_none_when_missing() {
|
||||
let ctx = TestContextBuilder::new().build();
|
||||
let b = TestContextBuilder::new();
|
||||
let result = get::execute(
|
||||
&ctx,
|
||||
b.goal_repo.clone(),
|
||||
GetGoalQuery {
|
||||
user_id: Uuid::nil(),
|
||||
year: 2025,
|
||||
|
||||
@@ -5,9 +5,9 @@ use crate::test_helpers::TestContextBuilder;
|
||||
|
||||
#[tokio::test]
|
||||
async fn returns_empty_when_no_goals() {
|
||||
let ctx = TestContextBuilder::new().build();
|
||||
let b = TestContextBuilder::new();
|
||||
let result = list::execute(
|
||||
&ctx,
|
||||
b.goal_repo.clone(),
|
||||
ListGoalsQuery {
|
||||
user_id: Uuid::nil(),
|
||||
},
|
||||
@@ -20,10 +20,11 @@ async fn returns_empty_when_no_goals() {
|
||||
|
||||
#[tokio::test]
|
||||
async fn returns_all_goals_for_user() {
|
||||
let ctx = TestContextBuilder::new().build();
|
||||
let b = TestContextBuilder::new();
|
||||
for year in [2023, 2024, 2025] {
|
||||
create::execute(
|
||||
&ctx,
|
||||
b.goal_repo.clone(),
|
||||
b.event_publisher.clone(),
|
||||
CreateGoalCommand {
|
||||
user_id: Uuid::nil(),
|
||||
year,
|
||||
@@ -35,7 +36,7 @@ async fn returns_all_goals_for_user() {
|
||||
}
|
||||
|
||||
let result = list::execute(
|
||||
&ctx,
|
||||
b.goal_repo.clone(),
|
||||
ListGoalsQuery {
|
||||
user_id: Uuid::nil(),
|
||||
},
|
||||
|
||||
@@ -8,9 +8,10 @@ use crate::test_helpers::TestContextBuilder;
|
||||
|
||||
#[tokio::test]
|
||||
async fn updates_target_count() {
|
||||
let ctx = TestContextBuilder::new().build();
|
||||
let b = TestContextBuilder::new();
|
||||
create::execute(
|
||||
&ctx,
|
||||
b.goal_repo.clone(),
|
||||
b.event_publisher.clone(),
|
||||
CreateGoalCommand {
|
||||
user_id: Uuid::nil(),
|
||||
year: 2025,
|
||||
@@ -21,7 +22,8 @@ async fn updates_target_count() {
|
||||
.unwrap();
|
||||
|
||||
let result = update::execute(
|
||||
&ctx,
|
||||
b.goal_repo.clone(),
|
||||
b.event_publisher.clone(),
|
||||
UpdateGoalCommand {
|
||||
user_id: Uuid::nil(),
|
||||
year: 2025,
|
||||
@@ -36,9 +38,10 @@ async fn updates_target_count() {
|
||||
|
||||
#[tokio::test]
|
||||
async fn fails_when_goal_not_found() {
|
||||
let ctx = TestContextBuilder::new().build();
|
||||
let b = TestContextBuilder::new();
|
||||
let result = update::execute(
|
||||
&ctx,
|
||||
b.goal_repo.clone(),
|
||||
b.event_publisher.clone(),
|
||||
UpdateGoalCommand {
|
||||
user_id: Uuid::nil(),
|
||||
year: 2025,
|
||||
@@ -52,9 +55,10 @@ async fn fails_when_goal_not_found() {
|
||||
|
||||
#[tokio::test]
|
||||
async fn rejects_zero_target() {
|
||||
let ctx = TestContextBuilder::new().build();
|
||||
let b = TestContextBuilder::new();
|
||||
create::execute(
|
||||
&ctx,
|
||||
b.goal_repo.clone(),
|
||||
b.event_publisher.clone(),
|
||||
CreateGoalCommand {
|
||||
user_id: Uuid::nil(),
|
||||
year: 2025,
|
||||
@@ -65,7 +69,8 @@ async fn rejects_zero_target() {
|
||||
.unwrap();
|
||||
|
||||
let result = update::execute(
|
||||
&ctx,
|
||||
b.goal_repo.clone(),
|
||||
b.event_publisher.clone(),
|
||||
UpdateGoalCommand {
|
||||
user_id: Uuid::nil(),
|
||||
year: 2025,
|
||||
|
||||
@@ -1,36 +1,37 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use domain::{
|
||||
errors::DomainError, events::DomainEvent, models::GoalWithProgress, value_objects::UserId,
|
||||
errors::DomainError,
|
||||
events::DomainEvent,
|
||||
models::GoalWithProgress,
|
||||
ports::{EventPublisher, GoalRepository},
|
||||
value_objects::UserId,
|
||||
};
|
||||
|
||||
use super::commands::UpdateGoalCommand;
|
||||
use crate::context::AppContext;
|
||||
|
||||
pub async fn execute(
|
||||
ctx: &AppContext,
|
||||
goal: Arc<dyn GoalRepository>,
|
||||
event_publisher: Arc<dyn EventPublisher>,
|
||||
cmd: UpdateGoalCommand,
|
||||
) -> Result<GoalWithProgress, DomainError> {
|
||||
let user_id = UserId::from_uuid(cmd.user_id);
|
||||
|
||||
let mut goal = ctx
|
||||
.repos
|
||||
.goal
|
||||
let mut g = goal
|
||||
.find_by_user_and_year(&user_id, cmd.year)
|
||||
.await?
|
||||
.ok_or_else(|| DomainError::NotFound(format!("Goal for year {}", cmd.year)))?;
|
||||
|
||||
goal.update_target(cmd.target_count)?;
|
||||
ctx.repos.goal.update(&goal).await?;
|
||||
g.update_target(cmd.target_count)?;
|
||||
goal.update(&g).await?;
|
||||
|
||||
let current_count = ctx
|
||||
.repos
|
||||
.goal
|
||||
let current_count = goal
|
||||
.count_reviews_in_year(&user_id, cmd.year)
|
||||
.await?;
|
||||
|
||||
ctx.services
|
||||
.event_publisher
|
||||
event_publisher
|
||||
.publish(&DomainEvent::GoalUpdated {
|
||||
goal_id: goal.id().clone(),
|
||||
goal_id: g.id().clone(),
|
||||
user_id,
|
||||
year: cmd.year,
|
||||
target_count: cmd.target_count,
|
||||
@@ -38,7 +39,7 @@ pub async fn execute(
|
||||
.await?;
|
||||
|
||||
Ok(GoalWithProgress {
|
||||
goal,
|
||||
goal: g,
|
||||
current_count,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user