refactor: group use cases into DDD bounded contexts
Flat use_cases/ (44 files) + monolithic commands.rs/queries.rs split into diary/, movies/, watchlist/, import/, auth/, users/, integrations/, search/, person/, federation/ — each with own commands.rs, queries.rs, and use case modules. Inline tests extracted to sibling tests/ dirs.
This commit is contained in:
69
crates/application/src/watchlist/add.rs
Normal file
69
crates/application/src/watchlist/add.rs
Normal file
@@ -0,0 +1,69 @@
|
||||
use domain::{
|
||||
errors::DomainError,
|
||||
events::DomainEvent,
|
||||
models::WatchlistEntry,
|
||||
value_objects::{MovieId, UserId},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
context::AppContext,
|
||||
diary::movie_resolver::{MovieResolver, MovieResolverDeps},
|
||||
watchlist::commands::AddToWatchlistCommand,
|
||||
};
|
||||
|
||||
pub async fn execute(ctx: &AppContext, cmd: AddToWatchlistCommand) -> Result<(), DomainError> {
|
||||
let user_id = UserId::from_uuid(cmd.user_id);
|
||||
|
||||
let movie = if let Some(id) = cmd.input.movie_id {
|
||||
let movie_id = MovieId::from_uuid(id);
|
||||
ctx.repos
|
||||
.movie
|
||||
.get_movie_by_id(&movie_id)
|
||||
.await?
|
||||
.ok_or_else(|| DomainError::NotFound(format!("Movie {id}")))?
|
||||
} else {
|
||||
let deps = MovieResolverDeps {
|
||||
repository: ctx.repos.movie.as_ref(),
|
||||
metadata_client: ctx.services.metadata.as_ref(),
|
||||
};
|
||||
let (movie, is_new) = MovieResolver::default_pipeline()
|
||||
.resolve(&cmd.input, &deps)
|
||||
.await?;
|
||||
if is_new {
|
||||
ctx.repos.movie.upsert_movie(&movie).await?;
|
||||
if let Some(ext_id) = movie.external_metadata_id() {
|
||||
let _ = ctx
|
||||
.services
|
||||
.event_publisher
|
||||
.publish(&DomainEvent::MovieDiscovered {
|
||||
movie_id: movie.id().clone(),
|
||||
external_metadata_id: ext_id.clone(),
|
||||
})
|
||||
.await;
|
||||
}
|
||||
}
|
||||
movie
|
||||
};
|
||||
|
||||
let entry = WatchlistEntry::new(user_id.clone(), movie.id().clone());
|
||||
ctx.repos.watchlist.add(&entry).await?;
|
||||
|
||||
let _ = ctx
|
||||
.services
|
||||
.event_publisher
|
||||
.publish(&DomainEvent::WatchlistEntryAdded {
|
||||
user_id,
|
||||
movie_id: movie.id().clone(),
|
||||
movie_title: movie.title().value().to_string(),
|
||||
release_year: movie.release_year().value(),
|
||||
external_metadata_id: movie.external_metadata_id().map(|e| e.value().to_string()),
|
||||
added_at: entry.added_at,
|
||||
})
|
||||
.await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "tests/add.rs"]
|
||||
mod tests;
|
||||
13
crates/application/src/watchlist/commands.rs
Normal file
13
crates/application/src/watchlist/commands.rs
Normal file
@@ -0,0 +1,13 @@
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::diary::commands::MovieInput;
|
||||
|
||||
pub struct AddToWatchlistCommand {
|
||||
pub user_id: Uuid,
|
||||
pub input: MovieInput,
|
||||
}
|
||||
|
||||
pub struct RemoveFromWatchlistCommand {
|
||||
pub user_id: Uuid,
|
||||
pub movie_id: Uuid,
|
||||
}
|
||||
19
crates/application/src/watchlist/get.rs
Normal file
19
crates/application/src/watchlist/get.rs
Normal file
@@ -0,0 +1,19 @@
|
||||
use domain::{
|
||||
errors::DomainError,
|
||||
models::{
|
||||
WatchlistWithMovie,
|
||||
collections::{PageParams, Paginated},
|
||||
},
|
||||
value_objects::UserId,
|
||||
};
|
||||
|
||||
use crate::{context::AppContext, watchlist::queries::GetWatchlistQuery};
|
||||
|
||||
pub async fn execute(
|
||||
ctx: &AppContext,
|
||||
query: GetWatchlistQuery,
|
||||
) -> Result<Paginated<WatchlistWithMovie>, DomainError> {
|
||||
let user_id = UserId::from_uuid(query.user_id);
|
||||
let page = PageParams::new(query.limit, query.offset)?;
|
||||
ctx.repos.watchlist.get_for_user(&user_id, &page).await
|
||||
}
|
||||
97
crates/application/src/watchlist/get_page.rs
Normal file
97
crates/application/src/watchlist/get_page.rs
Normal file
@@ -0,0 +1,97 @@
|
||||
use domain::{errors::DomainError, value_objects::UserId};
|
||||
|
||||
use crate::{
|
||||
context::AppContext, ports::WatchlistDisplayEntry, watchlist::queries::GetWatchlistQuery,
|
||||
};
|
||||
|
||||
pub struct WatchlistPageResult {
|
||||
pub display_entries: Vec<WatchlistDisplayEntry>,
|
||||
pub has_more: bool,
|
||||
pub current_offset: u32,
|
||||
pub limit: u32,
|
||||
}
|
||||
|
||||
pub async fn execute(
|
||||
ctx: &AppContext,
|
||||
query: GetWatchlistQuery,
|
||||
is_owner: bool,
|
||||
) -> Result<WatchlistPageResult, DomainError> {
|
||||
let user_id = UserId::from_uuid(query.user_id);
|
||||
let is_local = ctx.repos.user.find_by_id(&user_id).await?.is_some();
|
||||
|
||||
if is_local {
|
||||
let page = crate::watchlist::get::execute(ctx, query).await?;
|
||||
let has_more = page.offset + page.limit < page.total_count as u32;
|
||||
let display_entries = page
|
||||
.items
|
||||
.iter()
|
||||
.map(|w| {
|
||||
let remove_url = if is_owner {
|
||||
Some(format!("/watchlist/{}/remove", w.movie.id().value()))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
WatchlistDisplayEntry {
|
||||
poster_url: w
|
||||
.movie
|
||||
.poster_path()
|
||||
.map(|p| format!("/images/{}", p.value())),
|
||||
movie_title: w.movie.title().value().to_string(),
|
||||
release_year: w.movie.release_year().value(),
|
||||
movie_url: Some(format!("/movies/{}", w.movie.id().value())),
|
||||
added_at: w.entry.added_at.format("%b %-d, %Y").to_string(),
|
||||
remove_url,
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
Ok(WatchlistPageResult {
|
||||
display_entries,
|
||||
has_more,
|
||||
current_offset: page.offset,
|
||||
limit: page.limit,
|
||||
})
|
||||
} else {
|
||||
load_remote_watchlist(ctx, query.user_id).await
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "federation"))]
|
||||
async fn load_remote_watchlist(
|
||||
_ctx: &AppContext,
|
||||
_user_id: uuid::Uuid,
|
||||
) -> Result<WatchlistPageResult, DomainError> {
|
||||
Ok(WatchlistPageResult {
|
||||
display_entries: vec![],
|
||||
has_more: false,
|
||||
current_offset: 0,
|
||||
limit: 0,
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(feature = "federation")]
|
||||
async fn load_remote_watchlist(
|
||||
ctx: &AppContext,
|
||||
user_id: uuid::Uuid,
|
||||
) -> Result<WatchlistPageResult, DomainError> {
|
||||
let remote_entries = crate::federation::get_remote_watchlist::execute(ctx, user_id)
|
||||
.await
|
||||
.unwrap_or_default();
|
||||
let len = remote_entries.len() as u32;
|
||||
let display_entries = remote_entries
|
||||
.into_iter()
|
||||
.map(|e| WatchlistDisplayEntry {
|
||||
poster_url: e.poster_url,
|
||||
movie_title: e.movie_title,
|
||||
release_year: e.release_year,
|
||||
movie_url: None,
|
||||
added_at: e.added_at.format("%b %-d, %Y").to_string(),
|
||||
remove_url: None,
|
||||
})
|
||||
.collect();
|
||||
Ok(WatchlistPageResult {
|
||||
display_entries,
|
||||
has_more: false,
|
||||
current_offset: 0,
|
||||
limit: len,
|
||||
})
|
||||
}
|
||||
12
crates/application/src/watchlist/is_on.rs
Normal file
12
crates/application/src/watchlist/is_on.rs
Normal file
@@ -0,0 +1,12 @@
|
||||
use domain::{
|
||||
errors::DomainError,
|
||||
value_objects::{MovieId, UserId},
|
||||
};
|
||||
|
||||
use crate::{context::AppContext, watchlist::queries::IsOnWatchlistQuery};
|
||||
|
||||
pub async fn execute(ctx: &AppContext, query: IsOnWatchlistQuery) -> Result<bool, DomainError> {
|
||||
let user_id = UserId::from_uuid(query.user_id);
|
||||
let movie_id = MovieId::from_uuid(query.movie_id);
|
||||
ctx.repos.watchlist.contains(&user_id, &movie_id).await
|
||||
}
|
||||
7
crates/application/src/watchlist/mod.rs
Normal file
7
crates/application/src/watchlist/mod.rs
Normal file
@@ -0,0 +1,7 @@
|
||||
pub mod add;
|
||||
pub mod commands;
|
||||
pub mod get;
|
||||
pub mod get_page;
|
||||
pub mod is_on;
|
||||
pub mod queries;
|
||||
pub mod remove;
|
||||
12
crates/application/src/watchlist/queries.rs
Normal file
12
crates/application/src/watchlist/queries.rs
Normal file
@@ -0,0 +1,12 @@
|
||||
use uuid::Uuid;
|
||||
|
||||
pub struct GetWatchlistQuery {
|
||||
pub user_id: Uuid,
|
||||
pub limit: Option<u32>,
|
||||
pub offset: Option<u32>,
|
||||
}
|
||||
|
||||
pub struct IsOnWatchlistQuery {
|
||||
pub user_id: Uuid,
|
||||
pub movie_id: Uuid,
|
||||
}
|
||||
21
crates/application/src/watchlist/remove.rs
Normal file
21
crates/application/src/watchlist/remove.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
use domain::{
|
||||
errors::DomainError,
|
||||
events::DomainEvent,
|
||||
value_objects::{MovieId, UserId},
|
||||
};
|
||||
|
||||
use crate::{context::AppContext, watchlist::commands::RemoveFromWatchlistCommand};
|
||||
|
||||
pub async fn execute(ctx: &AppContext, cmd: RemoveFromWatchlistCommand) -> Result<(), DomainError> {
|
||||
let user_id = UserId::from_uuid(cmd.user_id);
|
||||
let movie_id = MovieId::from_uuid(cmd.movie_id);
|
||||
ctx.repos.watchlist.remove(&user_id, &movie_id).await?;
|
||||
|
||||
let _ = ctx
|
||||
.services
|
||||
.event_publisher
|
||||
.publish(&DomainEvent::WatchlistEntryRemoved { user_id, movie_id })
|
||||
.await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
87
crates/application/src/watchlist/tests/add.rs
Normal file
87
crates/application/src/watchlist/tests/add.rs
Normal file
@@ -0,0 +1,87 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use domain::{
|
||||
models::Movie,
|
||||
ports::MovieRepository,
|
||||
testing::{InMemoryMovieRepository, InMemoryWatchlistRepository},
|
||||
value_objects::{MovieTitle, ReleaseYear},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
diary::commands::MovieInput, test_helpers::TestContextBuilder, watchlist::add,
|
||||
watchlist::commands::AddToWatchlistCommand,
|
||||
};
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_add_to_watchlist_resolves_and_saves() {
|
||||
let movies = InMemoryMovieRepository::new();
|
||||
let watchlist = InMemoryWatchlistRepository::new();
|
||||
|
||||
let movie = Movie::new(
|
||||
None,
|
||||
MovieTitle::new("The Thing".into()).unwrap(),
|
||||
ReleaseYear::new(1982).unwrap(),
|
||||
None,
|
||||
None,
|
||||
);
|
||||
let movie_uuid = movie.id().value();
|
||||
movies.upsert_movie(&movie).await.unwrap();
|
||||
|
||||
let ctx = TestContextBuilder::new()
|
||||
.with_movies(Arc::clone(&movies) as _)
|
||||
.with_watchlist(Arc::clone(&watchlist) as _)
|
||||
.build();
|
||||
|
||||
let cmd = AddToWatchlistCommand {
|
||||
user_id: uuid::Uuid::new_v4(),
|
||||
input: MovieInput {
|
||||
movie_id: Some(movie_uuid),
|
||||
external_metadata_id: None,
|
||||
manual_title: None,
|
||||
manual_release_year: None,
|
||||
manual_director: None,
|
||||
},
|
||||
};
|
||||
|
||||
add::execute(&ctx, cmd).await.unwrap();
|
||||
|
||||
assert_eq!(watchlist.count(), 1);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_add_to_watchlist_already_present_is_idempotent() {
|
||||
let movies = InMemoryMovieRepository::new();
|
||||
let watchlist = InMemoryWatchlistRepository::new();
|
||||
|
||||
let movie = Movie::new(
|
||||
None,
|
||||
MovieTitle::new("RoboCop".into()).unwrap(),
|
||||
ReleaseYear::new(1987).unwrap(),
|
||||
None,
|
||||
None,
|
||||
);
|
||||
let movie_uuid = movie.id().value();
|
||||
let user_id = uuid::Uuid::new_v4();
|
||||
movies.upsert_movie(&movie).await.unwrap();
|
||||
|
||||
let ctx = TestContextBuilder::new()
|
||||
.with_movies(Arc::clone(&movies) as _)
|
||||
.with_watchlist(Arc::clone(&watchlist) as _)
|
||||
.build();
|
||||
|
||||
let make_cmd = || AddToWatchlistCommand {
|
||||
user_id,
|
||||
input: MovieInput {
|
||||
movie_id: Some(movie_uuid),
|
||||
external_metadata_id: None,
|
||||
manual_title: None,
|
||||
manual_release_year: None,
|
||||
manual_director: None,
|
||||
},
|
||||
};
|
||||
|
||||
add::execute(&ctx, make_cmd()).await.unwrap();
|
||||
add::execute(&ctx, make_cmd()).await.unwrap();
|
||||
|
||||
assert_eq!(watchlist.count(), 1, "idempotent add should not duplicate");
|
||||
}
|
||||
Reference in New Issue
Block a user