refactor: remaining MEDIUM — CQRS splits, DI Deps, profile dedup, event Value, response enum

M1: MovieRepository→MovieCommand/MovieQuery, WatchEventRepository→
WatchEventCommand/WatchEventQuery
M2: goals/ and import/ use Deps structs
M7: extract upload_image helper in update_profile
M8: FederationDeliveryRequested activity_json String→serde_json::Value
M11: UserProfileResponse uses ProfileViewData enum
This commit is contained in:
2026-07-10 03:50:43 +02:00
parent 12da356a40
commit dee013c7eb
99 changed files with 1262 additions and 896 deletions

View File

@@ -1,24 +1,19 @@
use std::sync::Arc;
use domain::{
errors::DomainError,
events::DomainEvent,
models::{Goal, GoalType, GoalWithProgress},
ports::{EventPublisher, GoalRepository, StatsRepository},
value_objects::UserId,
};
use super::commands::CreateGoalCommand;
use super::{commands::CreateGoalCommand, deps::GoalCommandDeps};
pub async fn execute(
goal: Arc<dyn GoalRepository>,
stats: Arc<dyn StatsRepository>,
event_publisher: Arc<dyn EventPublisher>,
deps: &GoalCommandDeps,
cmd: CreateGoalCommand,
) -> Result<GoalWithProgress, DomainError> {
let user_id = UserId::from_uuid(cmd.user_id);
let existing = goal.find_by_user_and_year(&user_id, cmd.year).await?;
let existing = deps.goal.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(),
@@ -31,11 +26,11 @@ pub async fn execute(
cmd.target_count,
GoalType::Movies,
)?;
goal.save(&g).await?;
deps.goal.save(&g).await?;
let current_count = stats.count_reviews_in_year(&user_id, cmd.year).await?;
let current_count = deps.stats.count_reviews_in_year(&user_id, cmd.year).await?;
event_publisher
deps.event_publisher
.publish(&DomainEvent::GoalCreated {
goal_id: g.id().clone(),
user_id,

View File

@@ -1,29 +1,26 @@
use std::sync::Arc;
use domain::{
errors::DomainError,
events::DomainEvent,
ports::{EventPublisher, GoalRepository},
value_objects::UserId,
};
use super::commands::DeleteGoalCommand;
use super::{commands::DeleteGoalCommand, deps::GoalCommandDeps};
pub async fn execute(
goal: Arc<dyn GoalRepository>,
event_publisher: Arc<dyn EventPublisher>,
deps: &GoalCommandDeps,
cmd: DeleteGoalCommand,
) -> Result<(), DomainError> {
let user_id = UserId::from_uuid(cmd.user_id);
let g = goal
let g = deps
.goal
.find_by_user_and_year(&user_id, cmd.year)
.await?
.ok_or_else(|| DomainError::NotFound(format!("Goal for year {}", cmd.year)))?;
goal.delete(g.id(), &user_id).await?;
deps.goal.delete(g.id(), &user_id).await?;
event_publisher
deps.event_publisher
.publish(&DomainEvent::GoalDeleted {
goal_id: g.id().clone(),
user_id,

View File

@@ -0,0 +1,14 @@
use std::sync::Arc;
use domain::ports::{EventPublisher, GoalRepository, StatsRepository};
pub struct GoalCommandDeps {
pub goal: Arc<dyn GoalRepository>,
pub stats: Arc<dyn StatsRepository>,
pub event_publisher: Arc<dyn EventPublisher>,
}
pub struct GoalQueryDeps {
pub goal: Arc<dyn GoalRepository>,
pub stats: Arc<dyn StatsRepository>,
}

View File

@@ -1,26 +1,22 @@
use std::sync::Arc;
use domain::{
errors::DomainError,
models::GoalWithProgress,
ports::{GoalRepository, StatsRepository},
value_objects::UserId,
};
use super::queries::GetGoalQuery;
use super::{deps::GoalQueryDeps, queries::GetGoalQuery};
pub async fn execute(
goal: Arc<dyn GoalRepository>,
stats: Arc<dyn StatsRepository>,
deps: &GoalQueryDeps,
query: GetGoalQuery,
) -> Result<Option<GoalWithProgress>, DomainError> {
let user_id = UserId::from_uuid(query.user_id);
let found = goal.find_by_user_and_year(&user_id, query.year).await?;
let found = deps.goal.find_by_user_and_year(&user_id, query.year).await?;
let Some(g) = found else { return Ok(None) };
let current_count = stats.count_reviews_in_year(&user_id, query.year).await?;
let current_count = deps.stats.count_reviews_in_year(&user_id, query.year).await?;
Ok(Some(GoalWithProgress {
goal: g,

View File

@@ -1,25 +1,21 @@
use std::sync::Arc;
use domain::{
errors::DomainError,
models::GoalWithProgress,
ports::{GoalRepository, StatsRepository},
value_objects::UserId,
};
use super::queries::ListGoalsQuery;
use super::{deps::GoalQueryDeps, queries::ListGoalsQuery};
pub async fn execute(
goal: Arc<dyn GoalRepository>,
stats: Arc<dyn StatsRepository>,
deps: &GoalQueryDeps,
query: ListGoalsQuery,
) -> Result<Vec<GoalWithProgress>, DomainError> {
let user_id = UserId::from_uuid(query.user_id);
let goals = goal.list_for_user(&user_id).await?;
let goals = deps.goal.list_for_user(&user_id).await?;
let mut result = Vec::with_capacity(goals.len());
for g in goals {
let current_count = stats.count_reviews_in_year(&user_id, g.year()).await?;
let current_count = deps.stats.count_reviews_in_year(&user_id, g.year()).await?;
result.push(GoalWithProgress {
goal: g,
current_count,

View File

@@ -1,6 +1,7 @@
pub mod commands;
pub mod create;
pub mod delete;
pub mod deps;
pub mod get;
pub mod list;
pub mod queries;

View File

@@ -4,6 +4,7 @@ use domain::events::DomainEvent;
use domain::testing::{FakeStatsRepository, InMemoryGoalRepository, NoopEventPublisher};
use uuid::Uuid;
use crate::goals::deps::GoalCommandDeps;
use crate::goals::{commands::CreateGoalCommand, create};
use crate::test_helpers::TestContextBuilder;
@@ -12,11 +13,14 @@ async fn creates_goal_and_returns_progress() {
let goals = InMemoryGoalRepository::new();
let stats = FakeStatsRepository::new();
let events = NoopEventPublisher::new();
let deps = GoalCommandDeps {
goal: Arc::clone(&goals) as _,
stats: Arc::clone(&stats) as _,
event_publisher: Arc::clone(&events) as _,
};
let result = create::execute(
Arc::clone(&goals) as _,
Arc::clone(&stats) as _,
Arc::clone(&events) as _,
&deps,
CreateGoalCommand {
user_id: Uuid::nil(),
year: 2025,
@@ -38,11 +42,14 @@ async fn creates_goal_with_review_count() {
let stats = FakeStatsRepository::new();
stats.set_review_count(Uuid::nil(), 2025, 5);
let events = NoopEventPublisher::new();
let deps = GoalCommandDeps {
goal: Arc::clone(&goals) as _,
stats: Arc::clone(&stats) as _,
event_publisher: Arc::clone(&events) as _,
};
let result = create::execute(
Arc::clone(&goals) as _,
Arc::clone(&stats) as _,
Arc::clone(&events) as _,
&deps,
CreateGoalCommand {
user_id: Uuid::nil(),
year: 2025,
@@ -60,11 +67,14 @@ async fn creates_goal_with_review_count() {
async fn emits_goal_created_event() {
let b = TestContextBuilder::new();
let events = NoopEventPublisher::new();
let deps = GoalCommandDeps {
goal: b.goal_repo.clone(),
stats: b.stats_repo.clone(),
event_publisher: Arc::clone(&events) as _,
};
create::execute(
b.goal_repo.clone(),
b.stats_repo.clone(),
Arc::clone(&events) as _,
&deps,
CreateGoalCommand {
user_id: Uuid::nil(),
year: 2025,
@@ -85,25 +95,21 @@ async fn emits_goal_created_event() {
#[tokio::test]
async fn rejects_duplicate_year() {
let b = TestContextBuilder::new();
let deps = GoalCommandDeps {
goal: b.goal_repo.clone(),
stats: b.stats_repo.clone(),
event_publisher: b.event_publisher.clone(),
};
let cmd = CreateGoalCommand {
user_id: Uuid::nil(),
year: 2025,
target_count: 10,
};
create::execute(
b.goal_repo.clone(),
b.stats_repo.clone(),
b.event_publisher.clone(),
cmd,
)
.await
.unwrap();
create::execute(&deps, cmd).await.unwrap();
let result = create::execute(
b.goal_repo.clone(),
b.stats_repo.clone(),
b.event_publisher.clone(),
&deps,
CreateGoalCommand {
user_id: Uuid::nil(),
year: 2025,
@@ -118,10 +124,13 @@ async fn rejects_duplicate_year() {
#[tokio::test]
async fn rejects_year_before_2020() {
let b = TestContextBuilder::new();
let deps = GoalCommandDeps {
goal: b.goal_repo.clone(),
stats: b.stats_repo.clone(),
event_publisher: b.event_publisher.clone(),
};
let result = create::execute(
b.goal_repo.clone(),
b.stats_repo.clone(),
b.event_publisher.clone(),
&deps,
CreateGoalCommand {
user_id: Uuid::nil(),
year: 2019,
@@ -136,10 +145,13 @@ async fn rejects_year_before_2020() {
#[tokio::test]
async fn rejects_zero_target() {
let b = TestContextBuilder::new();
let deps = GoalCommandDeps {
goal: b.goal_repo.clone(),
stats: b.stats_repo.clone(),
event_publisher: b.event_publisher.clone(),
};
let result = create::execute(
b.goal_repo.clone(),
b.stats_repo.clone(),
b.event_publisher.clone(),
&deps,
CreateGoalCommand {
user_id: Uuid::nil(),
year: 2025,

View File

@@ -3,6 +3,7 @@ use std::sync::Arc;
use domain::testing::{FakeStatsRepository, InMemoryGoalRepository, NoopEventPublisher};
use uuid::Uuid;
use crate::goals::deps::GoalCommandDeps;
use crate::goals::{
commands::{CreateGoalCommand, DeleteGoalCommand},
create, delete,
@@ -14,11 +15,14 @@ async fn deletes_existing_goal() {
let goals = InMemoryGoalRepository::new();
let stats = FakeStatsRepository::new();
let events = NoopEventPublisher::new();
let deps = GoalCommandDeps {
goal: Arc::clone(&goals) as _,
stats: Arc::clone(&stats) as _,
event_publisher: Arc::clone(&events) as _,
};
create::execute(
Arc::clone(&goals) as _,
Arc::clone(&stats) as _,
Arc::clone(&events) as _,
&deps,
CreateGoalCommand {
user_id: Uuid::nil(),
year: 2025,
@@ -30,8 +34,7 @@ async fn deletes_existing_goal() {
assert_eq!(goals.count(), 1);
delete::execute(
Arc::clone(&goals) as _,
Arc::clone(&events) as _,
&deps,
DeleteGoalCommand {
user_id: Uuid::nil(),
year: 2025,
@@ -46,9 +49,13 @@ async fn deletes_existing_goal() {
#[tokio::test]
async fn fails_when_not_found() {
let b = TestContextBuilder::new();
let deps = GoalCommandDeps {
goal: b.goal_repo.clone(),
stats: b.stats_repo.clone(),
event_publisher: b.event_publisher.clone(),
};
let result = delete::execute(
b.goal_repo.clone(),
b.event_publisher.clone(),
&deps,
DeleteGoalCommand {
user_id: Uuid::nil(),
year: 2025,

View File

@@ -1,15 +1,24 @@
use uuid::Uuid;
use crate::goals::deps::{GoalCommandDeps, GoalQueryDeps};
use crate::goals::{commands::CreateGoalCommand, create, get, queries::GetGoalQuery};
use crate::test_helpers::TestContextBuilder;
#[tokio::test]
async fn returns_goal_when_exists() {
let b = TestContextBuilder::new();
let cmd_deps = GoalCommandDeps {
goal: b.goal_repo.clone(),
stats: b.stats_repo.clone(),
event_publisher: b.event_publisher.clone(),
};
let query_deps = GoalQueryDeps {
goal: b.goal_repo.clone(),
stats: b.stats_repo.clone(),
};
create::execute(
b.goal_repo.clone(),
b.stats_repo.clone(),
b.event_publisher.clone(),
&cmd_deps,
CreateGoalCommand {
user_id: Uuid::nil(),
year: 2025,
@@ -20,8 +29,7 @@ async fn returns_goal_when_exists() {
.unwrap();
let result = get::execute(
b.goal_repo.clone(),
b.stats_repo.clone(),
&query_deps,
GetGoalQuery {
user_id: Uuid::nil(),
year: 2025,
@@ -37,9 +45,12 @@ async fn returns_goal_when_exists() {
#[tokio::test]
async fn returns_none_when_missing() {
let b = TestContextBuilder::new();
let query_deps = GoalQueryDeps {
goal: b.goal_repo.clone(),
stats: b.stats_repo.clone(),
};
let result = get::execute(
b.goal_repo.clone(),
b.stats_repo.clone(),
&query_deps,
GetGoalQuery {
user_id: Uuid::nil(),
year: 2025,

View File

@@ -1,14 +1,18 @@
use uuid::Uuid;
use crate::goals::deps::{GoalCommandDeps, GoalQueryDeps};
use crate::goals::{commands::CreateGoalCommand, create, list, queries::ListGoalsQuery};
use crate::test_helpers::TestContextBuilder;
#[tokio::test]
async fn returns_empty_when_no_goals() {
let b = TestContextBuilder::new();
let query_deps = GoalQueryDeps {
goal: b.goal_repo.clone(),
stats: b.stats_repo.clone(),
};
let result = list::execute(
b.goal_repo.clone(),
b.stats_repo.clone(),
&query_deps,
ListGoalsQuery {
user_id: Uuid::nil(),
},
@@ -22,11 +26,19 @@ async fn returns_empty_when_no_goals() {
#[tokio::test]
async fn returns_all_goals_for_user() {
let b = TestContextBuilder::new();
let cmd_deps = GoalCommandDeps {
goal: b.goal_repo.clone(),
stats: b.stats_repo.clone(),
event_publisher: b.event_publisher.clone(),
};
let query_deps = GoalQueryDeps {
goal: b.goal_repo.clone(),
stats: b.stats_repo.clone(),
};
for year in [2023, 2024, 2025] {
create::execute(
b.goal_repo.clone(),
b.stats_repo.clone(),
b.event_publisher.clone(),
&cmd_deps,
CreateGoalCommand {
user_id: Uuid::nil(),
year,
@@ -38,8 +50,7 @@ async fn returns_all_goals_for_user() {
}
let result = list::execute(
b.goal_repo.clone(),
b.stats_repo.clone(),
&query_deps,
ListGoalsQuery {
user_id: Uuid::nil(),
},

View File

@@ -1,5 +1,6 @@
use uuid::Uuid;
use crate::goals::deps::GoalCommandDeps;
use crate::goals::{
commands::{CreateGoalCommand, UpdateGoalCommand},
create, update,
@@ -9,10 +10,14 @@ use crate::test_helpers::TestContextBuilder;
#[tokio::test]
async fn updates_target_count() {
let b = TestContextBuilder::new();
let deps = GoalCommandDeps {
goal: b.goal_repo.clone(),
stats: b.stats_repo.clone(),
event_publisher: b.event_publisher.clone(),
};
create::execute(
b.goal_repo.clone(),
b.stats_repo.clone(),
b.event_publisher.clone(),
&deps,
CreateGoalCommand {
user_id: Uuid::nil(),
year: 2025,
@@ -23,9 +28,7 @@ async fn updates_target_count() {
.unwrap();
let result = update::execute(
b.goal_repo.clone(),
b.stats_repo.clone(),
b.event_publisher.clone(),
&deps,
UpdateGoalCommand {
user_id: Uuid::nil(),
year: 2025,
@@ -41,10 +44,13 @@ async fn updates_target_count() {
#[tokio::test]
async fn fails_when_goal_not_found() {
let b = TestContextBuilder::new();
let deps = GoalCommandDeps {
goal: b.goal_repo.clone(),
stats: b.stats_repo.clone(),
event_publisher: b.event_publisher.clone(),
};
let result = update::execute(
b.goal_repo.clone(),
b.stats_repo.clone(),
b.event_publisher.clone(),
&deps,
UpdateGoalCommand {
user_id: Uuid::nil(),
year: 2025,
@@ -59,10 +65,14 @@ async fn fails_when_goal_not_found() {
#[tokio::test]
async fn rejects_zero_target() {
let b = TestContextBuilder::new();
let deps = GoalCommandDeps {
goal: b.goal_repo.clone(),
stats: b.stats_repo.clone(),
event_publisher: b.event_publisher.clone(),
};
create::execute(
b.goal_repo.clone(),
b.stats_repo.clone(),
b.event_publisher.clone(),
&deps,
CreateGoalCommand {
user_id: Uuid::nil(),
year: 2025,
@@ -73,9 +83,7 @@ async fn rejects_zero_target() {
.unwrap();
let result = update::execute(
b.goal_repo.clone(),
b.stats_repo.clone(),
b.event_publisher.clone(),
&deps,
UpdateGoalCommand {
user_id: Uuid::nil(),
year: 2025,

View File

@@ -1,34 +1,30 @@
use std::sync::Arc;
use domain::{
errors::DomainError,
events::DomainEvent,
models::GoalWithProgress,
ports::{EventPublisher, GoalRepository, StatsRepository},
value_objects::UserId,
};
use super::commands::UpdateGoalCommand;
use super::{commands::UpdateGoalCommand, deps::GoalCommandDeps};
pub async fn execute(
goal: Arc<dyn GoalRepository>,
stats: Arc<dyn StatsRepository>,
event_publisher: Arc<dyn EventPublisher>,
deps: &GoalCommandDeps,
cmd: UpdateGoalCommand,
) -> Result<GoalWithProgress, DomainError> {
let user_id = UserId::from_uuid(cmd.user_id);
let mut g = goal
let mut g = deps
.goal
.find_by_user_and_year(&user_id, cmd.year)
.await?
.ok_or_else(|| DomainError::NotFound(format!("Goal for year {}", cmd.year)))?;
g.update_target(cmd.target_count)?;
goal.update(&g).await?;
deps.goal.update(&g).await?;
let current_count = stats.count_reviews_in_year(&user_id, cmd.year).await?;
let current_count = deps.stats.count_reviews_in_year(&user_id, cmd.year).await?;
event_publisher
deps.event_publisher
.publish(&DomainEvent::GoalUpdated {
goal_id: g.id().clone(),
user_id,