refactor(goals): scoped Arc deps instead of AppContext

This commit is contained in:
2026-06-11 21:36:11 +02:00
parent 2b295e10ba
commit f006ba00a8
12 changed files with 143 additions and 110 deletions

View File

@@ -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,
})
}

View File

@@ -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,
})

View File

@@ -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,
}))
}

View File

@@ -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,
});
}

View File

@@ -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,

View File

@@ -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,

View File

@@ -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,

View File

@@ -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(),
},

View File

@@ -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,

View File

@@ -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,
})
}

View File

@@ -39,7 +39,7 @@ pub async fn list_goals(
user: AuthenticatedUser,
) -> Result<Json<GoalsResponse>, ApiError> {
let goals = application::goals::list::execute(
&state.app_ctx,
state.app_ctx.repos.goal.clone(),
application::goals::queries::ListGoalsQuery {
user_id: user.0.value(),
},
@@ -65,7 +65,8 @@ pub async fn create_goal(
Json(req): Json<CreateGoalRequest>,
) -> Result<Json<GoalDto>, ApiError> {
let g = application::goals::create::execute(
&state.app_ctx,
state.app_ctx.repos.goal.clone(),
state.app_ctx.services.event_publisher.clone(),
application::goals::commands::CreateGoalCommand {
user_id: user.0.value(),
year: req.year,
@@ -93,7 +94,8 @@ pub async fn update_goal(
Json(req): Json<UpdateGoalRequest>,
) -> Result<Json<GoalDto>, ApiError> {
let g = application::goals::update::execute(
&state.app_ctx,
state.app_ctx.repos.goal.clone(),
state.app_ctx.services.event_publisher.clone(),
application::goals::commands::UpdateGoalCommand {
user_id: user.0.value(),
year,
@@ -119,7 +121,8 @@ pub async fn delete_goal(
Path(year): Path<u16>,
) -> Result<StatusCode, ApiError> {
application::goals::delete::execute(
&state.app_ctx,
state.app_ctx.repos.goal.clone(),
state.app_ctx.services.event_publisher.clone(),
application::goals::commands::DeleteGoalCommand {
user_id: user.0.value(),
year,
@@ -143,7 +146,7 @@ pub async fn get_user_goals(
Path(user_id): Path<Uuid>,
) -> Result<Json<GoalsResponse>, ApiError> {
let goals = application::goals::list::execute(
&state.app_ctx,
state.app_ctx.repos.goal.clone(),
application::goals::queries::ListGoalsQuery { user_id },
)
.await?;

View File

@@ -351,7 +351,7 @@ pub async fn get_user_profile(
trends,
goals: {
let goals_list = application::goals::list::execute(
&state.app_ctx,
state.app_ctx.repos.goal.clone(),
application::goals::queries::ListGoalsQuery { user_id },
)
.await
@@ -634,7 +634,7 @@ pub async fn get_user_profile_html(
search: params.search.clone(),
goals: {
let goals_list = application::goals::list::execute(
&state.app_ctx,
state.app_ctx.repos.goal.clone(),
application::goals::queries::ListGoalsQuery {
user_id: profile_user_uuid,
},