feat(application): wrapup generate/get/list use cases
This commit is contained in:
@@ -6,7 +6,7 @@ use domain::ports::{
|
||||
MovieRepository, PasswordHasher, PersonCommand, PersonQuery, PosterFetcherClient,
|
||||
RemoteWatchlistRepository, ReviewRepository, SearchCommand, SearchPort, SocialQueryPort,
|
||||
StatsRepository, UserProfileFieldsRepository, UserRepository, WatchEventRepository,
|
||||
WatchlistRepository, WrapUpStatsQuery, WebhookTokenRepository,
|
||||
WatchlistRepository, WrapUpRepository, WrapUpStatsQuery, WebhookTokenRepository,
|
||||
};
|
||||
|
||||
use crate::config::AppConfig;
|
||||
@@ -32,6 +32,7 @@ pub struct Repositories {
|
||||
pub remote_watchlist: Arc<dyn RemoteWatchlistRepository>,
|
||||
pub social_query: Arc<dyn SocialQueryPort>,
|
||||
pub wrapup_stats: Arc<dyn WrapUpStatsQuery>,
|
||||
pub wrapup_repo: Arc<dyn WrapUpRepository>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use domain::testing::{InMemoryWrapUpStatsQuery, NoopRemoteWatchlistRepository, NoopSocialQueryPort};
|
||||
use domain::testing::{InMemoryWrapUpRepository, InMemoryWrapUpStatsQuery, NoopRemoteWatchlistRepository, NoopSocialQueryPort};
|
||||
use domain::{
|
||||
ports::{
|
||||
AuthService, DiaryExporter, DiaryRepository, DocumentParser, EventPublisher, ImageStorage,
|
||||
@@ -8,7 +8,7 @@ use domain::{
|
||||
MovieRepository, PasswordHasher, PersonCommand, PersonQuery, PosterFetcherClient,
|
||||
ReviewRepository, SearchCommand, SearchPort, StatsRepository, UserProfileFieldsRepository,
|
||||
UserRepository, WatchEventRepository, WatchlistRepository, WebhookTokenRepository,
|
||||
WrapUpStatsQuery,
|
||||
WrapUpRepository, WrapUpStatsQuery,
|
||||
},
|
||||
testing::{
|
||||
FakeAuthService, FakeMetadataClient, FakePasswordHasher, InMemoryMovieRepository,
|
||||
@@ -52,6 +52,7 @@ pub struct TestContextBuilder {
|
||||
pub search_port: Arc<dyn SearchPort>,
|
||||
pub search_command: Arc<dyn SearchCommand>,
|
||||
pub wrapup_stats: Arc<dyn WrapUpStatsQuery>,
|
||||
pub wrapup_repo: Arc<dyn WrapUpRepository>,
|
||||
pub config: AppConfig,
|
||||
}
|
||||
|
||||
@@ -83,6 +84,7 @@ impl TestContextBuilder {
|
||||
search_port: Arc::new(PanicSearchPort),
|
||||
search_command: Arc::new(PanicSearchCommand),
|
||||
wrapup_stats: InMemoryWrapUpStatsQuery::new(),
|
||||
wrapup_repo: InMemoryWrapUpRepository::new(),
|
||||
config: AppConfig {
|
||||
allow_registration: true,
|
||||
base_url: "http://localhost:3000".into(),
|
||||
@@ -153,6 +155,7 @@ impl TestContextBuilder {
|
||||
remote_watchlist: Arc::new(NoopRemoteWatchlistRepository),
|
||||
social_query: Arc::new(NoopSocialQueryPort),
|
||||
wrapup_stats: self.wrapup_stats,
|
||||
wrapup_repo: self.wrapup_repo,
|
||||
},
|
||||
services: Services {
|
||||
auth: self.auth_service,
|
||||
|
||||
8
crates/application/src/wrapup/commands.rs
Normal file
8
crates/application/src/wrapup/commands.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
use chrono::NaiveDate;
|
||||
use uuid::Uuid;
|
||||
|
||||
pub struct RequestWrapUpCommand {
|
||||
pub user_id: Option<Uuid>,
|
||||
pub start_date: NaiveDate,
|
||||
pub end_date: NaiveDate,
|
||||
}
|
||||
47
crates/application/src/wrapup/generate.rs
Normal file
47
crates/application/src/wrapup/generate.rs
Normal file
@@ -0,0 +1,47 @@
|
||||
use domain::errors::DomainError;
|
||||
use domain::events::DomainEvent;
|
||||
use domain::models::wrapup::WrapUpStatus;
|
||||
use domain::value_objects::{UserId, WrapUpId};
|
||||
|
||||
use crate::context::AppContext;
|
||||
use crate::wrapup::commands::RequestWrapUpCommand;
|
||||
|
||||
pub async fn execute(ctx: &AppContext, cmd: RequestWrapUpCommand) -> Result<WrapUpId, DomainError> {
|
||||
let existing = ctx
|
||||
.repos
|
||||
.wrapup_repo
|
||||
.find_existing(cmd.user_id, cmd.start_date, cmd.end_date)
|
||||
.await?;
|
||||
|
||||
if let Some(ref rec) = existing {
|
||||
if rec.status == WrapUpStatus::Ready || rec.status == WrapUpStatus::Generating {
|
||||
return Ok(rec.id.clone());
|
||||
}
|
||||
}
|
||||
|
||||
let id = WrapUpId::generate();
|
||||
let record = domain::models::wrapup::WrapUpRecord {
|
||||
id: id.clone(),
|
||||
user_id: cmd.user_id,
|
||||
start_date: cmd.start_date,
|
||||
end_date: cmd.end_date,
|
||||
status: WrapUpStatus::Pending,
|
||||
report_json: None,
|
||||
error_message: None,
|
||||
created_at: chrono::Utc::now().naive_utc(),
|
||||
completed_at: None,
|
||||
};
|
||||
ctx.repos.wrapup_repo.create(&record).await?;
|
||||
|
||||
ctx.services
|
||||
.event_publisher
|
||||
.publish(&DomainEvent::WrapUpRequested {
|
||||
wrapup_id: id.clone(),
|
||||
user_id: cmd.user_id.map(UserId::from_uuid),
|
||||
start_date: cmd.start_date,
|
||||
end_date: cmd.end_date,
|
||||
})
|
||||
.await?;
|
||||
|
||||
Ok(id)
|
||||
}
|
||||
9
crates/application/src/wrapup/get_wrapup.rs
Normal file
9
crates/application/src/wrapup/get_wrapup.rs
Normal file
@@ -0,0 +1,9 @@
|
||||
use domain::errors::DomainError;
|
||||
use domain::models::wrapup::WrapUpRecord;
|
||||
use domain::value_objects::WrapUpId;
|
||||
|
||||
use crate::context::AppContext;
|
||||
|
||||
pub async fn execute(ctx: &AppContext, id: WrapUpId) -> Result<Option<WrapUpRecord>, DomainError> {
|
||||
ctx.repos.wrapup_repo.get_by_id(&id).await
|
||||
}
|
||||
20
crates/application/src/wrapup/list_wrapups.rs
Normal file
20
crates/application/src/wrapup/list_wrapups.rs
Normal file
@@ -0,0 +1,20 @@
|
||||
use uuid::Uuid;
|
||||
|
||||
use domain::errors::DomainError;
|
||||
use domain::models::wrapup::WrapUpRecord;
|
||||
|
||||
use crate::context::AppContext;
|
||||
|
||||
pub struct ListWrapUpsQuery {
|
||||
pub user_id: Option<Uuid>,
|
||||
}
|
||||
|
||||
pub async fn execute(
|
||||
ctx: &AppContext,
|
||||
query: ListWrapUpsQuery,
|
||||
) -> Result<Vec<WrapUpRecord>, DomainError> {
|
||||
match query.user_id {
|
||||
Some(uid) => ctx.repos.wrapup_repo.list_for_user(uid).await,
|
||||
None => ctx.repos.wrapup_repo.list_global().await,
|
||||
}
|
||||
}
|
||||
@@ -1,2 +1,6 @@
|
||||
pub mod commands;
|
||||
pub mod compute;
|
||||
pub mod generate;
|
||||
pub mod get_wrapup;
|
||||
pub mod list_wrapups;
|
||||
pub mod queries;
|
||||
|
||||
Reference in New Issue
Block a user