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