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,
|
||||
|
||||
Reference in New Issue
Block a user