importer feature
This commit is contained in:
62
crates/application/src/use_cases/apply_import_mapping.rs
Normal file
62
crates/application/src/use_cases/apply_import_mapping.rs
Normal file
@@ -0,0 +1,62 @@
|
||||
use domain::{
|
||||
errors::DomainError,
|
||||
value_objects::{ExternalMetadataId, ImportSessionId, MovieTitle, ReleaseYear, UserId},
|
||||
};
|
||||
use importer::{AnnotatedRow, ParsedFile, apply_mapping};
|
||||
|
||||
use crate::{commands::ApplyImportMappingCommand, context::AppContext};
|
||||
|
||||
pub async fn execute(ctx: &AppContext, cmd: ApplyImportMappingCommand) -> Result<Vec<AnnotatedRow>, DomainError> {
|
||||
let user_id = UserId::from_uuid(cmd.user_id);
|
||||
let session_id = ImportSessionId::from_uuid(cmd.session_id);
|
||||
let mappings = cmd.mappings;
|
||||
let mut session = ctx.import_session_repository
|
||||
.get(&session_id, &user_id)
|
||||
.await?
|
||||
.ok_or_else(|| DomainError::NotFound("import session".into()))?;
|
||||
|
||||
let parsed: ParsedFile = serde_json::from_str(&session.parsed_data)
|
||||
.map_err(|e| DomainError::InfrastructureError(e.to_string()))?;
|
||||
|
||||
let mut annotated = apply_mapping(&parsed, &mappings);
|
||||
|
||||
for row in annotated.iter_mut() {
|
||||
if let importer::RowResult::Valid(ref import_row) = row.result {
|
||||
row.is_duplicate = check_duplicate(ctx, import_row).await?;
|
||||
}
|
||||
}
|
||||
|
||||
session.field_mappings = Some(
|
||||
serde_json::to_string(&mappings)
|
||||
.map_err(|e| DomainError::InfrastructureError(e.to_string()))?
|
||||
);
|
||||
session.row_results = Some(
|
||||
serde_json::to_string(&annotated)
|
||||
.map_err(|e| DomainError::InfrastructureError(e.to_string()))?
|
||||
);
|
||||
|
||||
ctx.import_session_repository.update(&session).await?;
|
||||
|
||||
Ok(annotated)
|
||||
}
|
||||
|
||||
async fn check_duplicate(ctx: &AppContext, row: &importer::ImportRow) -> Result<bool, DomainError> {
|
||||
if let Some(ext_id) = &row.external_metadata_id {
|
||||
if let Ok(eid) = ExternalMetadataId::new(ext_id.clone()) {
|
||||
if ctx.movie_repository.get_movie_by_external_id(&eid).await?.is_some() {
|
||||
return Ok(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
if let (Some(title), Some(year_str)) = (&row.title, &row.release_year) {
|
||||
let title_vo = MovieTitle::new(title.clone());
|
||||
let year_vo = year_str.parse::<u16>().ok().and_then(|y| ReleaseYear::new(y).ok());
|
||||
if let (Ok(t), Some(y)) = (title_vo, year_vo) {
|
||||
let matches = ctx.movie_repository.get_movies_by_title_and_year(&t, &y).await?;
|
||||
if !matches.is_empty() {
|
||||
return Ok(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(false)
|
||||
}
|
||||
20
crates/application/src/use_cases/apply_import_profile.rs
Normal file
20
crates/application/src/use_cases/apply_import_profile.rs
Normal file
@@ -0,0 +1,20 @@
|
||||
use domain::{errors::DomainError, value_objects::{ImportProfileId, ImportSessionId, UserId}};
|
||||
use crate::{commands::ApplyImportProfileCommand, context::AppContext};
|
||||
|
||||
/// Copies the profile's field_mappings onto the session. Caller must then invoke
|
||||
/// apply_import_mapping to regenerate row_results with the new mappings.
|
||||
pub async fn execute(ctx: &AppContext, cmd: ApplyImportProfileCommand) -> Result<(), DomainError> {
|
||||
let user_id = UserId::from_uuid(cmd.user_id);
|
||||
let session_id = ImportSessionId::from_uuid(cmd.session_id);
|
||||
let profile_id = ImportProfileId::from_uuid(cmd.profile_id);
|
||||
|
||||
let profile = ctx.import_profile_repository
|
||||
.get(&profile_id, &user_id).await?
|
||||
.ok_or_else(|| DomainError::NotFound("import profile".into()))?;
|
||||
let mut session = ctx.import_session_repository
|
||||
.get(&session_id, &user_id).await?
|
||||
.ok_or_else(|| DomainError::NotFound("import session".into()))?;
|
||||
session.field_mappings = Some(profile.field_mappings);
|
||||
session.row_results = None;
|
||||
ctx.import_session_repository.update(&session).await
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
use domain::errors::DomainError;
|
||||
use crate::context::AppContext;
|
||||
|
||||
pub async fn execute(ctx: &AppContext) -> Result<u64, DomainError> {
|
||||
ctx.import_session_repository.delete_expired().await
|
||||
}
|
||||
44
crates/application/src/use_cases/create_import_session.rs
Normal file
44
crates/application/src/use_cases/create_import_session.rs
Normal file
@@ -0,0 +1,44 @@
|
||||
use chrono::Utc;
|
||||
use domain::{errors::DomainError, models::ImportSession, value_objects::{ImportSessionId, UserId}};
|
||||
use importer::{ImportError, ParsedFile};
|
||||
|
||||
use crate::{commands::{CreateImportSessionCommand, FileFormat}, context::AppContext};
|
||||
|
||||
pub struct CreateSessionResult {
|
||||
pub session_id: ImportSessionId,
|
||||
pub columns: Vec<String>,
|
||||
pub sample_rows: Vec<Vec<String>>,
|
||||
}
|
||||
|
||||
pub async fn execute(ctx: &AppContext, cmd: CreateImportSessionCommand) -> Result<CreateSessionResult, DomainError> {
|
||||
let user_id = UserId::from_uuid(cmd.user_id);
|
||||
ctx.import_session_repository.delete_expired_for_user(&user_id).await?;
|
||||
|
||||
let parsed = parse(cmd.bytes, cmd.format).map_err(|e| DomainError::ValidationError(e.to_string()))?;
|
||||
let sample_rows = parsed.rows.iter().take(5).cloned().collect();
|
||||
let columns = parsed.columns.clone();
|
||||
|
||||
let parsed_data = serde_json::to_string(&parsed)
|
||||
.map_err(|e| DomainError::InfrastructureError(e.to_string()))?;
|
||||
|
||||
let now = Utc::now().naive_utc();
|
||||
let session = ImportSession::new(ImportSessionId::generate(), user_id, parsed_data, now);
|
||||
let session_id = session.id.clone();
|
||||
|
||||
ctx.import_session_repository.create(&session).await?;
|
||||
|
||||
Ok(CreateSessionResult { session_id, columns, sample_rows })
|
||||
}
|
||||
|
||||
fn parse(bytes: Vec<u8>, format: FileFormat) -> Result<ParsedFile, ImportError> {
|
||||
match format {
|
||||
FileFormat::Csv => importer::parse_csv(&bytes),
|
||||
FileFormat::Json => importer::parse_json(&bytes),
|
||||
FileFormat::Xlsx => {
|
||||
#[cfg(feature = "xlsx")]
|
||||
{ importer::parse_xlsx(&bytes) }
|
||||
#[cfg(not(feature = "xlsx"))]
|
||||
{ Err(ImportError::Xlsx("XLSX support not compiled in".into())) }
|
||||
}
|
||||
}
|
||||
}
|
||||
12
crates/application/src/use_cases/delete_import_profile.rs
Normal file
12
crates/application/src/use_cases/delete_import_profile.rs
Normal file
@@ -0,0 +1,12 @@
|
||||
use domain::{errors::DomainError, value_objects::{ImportProfileId, UserId}};
|
||||
use crate::{commands::DeleteImportProfileCommand, context::AppContext};
|
||||
|
||||
pub async fn execute(ctx: &AppContext, cmd: DeleteImportProfileCommand) -> Result<(), DomainError> {
|
||||
let user_id = UserId::from_uuid(cmd.user_id);
|
||||
let profile_id = ImportProfileId::from_uuid(cmd.profile_id);
|
||||
|
||||
ctx.import_profile_repository
|
||||
.get(&profile_id, &user_id).await?
|
||||
.ok_or_else(|| DomainError::NotFound("import profile".into()))?;
|
||||
ctx.import_profile_repository.delete(&profile_id).await
|
||||
}
|
||||
84
crates/application/src/use_cases/execute_import.rs
Normal file
84
crates/application/src/use_cases/execute_import.rs
Normal file
@@ -0,0 +1,84 @@
|
||||
use chrono::NaiveDateTime;
|
||||
use domain::{errors::DomainError, value_objects::{ImportSessionId, UserId}};
|
||||
use importer::{AnnotatedRow, ImportRow, RowResult};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{commands::{ExecuteImportCommand, LogReviewCommand}, context::AppContext, use_cases::log_review};
|
||||
|
||||
pub struct ImportSummary {
|
||||
pub imported: usize,
|
||||
pub skipped_duplicates: usize,
|
||||
pub failed: Vec<(usize, String)>,
|
||||
}
|
||||
|
||||
pub async fn execute(ctx: &AppContext, cmd: ExecuteImportCommand) -> Result<ImportSummary, DomainError> {
|
||||
let user_id = UserId::from_uuid(cmd.user_id);
|
||||
let session_id = ImportSessionId::from_uuid(cmd.session_id);
|
||||
let confirmed_indices = cmd.confirmed_indices;
|
||||
let session = ctx.import_session_repository
|
||||
.get(&session_id, &user_id)
|
||||
.await?
|
||||
.ok_or_else(|| DomainError::NotFound("import session".into()))?;
|
||||
|
||||
let row_results: Vec<AnnotatedRow> = session.row_results
|
||||
.as_deref()
|
||||
.and_then(|s| serde_json::from_str(s).ok())
|
||||
.unwrap_or_default();
|
||||
|
||||
let confirmed_set: std::collections::HashSet<usize> = confirmed_indices.into_iter().collect();
|
||||
|
||||
let mut imported = 0;
|
||||
let mut skipped_duplicates = 0;
|
||||
let mut failed = Vec::new();
|
||||
|
||||
for (idx, annotated) in row_results.into_iter().enumerate() {
|
||||
if !confirmed_set.contains(&idx) {
|
||||
skipped_duplicates += 1;
|
||||
continue;
|
||||
}
|
||||
match annotated.result {
|
||||
RowResult::Valid(row) => {
|
||||
match row_to_command(&row, user_id.value()) {
|
||||
Ok(cmd) => {
|
||||
match log_review::execute(ctx, cmd).await {
|
||||
Ok(_) => imported += 1,
|
||||
Err(e) => failed.push((idx, e.to_string())),
|
||||
}
|
||||
}
|
||||
Err(e) => failed.push((idx, e)),
|
||||
}
|
||||
}
|
||||
RowResult::Invalid { errors, .. } => {
|
||||
failed.push((idx, errors.join("; ")));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ctx.import_session_repository.delete(&session_id).await?;
|
||||
|
||||
Ok(ImportSummary { imported, skipped_duplicates, failed })
|
||||
}
|
||||
|
||||
fn row_to_command(row: &ImportRow, user_id: Uuid) -> Result<LogReviewCommand, String> {
|
||||
let rating = row.rating.as_deref()
|
||||
.ok_or("missing rating")?
|
||||
.parse::<u8>()
|
||||
.map_err(|_| "rating is not a valid u8".to_string())?;
|
||||
|
||||
let watched_at_str = row.watched_at.as_deref().ok_or("missing watched_at")?;
|
||||
let watched_at = NaiveDateTime::parse_from_str(&format!("{} 00:00:00", watched_at_str), "%Y-%m-%d %H:%M:%S")
|
||||
.or_else(|_| NaiveDateTime::parse_from_str(watched_at_str, "%Y-%m-%d %H:%M:%S"))
|
||||
.or_else(|_| NaiveDateTime::parse_from_str(watched_at_str, "%Y-%m-%dT%H:%M:%S"))
|
||||
.map_err(|_| format!("cannot parse watched_at: '{}'", watched_at_str))?;
|
||||
|
||||
Ok(LogReviewCommand {
|
||||
external_metadata_id: row.external_metadata_id.clone(),
|
||||
manual_title: row.title.clone(),
|
||||
manual_release_year: row.release_year.as_deref().and_then(|s| s.parse().ok()),
|
||||
manual_director: row.director.clone(),
|
||||
user_id,
|
||||
rating,
|
||||
comment: row.comment.clone(),
|
||||
watched_at,
|
||||
})
|
||||
}
|
||||
6
crates/application/src/use_cases/list_import_profiles.rs
Normal file
6
crates/application/src/use_cases/list_import_profiles.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
use domain::{errors::DomainError, models::ImportProfile, value_objects::UserId};
|
||||
use crate::context::AppContext;
|
||||
|
||||
pub async fn execute(ctx: &AppContext, user_id: &UserId) -> Result<Vec<ImportProfile>, DomainError> {
|
||||
ctx.import_profile_repository.list_for_user(user_id).await
|
||||
}
|
||||
@@ -1,4 +1,12 @@
|
||||
pub mod apply_import_mapping;
|
||||
pub mod apply_import_profile;
|
||||
pub mod cleanup_expired_import_sessions;
|
||||
pub mod create_import_session;
|
||||
pub mod delete_import_profile;
|
||||
pub mod delete_review;
|
||||
pub mod execute_import;
|
||||
pub mod list_import_profiles;
|
||||
pub mod save_import_profile;
|
||||
pub mod export_diary;
|
||||
pub mod get_activity_feed;
|
||||
pub mod get_diary;
|
||||
|
||||
18
crates/application/src/use_cases/save_import_profile.rs
Normal file
18
crates/application/src/use_cases/save_import_profile.rs
Normal file
@@ -0,0 +1,18 @@
|
||||
use chrono::Utc;
|
||||
use domain::{errors::DomainError, models::ImportProfile, value_objects::{ImportProfileId, ImportSessionId, UserId}};
|
||||
use crate::{commands::SaveImportProfileCommand, context::AppContext};
|
||||
|
||||
pub async fn execute(ctx: &AppContext, cmd: SaveImportProfileCommand) -> Result<ImportProfileId, DomainError> {
|
||||
let user_id = UserId::from_uuid(cmd.user_id);
|
||||
let session_id = ImportSessionId::from_uuid(cmd.session_id);
|
||||
|
||||
let session = ctx.import_session_repository
|
||||
.get(&session_id, &user_id).await?
|
||||
.ok_or_else(|| DomainError::NotFound("import session".into()))?;
|
||||
let mappings = session.field_mappings
|
||||
.ok_or_else(|| DomainError::ValidationError("no mapping applied to this session yet".into()))?;
|
||||
let profile = ImportProfile::new(ImportProfileId::generate(), user_id, cmd.name, mappings, Utc::now().naive_utc());
|
||||
let id = profile.id.clone();
|
||||
ctx.import_profile_repository.save(&profile).await?;
|
||||
Ok(id)
|
||||
}
|
||||
Reference in New Issue
Block a user