refactor(watchlist): WatchlistAddDeps, scoped Arc deps
This commit is contained in:
@@ -6,34 +6,31 @@ use domain::{
|
||||
};
|
||||
|
||||
use crate::{
|
||||
context::AppContext,
|
||||
diary::movie_resolver::{MovieResolver, MovieResolverDeps},
|
||||
watchlist::commands::AddToWatchlistCommand,
|
||||
watchlist::{commands::AddToWatchlistCommand, deps::WatchlistAddDeps},
|
||||
};
|
||||
|
||||
pub async fn execute(ctx: &AppContext, cmd: AddToWatchlistCommand) -> Result<(), DomainError> {
|
||||
pub async fn execute(deps: &WatchlistAddDeps, 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
|
||||
deps.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 resolver_deps = MovieResolverDeps {
|
||||
repository: deps.movie.as_ref(),
|
||||
metadata_client: deps.metadata.as_ref(),
|
||||
};
|
||||
let (movie, is_new) = MovieResolver::default_pipeline()
|
||||
.resolve(&cmd.input, &deps)
|
||||
.resolve(&cmd.input, &resolver_deps)
|
||||
.await?;
|
||||
if is_new {
|
||||
ctx.repos.movie.upsert_movie(&movie).await?;
|
||||
deps.movie.upsert_movie(&movie).await?;
|
||||
if let Some(ext_id) = movie.external_metadata_id() {
|
||||
let _ = ctx
|
||||
.services
|
||||
let _ = deps
|
||||
.event_publisher
|
||||
.publish(&DomainEvent::MovieDiscovered {
|
||||
movie_id: movie.id().clone(),
|
||||
@@ -46,10 +43,9 @@ pub async fn execute(ctx: &AppContext, cmd: AddToWatchlistCommand) -> Result<(),
|
||||
};
|
||||
|
||||
let entry = WatchlistEntry::new(user_id.clone(), movie.id().clone());
|
||||
ctx.repos.watchlist.add(&entry).await?;
|
||||
deps.watchlist.add(&entry).await?;
|
||||
|
||||
let _ = ctx
|
||||
.services
|
||||
let _ = deps
|
||||
.event_publisher
|
||||
.publish(&DomainEvent::WatchlistEntryAdded {
|
||||
user_id,
|
||||
|
||||
10
crates/application/src/watchlist/deps.rs
Normal file
10
crates/application/src/watchlist/deps.rs
Normal file
@@ -0,0 +1,10 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use domain::ports::{EventPublisher, MetadataClient, MovieRepository, WatchlistRepository};
|
||||
|
||||
pub struct WatchlistAddDeps {
|
||||
pub movie: Arc<dyn MovieRepository>,
|
||||
pub metadata: Arc<dyn MetadataClient>,
|
||||
pub watchlist: Arc<dyn WatchlistRepository>,
|
||||
pub event_publisher: Arc<dyn EventPublisher>,
|
||||
}
|
||||
@@ -1,21 +1,24 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use domain::{
|
||||
errors::DomainError,
|
||||
models::{
|
||||
WatchlistWithMovie,
|
||||
collections::{PageParams, Paginated},
|
||||
},
|
||||
ports::WatchlistRepository,
|
||||
value_objects::UserId,
|
||||
};
|
||||
|
||||
use crate::{context::AppContext, watchlist::queries::GetWatchlistQuery};
|
||||
use crate::watchlist::queries::GetWatchlistQuery;
|
||||
|
||||
pub async fn execute(
|
||||
ctx: &AppContext,
|
||||
watchlist: Arc<dyn WatchlistRepository>,
|
||||
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
|
||||
watchlist.get_for_user(&user_id, &page).await
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -1,14 +1,20 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use domain::{
|
||||
errors::DomainError,
|
||||
ports::WatchlistRepository,
|
||||
value_objects::{MovieId, UserId},
|
||||
};
|
||||
|
||||
use crate::{context::AppContext, watchlist::queries::IsOnWatchlistQuery};
|
||||
use crate::watchlist::queries::IsOnWatchlistQuery;
|
||||
|
||||
pub async fn execute(ctx: &AppContext, query: IsOnWatchlistQuery) -> Result<bool, DomainError> {
|
||||
pub async fn execute(
|
||||
watchlist: Arc<dyn WatchlistRepository>,
|
||||
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
|
||||
watchlist.contains(&user_id, &movie_id).await
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
pub mod add;
|
||||
pub mod commands;
|
||||
pub mod deps;
|
||||
pub mod get;
|
||||
pub mod is_on;
|
||||
pub mod queries;
|
||||
|
||||
@@ -1,19 +1,24 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use domain::{
|
||||
errors::DomainError,
|
||||
events::DomainEvent,
|
||||
ports::{EventPublisher, WatchlistRepository},
|
||||
value_objects::{MovieId, UserId},
|
||||
};
|
||||
|
||||
use crate::{context::AppContext, watchlist::commands::RemoveFromWatchlistCommand};
|
||||
use crate::watchlist::commands::RemoveFromWatchlistCommand;
|
||||
|
||||
pub async fn execute(ctx: &AppContext, cmd: RemoveFromWatchlistCommand) -> Result<(), DomainError> {
|
||||
pub async fn execute(
|
||||
watchlist: Arc<dyn WatchlistRepository>,
|
||||
event_publisher: Arc<dyn EventPublisher>,
|
||||
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?;
|
||||
watchlist.remove(&user_id, &movie_id).await?;
|
||||
|
||||
let _ = ctx
|
||||
.services
|
||||
.event_publisher
|
||||
let _ = event_publisher
|
||||
.publish(&DomainEvent::WatchlistEntryRemoved { user_id, movie_id })
|
||||
.await;
|
||||
|
||||
|
||||
@@ -3,15 +3,27 @@ use std::sync::Arc;
|
||||
use domain::{
|
||||
models::Movie,
|
||||
ports::MovieRepository,
|
||||
testing::{InMemoryMovieRepository, InMemoryWatchlistRepository},
|
||||
testing::{InMemoryMovieRepository, InMemoryWatchlistRepository, NoopEventPublisher},
|
||||
value_objects::{MovieTitle, ReleaseYear},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
diary::commands::MovieInput, test_helpers::TestContextBuilder, watchlist::add,
|
||||
watchlist::commands::AddToWatchlistCommand,
|
||||
diary::commands::MovieInput,
|
||||
watchlist::{add, commands::AddToWatchlistCommand, deps::WatchlistAddDeps},
|
||||
};
|
||||
|
||||
fn make_deps(
|
||||
movies: Arc<InMemoryMovieRepository>,
|
||||
watchlist: Arc<InMemoryWatchlistRepository>,
|
||||
) -> WatchlistAddDeps {
|
||||
WatchlistAddDeps {
|
||||
movie: movies,
|
||||
metadata: Arc::new(domain::testing::FakeMetadataClient),
|
||||
watchlist,
|
||||
event_publisher: NoopEventPublisher::new(),
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_add_to_watchlist_resolves_and_saves() {
|
||||
let movies = InMemoryMovieRepository::new();
|
||||
@@ -27,10 +39,7 @@ async fn test_add_to_watchlist_resolves_and_saves() {
|
||||
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 deps = make_deps(Arc::clone(&movies), Arc::clone(&watchlist));
|
||||
|
||||
let cmd = AddToWatchlistCommand {
|
||||
user_id: uuid::Uuid::new_v4(),
|
||||
@@ -43,7 +52,7 @@ async fn test_add_to_watchlist_resolves_and_saves() {
|
||||
},
|
||||
};
|
||||
|
||||
add::execute(&ctx, cmd).await.unwrap();
|
||||
add::execute(&deps, cmd).await.unwrap();
|
||||
|
||||
assert_eq!(watchlist.count(), 1);
|
||||
}
|
||||
@@ -64,10 +73,7 @@ async fn test_add_to_watchlist_already_present_is_idempotent() {
|
||||
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 deps = make_deps(Arc::clone(&movies), Arc::clone(&watchlist));
|
||||
|
||||
let make_cmd = || AddToWatchlistCommand {
|
||||
user_id,
|
||||
@@ -80,8 +86,8 @@ async fn test_add_to_watchlist_already_present_is_idempotent() {
|
||||
},
|
||||
};
|
||||
|
||||
add::execute(&ctx, make_cmd()).await.unwrap();
|
||||
add::execute(&ctx, make_cmd()).await.unwrap();
|
||||
add::execute(&deps, make_cmd()).await.unwrap();
|
||||
add::execute(&deps, make_cmd()).await.unwrap();
|
||||
|
||||
assert_eq!(watchlist.count(), 1, "idempotent add should not duplicate");
|
||||
}
|
||||
@@ -91,10 +97,7 @@ async fn test_add_to_watchlist_with_manual_movie() {
|
||||
let movies = InMemoryMovieRepository::new();
|
||||
let watchlist = InMemoryWatchlistRepository::new();
|
||||
|
||||
let ctx = TestContextBuilder::new()
|
||||
.with_movies(Arc::clone(&movies) as _)
|
||||
.with_watchlist(Arc::clone(&watchlist) as _)
|
||||
.build();
|
||||
let deps = make_deps(Arc::clone(&movies), Arc::clone(&watchlist));
|
||||
|
||||
let cmd = AddToWatchlistCommand {
|
||||
user_id: uuid::Uuid::new_v4(),
|
||||
@@ -107,7 +110,7 @@ async fn test_add_to_watchlist_with_manual_movie() {
|
||||
},
|
||||
};
|
||||
|
||||
add::execute(&ctx, cmd).await.unwrap();
|
||||
add::execute(&deps, cmd).await.unwrap();
|
||||
|
||||
assert_eq!(watchlist.count(), 1);
|
||||
assert_eq!(movies.count(), 1);
|
||||
@@ -118,10 +121,7 @@ async fn test_add_to_watchlist_movie_not_found_by_id() {
|
||||
let movies = InMemoryMovieRepository::new();
|
||||
let watchlist = InMemoryWatchlistRepository::new();
|
||||
|
||||
let ctx = TestContextBuilder::new()
|
||||
.with_movies(Arc::clone(&movies) as _)
|
||||
.with_watchlist(Arc::clone(&watchlist) as _)
|
||||
.build();
|
||||
let deps = make_deps(Arc::clone(&movies), Arc::clone(&watchlist));
|
||||
|
||||
let cmd = AddToWatchlistCommand {
|
||||
user_id: uuid::Uuid::new_v4(),
|
||||
@@ -134,5 +134,5 @@ async fn test_add_to_watchlist_movie_not_found_by_id() {
|
||||
},
|
||||
};
|
||||
|
||||
assert!(add::execute(&ctx, cmd).await.is_err());
|
||||
assert!(add::execute(&deps, cmd).await.is_err());
|
||||
}
|
||||
|
||||
@@ -5,9 +5,9 @@ use crate::watchlist::{get, queries::GetWatchlistQuery};
|
||||
|
||||
#[tokio::test]
|
||||
async fn returns_empty_page_for_new_user() {
|
||||
let ctx = TestContextBuilder::new().build();
|
||||
let b = TestContextBuilder::new();
|
||||
let result = get::execute(
|
||||
&ctx,
|
||||
b.watchlist_repo.clone(),
|
||||
GetWatchlistQuery {
|
||||
user_id: Uuid::new_v4(),
|
||||
limit: None,
|
||||
|
||||
@@ -22,12 +22,8 @@ async fn returns_true_when_present() {
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let ctx = TestContextBuilder::new()
|
||||
.with_watchlist(Arc::clone(&watchlist) as _)
|
||||
.build();
|
||||
|
||||
let result = is_on::execute(
|
||||
&ctx,
|
||||
Arc::clone(&watchlist) as _,
|
||||
IsOnWatchlistQuery {
|
||||
user_id: uid,
|
||||
movie_id: mid,
|
||||
@@ -41,9 +37,9 @@ async fn returns_true_when_present() {
|
||||
|
||||
#[tokio::test]
|
||||
async fn returns_false_when_absent() {
|
||||
let ctx = TestContextBuilder::new().build();
|
||||
let b = TestContextBuilder::new();
|
||||
let result = is_on::execute(
|
||||
&ctx,
|
||||
b.watchlist_repo.clone(),
|
||||
IsOnWatchlistQuery {
|
||||
user_id: Uuid::new_v4(),
|
||||
movie_id: Uuid::new_v4(),
|
||||
|
||||
@@ -24,13 +24,9 @@ async fn removes_entry_and_emits_event() {
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let ctx = TestContextBuilder::new()
|
||||
.with_watchlist(Arc::clone(&watchlist) as _)
|
||||
.with_event_publisher(Arc::clone(&events) as _)
|
||||
.build();
|
||||
|
||||
remove::execute(
|
||||
&ctx,
|
||||
Arc::clone(&watchlist) as _,
|
||||
Arc::clone(&events) as _,
|
||||
RemoveFromWatchlistCommand {
|
||||
user_id: uid,
|
||||
movie_id: mid,
|
||||
@@ -50,9 +46,10 @@ async fn removes_entry_and_emits_event() {
|
||||
|
||||
#[tokio::test]
|
||||
async fn fails_when_not_on_watchlist() {
|
||||
let ctx = TestContextBuilder::new().build();
|
||||
let b = TestContextBuilder::new();
|
||||
let result = remove::execute(
|
||||
&ctx,
|
||||
b.watchlist_repo.clone(),
|
||||
b.event_publisher.clone(),
|
||||
RemoveFromWatchlistCommand {
|
||||
user_id: Uuid::new_v4(),
|
||||
movie_id: Uuid::new_v4(),
|
||||
|
||||
Reference in New Issue
Block a user