app: add sidecar sync commands (export, detect, import, resolve, full export/import)
This commit is contained in:
48
crates/application/src/processing/commands/complete_job.rs
Normal file
48
crates/application/src/processing/commands/complete_job.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
use std::sync::Arc;
|
||||
use domain::{
|
||||
entities::Job,
|
||||
errors::DomainError,
|
||||
events::DomainEvent,
|
||||
ports::{EventPublisher, JobBatchRepository, JobRepository},
|
||||
value_objects::{DateTimeStamp, StructuredData, SystemId},
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct CompleteJobCommand {
|
||||
pub job_id: SystemId,
|
||||
pub result: StructuredData,
|
||||
}
|
||||
|
||||
pub struct CompleteJobHandler {
|
||||
job_repo: Arc<dyn JobRepository>,
|
||||
batch_repo: Arc<dyn JobBatchRepository>,
|
||||
event_pub: Arc<dyn EventPublisher>,
|
||||
}
|
||||
|
||||
impl CompleteJobHandler {
|
||||
pub fn new(
|
||||
job_repo: Arc<dyn JobRepository>,
|
||||
batch_repo: Arc<dyn JobBatchRepository>,
|
||||
event_pub: Arc<dyn EventPublisher>,
|
||||
) -> Self {
|
||||
Self { job_repo, batch_repo, event_pub }
|
||||
}
|
||||
|
||||
pub async fn execute(&self, cmd: CompleteJobCommand) -> Result<Job, DomainError> {
|
||||
let mut job = self.job_repo.find_by_id(&cmd.job_id).await?
|
||||
.ok_or_else(|| DomainError::NotFound(format!("Job {} not found", cmd.job_id)))?;
|
||||
job.complete(cmd.result);
|
||||
self.job_repo.save(&job).await?;
|
||||
if let Some(ref batch_id) = job.batch_id {
|
||||
let mut batch = self.batch_repo.find_by_id(batch_id).await?
|
||||
.ok_or_else(|| DomainError::NotFound(format!("Batch {} not found", batch_id)))?;
|
||||
batch.record_completion();
|
||||
self.batch_repo.save(&batch).await?;
|
||||
}
|
||||
self.event_pub.publish(DomainEvent::JobCompleted {
|
||||
job_id: job.job_id,
|
||||
timestamp: DateTimeStamp::now(),
|
||||
}).await?;
|
||||
Ok(job)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
use std::sync::Arc;
|
||||
use domain::{
|
||||
entities::ProcessingPipeline,
|
||||
errors::DomainError,
|
||||
ports::{PipelineRepository, PluginRepository},
|
||||
value_objects::{StructuredData, SystemId},
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct PipelineStepConfig {
|
||||
pub plugin_id: SystemId,
|
||||
pub config: StructuredData,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct ConfigurePipelineCommand {
|
||||
pub trigger_event: String,
|
||||
pub steps: Vec<PipelineStepConfig>,
|
||||
}
|
||||
|
||||
pub struct ConfigurePipelineHandler {
|
||||
pipeline_repo: Arc<dyn PipelineRepository>,
|
||||
plugin_repo: Arc<dyn PluginRepository>,
|
||||
}
|
||||
|
||||
impl ConfigurePipelineHandler {
|
||||
pub fn new(
|
||||
pipeline_repo: Arc<dyn PipelineRepository>,
|
||||
plugin_repo: Arc<dyn PluginRepository>,
|
||||
) -> Self {
|
||||
Self { pipeline_repo, plugin_repo }
|
||||
}
|
||||
|
||||
pub async fn execute(&self, cmd: ConfigurePipelineCommand) -> Result<ProcessingPipeline, DomainError> {
|
||||
for step in &cmd.steps {
|
||||
self.plugin_repo.find_by_id(&step.plugin_id).await?
|
||||
.ok_or_else(|| DomainError::NotFound(format!("Plugin {} not found", step.plugin_id)))?;
|
||||
}
|
||||
let mut pipeline = ProcessingPipeline::new(cmd.trigger_event);
|
||||
for step in cmd.steps {
|
||||
pipeline.add_step(step.plugin_id, step.config);
|
||||
}
|
||||
self.pipeline_repo.save(&pipeline).await?;
|
||||
Ok(pipeline)
|
||||
}
|
||||
}
|
||||
45
crates/application/src/processing/commands/enqueue_job.rs
Normal file
45
crates/application/src/processing/commands/enqueue_job.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
use std::sync::Arc;
|
||||
use domain::{
|
||||
entities::{Job, JobType},
|
||||
errors::DomainError,
|
||||
events::DomainEvent,
|
||||
ports::{EventPublisher, JobRepository},
|
||||
value_objects::{DateTimeStamp, StructuredData, SystemId},
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct EnqueueJobCommand {
|
||||
pub job_type: JobType,
|
||||
pub priority: u32,
|
||||
pub payload: StructuredData,
|
||||
pub target_asset_id: Option<SystemId>,
|
||||
pub batch_id: Option<SystemId>,
|
||||
}
|
||||
|
||||
pub struct EnqueueJobHandler {
|
||||
job_repo: Arc<dyn JobRepository>,
|
||||
event_pub: Arc<dyn EventPublisher>,
|
||||
}
|
||||
|
||||
impl EnqueueJobHandler {
|
||||
pub fn new(job_repo: Arc<dyn JobRepository>, event_pub: Arc<dyn EventPublisher>) -> Self {
|
||||
Self { job_repo, event_pub }
|
||||
}
|
||||
|
||||
pub async fn execute(&self, cmd: EnqueueJobCommand) -> Result<Job, DomainError> {
|
||||
let mut job = Job::new(cmd.job_type.clone(), cmd.priority, cmd.payload);
|
||||
if let Some(id) = cmd.target_asset_id {
|
||||
job = job.with_target(id);
|
||||
}
|
||||
if let Some(id) = cmd.batch_id {
|
||||
job = job.with_batch(id);
|
||||
}
|
||||
self.job_repo.save(&job).await?;
|
||||
self.event_pub.publish(DomainEvent::JobEnqueued {
|
||||
job_id: job.job_id,
|
||||
job_type: format!("{:?}", cmd.job_type),
|
||||
timestamp: DateTimeStamp::now(),
|
||||
}).await?;
|
||||
Ok(job)
|
||||
}
|
||||
}
|
||||
57
crates/application/src/processing/commands/fail_job.rs
Normal file
57
crates/application/src/processing/commands/fail_job.rs
Normal file
@@ -0,0 +1,57 @@
|
||||
use std::sync::Arc;
|
||||
use domain::{
|
||||
entities::{Job, JobStatus},
|
||||
errors::DomainError,
|
||||
events::DomainEvent,
|
||||
ports::{EventPublisher, JobBatchRepository, JobRepository},
|
||||
value_objects::{DateTimeStamp, SystemId},
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct FailJobCommand {
|
||||
pub job_id: SystemId,
|
||||
pub error: String,
|
||||
}
|
||||
|
||||
pub struct FailJobHandler {
|
||||
job_repo: Arc<dyn JobRepository>,
|
||||
batch_repo: Arc<dyn JobBatchRepository>,
|
||||
event_pub: Arc<dyn EventPublisher>,
|
||||
}
|
||||
|
||||
impl FailJobHandler {
|
||||
pub fn new(
|
||||
job_repo: Arc<dyn JobRepository>,
|
||||
batch_repo: Arc<dyn JobBatchRepository>,
|
||||
event_pub: Arc<dyn EventPublisher>,
|
||||
) -> Self {
|
||||
Self { job_repo, batch_repo, event_pub }
|
||||
}
|
||||
|
||||
pub async fn execute(&self, cmd: FailJobCommand) -> Result<Job, DomainError> {
|
||||
let mut job = self.job_repo.find_by_id(&cmd.job_id).await?
|
||||
.ok_or_else(|| DomainError::NotFound(format!("Job {} not found", cmd.job_id)))?;
|
||||
job.fail(&cmd.error);
|
||||
self.job_repo.save(&job).await?;
|
||||
if job.status == JobStatus::Failed {
|
||||
if let Some(ref batch_id) = job.batch_id {
|
||||
let mut batch = self.batch_repo.find_by_id(batch_id).await?
|
||||
.ok_or_else(|| DomainError::NotFound(format!("Batch {} not found", batch_id)))?;
|
||||
batch.record_failure();
|
||||
self.batch_repo.save(&batch).await?;
|
||||
}
|
||||
self.event_pub.publish(DomainEvent::JobFailed {
|
||||
job_id: job.job_id,
|
||||
error: cmd.error,
|
||||
timestamp: DateTimeStamp::now(),
|
||||
}).await?;
|
||||
} else if job.status == JobStatus::Queued {
|
||||
self.event_pub.publish(DomainEvent::JobEnqueued {
|
||||
job_id: job.job_id,
|
||||
job_type: format!("{:?}", job.job_type),
|
||||
timestamp: DateTimeStamp::now(),
|
||||
}).await?;
|
||||
}
|
||||
Ok(job)
|
||||
}
|
||||
}
|
||||
63
crates/application/src/processing/commands/manage_plugin.rs
Normal file
63
crates/application/src/processing/commands/manage_plugin.rs
Normal file
@@ -0,0 +1,63 @@
|
||||
use std::sync::Arc;
|
||||
use domain::{
|
||||
entities::{Plugin, PluginType},
|
||||
errors::DomainError,
|
||||
ports::PluginRepository,
|
||||
value_objects::{StructuredData, SystemId},
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub enum PluginAction {
|
||||
Create {
|
||||
name: String,
|
||||
plugin_type: PluginType,
|
||||
config: StructuredData,
|
||||
},
|
||||
Enable,
|
||||
Disable,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct ManagePluginCommand {
|
||||
pub plugin_id: Option<SystemId>,
|
||||
pub action: PluginAction,
|
||||
}
|
||||
|
||||
pub struct ManagePluginHandler {
|
||||
plugin_repo: Arc<dyn PluginRepository>,
|
||||
}
|
||||
|
||||
impl ManagePluginHandler {
|
||||
pub fn new(plugin_repo: Arc<dyn PluginRepository>) -> Self {
|
||||
Self { plugin_repo }
|
||||
}
|
||||
|
||||
pub async fn execute(&self, cmd: ManagePluginCommand) -> Result<Plugin, DomainError> {
|
||||
match cmd.action {
|
||||
PluginAction::Create { name, plugin_type, config } => {
|
||||
let mut plugin = Plugin::new(name, plugin_type);
|
||||
plugin.configuration = config;
|
||||
self.plugin_repo.save(&plugin).await?;
|
||||
Ok(plugin)
|
||||
}
|
||||
PluginAction::Enable => {
|
||||
let id = cmd.plugin_id
|
||||
.ok_or_else(|| DomainError::Validation("plugin_id required for Enable".into()))?;
|
||||
let mut plugin = self.plugin_repo.find_by_id(&id).await?
|
||||
.ok_or_else(|| DomainError::NotFound(format!("Plugin {} not found", id)))?;
|
||||
plugin.enable();
|
||||
self.plugin_repo.save(&plugin).await?;
|
||||
Ok(plugin)
|
||||
}
|
||||
PluginAction::Disable => {
|
||||
let id = cmd.plugin_id
|
||||
.ok_or_else(|| DomainError::Validation("plugin_id required for Disable".into()))?;
|
||||
let mut plugin = self.plugin_repo.find_by_id(&id).await?
|
||||
.ok_or_else(|| DomainError::NotFound(format!("Plugin {} not found", id)))?;
|
||||
plugin.disable();
|
||||
self.plugin_repo.save(&plugin).await?;
|
||||
Ok(plugin)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
6
crates/application/src/processing/commands/mod.rs
Normal file
6
crates/application/src/processing/commands/mod.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
pub mod enqueue_job;
|
||||
pub mod start_job;
|
||||
pub mod complete_job;
|
||||
pub mod fail_job;
|
||||
pub mod manage_plugin;
|
||||
pub mod configure_pipeline;
|
||||
30
crates/application/src/processing/commands/start_job.rs
Normal file
30
crates/application/src/processing/commands/start_job.rs
Normal file
@@ -0,0 +1,30 @@
|
||||
use std::sync::Arc;
|
||||
use domain::{
|
||||
entities::Job,
|
||||
errors::DomainError,
|
||||
ports::JobRepository,
|
||||
value_objects::SystemId,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct StartJobCommand {
|
||||
pub job_id: SystemId,
|
||||
}
|
||||
|
||||
pub struct StartJobHandler {
|
||||
job_repo: Arc<dyn JobRepository>,
|
||||
}
|
||||
|
||||
impl StartJobHandler {
|
||||
pub fn new(job_repo: Arc<dyn JobRepository>) -> Self {
|
||||
Self { job_repo }
|
||||
}
|
||||
|
||||
pub async fn execute(&self, cmd: StartJobCommand) -> Result<Job, DomainError> {
|
||||
let mut job = self.job_repo.find_by_id(&cmd.job_id).await?
|
||||
.ok_or_else(|| DomainError::NotFound(format!("Job {} not found", cmd.job_id)))?;
|
||||
job.start()?;
|
||||
self.job_repo.save(&job).await?;
|
||||
Ok(job)
|
||||
}
|
||||
}
|
||||
@@ -1 +1,10 @@
|
||||
// Processing commands/queries (future: EnqueueJob, ProcessBatch, etc.)
|
||||
pub mod commands;
|
||||
pub mod queries;
|
||||
|
||||
pub use commands::enqueue_job::{EnqueueJobCommand, EnqueueJobHandler};
|
||||
pub use commands::start_job::{StartJobCommand, StartJobHandler};
|
||||
pub use commands::complete_job::{CompleteJobCommand, CompleteJobHandler};
|
||||
pub use commands::fail_job::{FailJobCommand, FailJobHandler};
|
||||
pub use commands::manage_plugin::{ManagePluginCommand, ManagePluginHandler, PluginAction};
|
||||
pub use commands::configure_pipeline::{ConfigurePipelineCommand, ConfigurePipelineHandler, PipelineStepConfig};
|
||||
pub use queries::report_batch_progress::{ReportBatchProgressQuery, ReportBatchProgressHandler, BatchProgress};
|
||||
|
||||
1
crates/application/src/processing/queries/mod.rs
Normal file
1
crates/application/src/processing/queries/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod report_batch_progress;
|
||||
@@ -0,0 +1,36 @@
|
||||
use std::sync::Arc;
|
||||
use domain::{
|
||||
entities::{Job, JobBatch},
|
||||
errors::DomainError,
|
||||
ports::{JobBatchRepository, JobRepository},
|
||||
value_objects::SystemId,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct ReportBatchProgressQuery {
|
||||
pub batch_id: SystemId,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct BatchProgress {
|
||||
pub batch: JobBatch,
|
||||
pub jobs: Vec<Job>,
|
||||
}
|
||||
|
||||
pub struct ReportBatchProgressHandler {
|
||||
batch_repo: Arc<dyn JobBatchRepository>,
|
||||
job_repo: Arc<dyn JobRepository>,
|
||||
}
|
||||
|
||||
impl ReportBatchProgressHandler {
|
||||
pub fn new(batch_repo: Arc<dyn JobBatchRepository>, job_repo: Arc<dyn JobRepository>) -> Self {
|
||||
Self { batch_repo, job_repo }
|
||||
}
|
||||
|
||||
pub async fn execute(&self, query: ReportBatchProgressQuery) -> Result<BatchProgress, DomainError> {
|
||||
let batch = self.batch_repo.find_by_id(&query.batch_id).await?
|
||||
.ok_or_else(|| DomainError::NotFound(format!("Batch {} not found", query.batch_id)))?;
|
||||
let jobs = self.job_repo.find_by_batch(&query.batch_id).await?;
|
||||
Ok(BatchProgress { batch, jobs })
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
use std::sync::Arc;
|
||||
use domain::{
|
||||
entities::SyncStatus,
|
||||
errors::DomainError,
|
||||
ports::{SidecarRepository, SidecarWriterPort},
|
||||
};
|
||||
use crate::sidecar::hash_helper::hash_structured_data;
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct DetectExternalChangesCommand;
|
||||
|
||||
pub struct DetectExternalChangesHandler {
|
||||
sidecar_repo: Arc<dyn SidecarRepository>,
|
||||
writer: Arc<dyn SidecarWriterPort>,
|
||||
}
|
||||
|
||||
impl DetectExternalChangesHandler {
|
||||
pub fn new(
|
||||
sidecar_repo: Arc<dyn SidecarRepository>,
|
||||
writer: Arc<dyn SidecarWriterPort>,
|
||||
) -> Self {
|
||||
Self { sidecar_repo, writer }
|
||||
}
|
||||
|
||||
pub async fn execute(&self, _cmd: DetectExternalChangesCommand) -> Result<u32, DomainError> {
|
||||
let records = self.sidecar_repo.find_by_status(SyncStatus::InSync).await?;
|
||||
let mut changed = 0u32;
|
||||
|
||||
for mut record in records {
|
||||
match self.writer.read_sidecar(&record.sidecar_storage_path).await {
|
||||
Ok(data) => {
|
||||
let hash = hash_structured_data(&data);
|
||||
let differs = record.last_known_file_hash.as_ref() != Some(&hash);
|
||||
if differs {
|
||||
record.mark_pending_read();
|
||||
self.sidecar_repo.save(&record).await?;
|
||||
changed += 1;
|
||||
}
|
||||
}
|
||||
Err(_) => {
|
||||
record.mark_error("Sidecar file not found");
|
||||
self.sidecar_repo.save(&record).await?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(changed)
|
||||
}
|
||||
}
|
||||
48
crates/application/src/sidecar/commands/export_sidecar.rs
Normal file
48
crates/application/src/sidecar/commands/export_sidecar.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
use std::sync::Arc;
|
||||
use domain::{
|
||||
catalog::services::resolve_metadata,
|
||||
entities::SidecarRecord,
|
||||
errors::DomainError,
|
||||
ports::{AssetMetadataRepository, SidecarRepository, SidecarWriterPort},
|
||||
value_objects::SystemId,
|
||||
};
|
||||
use crate::sidecar::hash_helper::hash_structured_data;
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct ExportSidecarCommand {
|
||||
pub asset_id: SystemId,
|
||||
}
|
||||
|
||||
pub struct ExportSidecarHandler {
|
||||
metadata_repo: Arc<dyn AssetMetadataRepository>,
|
||||
sidecar_repo: Arc<dyn SidecarRepository>,
|
||||
writer: Arc<dyn SidecarWriterPort>,
|
||||
}
|
||||
|
||||
impl ExportSidecarHandler {
|
||||
pub fn new(
|
||||
metadata_repo: Arc<dyn AssetMetadataRepository>,
|
||||
sidecar_repo: Arc<dyn SidecarRepository>,
|
||||
writer: Arc<dyn SidecarWriterPort>,
|
||||
) -> Self {
|
||||
Self { metadata_repo, sidecar_repo, writer }
|
||||
}
|
||||
|
||||
pub async fn execute(&self, cmd: ExportSidecarCommand) -> Result<SidecarRecord, DomainError> {
|
||||
let layers = self.metadata_repo.find_by_asset(&cmd.asset_id).await?;
|
||||
let resolved = resolve_metadata(&layers);
|
||||
|
||||
let mut record = match self.sidecar_repo.find_by_asset(&cmd.asset_id).await? {
|
||||
Some(r) => r,
|
||||
None => SidecarRecord::new(cmd.asset_id, format!("sidecars/{}.xmp", cmd.asset_id)),
|
||||
};
|
||||
|
||||
self.writer.write_sidecar(&resolved, &record.sidecar_storage_path).await?;
|
||||
|
||||
let hash = hash_structured_data(&resolved);
|
||||
record.mark_synced(hash);
|
||||
self.sidecar_repo.save(&record).await?;
|
||||
|
||||
Ok(record)
|
||||
}
|
||||
}
|
||||
55
crates/application/src/sidecar/commands/full_export.rs
Normal file
55
crates/application/src/sidecar/commands/full_export.rs
Normal file
@@ -0,0 +1,55 @@
|
||||
use std::sync::Arc;
|
||||
use domain::{
|
||||
catalog::services::resolve_metadata,
|
||||
entities::SidecarRecord,
|
||||
errors::DomainError,
|
||||
ports::{AssetRepository, AssetMetadataRepository, SidecarRepository, SidecarWriterPort},
|
||||
value_objects::SystemId,
|
||||
};
|
||||
use crate::sidecar::hash_helper::hash_structured_data;
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct FullExportCommand {
|
||||
pub owner_id: SystemId,
|
||||
}
|
||||
|
||||
pub struct FullExportHandler {
|
||||
asset_repo: Arc<dyn AssetRepository>,
|
||||
metadata_repo: Arc<dyn AssetMetadataRepository>,
|
||||
sidecar_repo: Arc<dyn SidecarRepository>,
|
||||
writer: Arc<dyn SidecarWriterPort>,
|
||||
}
|
||||
|
||||
impl FullExportHandler {
|
||||
pub fn new(
|
||||
asset_repo: Arc<dyn AssetRepository>,
|
||||
metadata_repo: Arc<dyn AssetMetadataRepository>,
|
||||
sidecar_repo: Arc<dyn SidecarRepository>,
|
||||
writer: Arc<dyn SidecarWriterPort>,
|
||||
) -> Self {
|
||||
Self { asset_repo, metadata_repo, sidecar_repo, writer }
|
||||
}
|
||||
|
||||
pub async fn execute(&self, cmd: FullExportCommand) -> Result<u32, DomainError> {
|
||||
let assets = self.asset_repo.find_by_owner(&cmd.owner_id, u32::MAX, 0).await?;
|
||||
let mut count = 0u32;
|
||||
|
||||
for asset in &assets {
|
||||
let layers = self.metadata_repo.find_by_asset(&asset.asset_id).await?;
|
||||
let resolved = resolve_metadata(&layers);
|
||||
|
||||
let mut record = match self.sidecar_repo.find_by_asset(&asset.asset_id).await? {
|
||||
Some(r) => r,
|
||||
None => SidecarRecord::new(asset.asset_id, format!("sidecars/{}.xmp", asset.asset_id)),
|
||||
};
|
||||
|
||||
self.writer.write_sidecar(&resolved, &record.sidecar_storage_path).await?;
|
||||
let hash = hash_structured_data(&resolved);
|
||||
record.mark_synced(hash);
|
||||
self.sidecar_repo.save(&record).await?;
|
||||
count += 1;
|
||||
}
|
||||
|
||||
Ok(count)
|
||||
}
|
||||
}
|
||||
64
crates/application/src/sidecar/commands/full_import.rs
Normal file
64
crates/application/src/sidecar/commands/full_import.rs
Normal file
@@ -0,0 +1,64 @@
|
||||
use std::sync::Arc;
|
||||
use domain::{
|
||||
catalog::entities::{AssetMetadata, MetadataSource},
|
||||
entities::SidecarRecord,
|
||||
errors::DomainError,
|
||||
ports::{AssetRepository, AssetMetadataRepository, SidecarRepository, SidecarWriterPort},
|
||||
value_objects::SystemId,
|
||||
};
|
||||
use crate::sidecar::hash_helper::hash_structured_data;
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct FullImportCommand {
|
||||
pub owner_id: SystemId,
|
||||
}
|
||||
|
||||
pub struct FullImportHandler {
|
||||
asset_repo: Arc<dyn AssetRepository>,
|
||||
metadata_repo: Arc<dyn AssetMetadataRepository>,
|
||||
sidecar_repo: Arc<dyn SidecarRepository>,
|
||||
writer: Arc<dyn SidecarWriterPort>,
|
||||
}
|
||||
|
||||
impl FullImportHandler {
|
||||
pub fn new(
|
||||
asset_repo: Arc<dyn AssetRepository>,
|
||||
metadata_repo: Arc<dyn AssetMetadataRepository>,
|
||||
sidecar_repo: Arc<dyn SidecarRepository>,
|
||||
writer: Arc<dyn SidecarWriterPort>,
|
||||
) -> Self {
|
||||
Self { asset_repo, metadata_repo, sidecar_repo, writer }
|
||||
}
|
||||
|
||||
pub async fn execute(&self, cmd: FullImportCommand) -> Result<u32, DomainError> {
|
||||
let assets = self.asset_repo.find_by_owner(&cmd.owner_id, u32::MAX, 0).await?;
|
||||
let mut count = 0u32;
|
||||
|
||||
for asset in &assets {
|
||||
let record = match self.sidecar_repo.find_by_asset(&asset.asset_id).await? {
|
||||
Some(r) => r,
|
||||
None => {
|
||||
// No sidecar record — try creating one to read from
|
||||
SidecarRecord::new(asset.asset_id, format!("sidecars/{}.xmp", asset.asset_id))
|
||||
}
|
||||
};
|
||||
|
||||
match self.writer.read_sidecar(&record.sidecar_storage_path).await {
|
||||
Ok(data) => {
|
||||
let metadata = AssetMetadata::new(asset.asset_id, MetadataSource::ExifExtracted, data.clone());
|
||||
self.metadata_repo.save(&metadata).await?;
|
||||
let hash = hash_structured_data(&data);
|
||||
let mut record = record;
|
||||
record.mark_synced(hash);
|
||||
self.sidecar_repo.save(&record).await?;
|
||||
count += 1;
|
||||
}
|
||||
Err(_) => {
|
||||
// Sidecar file missing — skip
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(count)
|
||||
}
|
||||
}
|
||||
51
crates/application/src/sidecar/commands/import_sidecar.rs
Normal file
51
crates/application/src/sidecar/commands/import_sidecar.rs
Normal file
@@ -0,0 +1,51 @@
|
||||
use std::sync::Arc;
|
||||
use domain::{
|
||||
catalog::entities::{AssetMetadata, MetadataSource},
|
||||
entities::SyncStatus,
|
||||
errors::DomainError,
|
||||
ports::{AssetMetadataRepository, SidecarRepository, SidecarWriterPort},
|
||||
value_objects::SystemId,
|
||||
};
|
||||
use crate::sidecar::hash_helper::hash_structured_data;
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct ImportSidecarCommand {
|
||||
pub asset_id: SystemId,
|
||||
}
|
||||
|
||||
pub struct ImportSidecarHandler {
|
||||
sidecar_repo: Arc<dyn SidecarRepository>,
|
||||
writer: Arc<dyn SidecarWriterPort>,
|
||||
metadata_repo: Arc<dyn AssetMetadataRepository>,
|
||||
}
|
||||
|
||||
impl ImportSidecarHandler {
|
||||
pub fn new(
|
||||
sidecar_repo: Arc<dyn SidecarRepository>,
|
||||
writer: Arc<dyn SidecarWriterPort>,
|
||||
metadata_repo: Arc<dyn AssetMetadataRepository>,
|
||||
) -> Self {
|
||||
Self { sidecar_repo, writer, metadata_repo }
|
||||
}
|
||||
|
||||
pub async fn execute(&self, cmd: ImportSidecarCommand) -> Result<AssetMetadata, DomainError> {
|
||||
let mut record = self.sidecar_repo.find_by_asset(&cmd.asset_id).await?
|
||||
.ok_or_else(|| DomainError::NotFound(format!("Sidecar record for {} not found", cmd.asset_id)))?;
|
||||
|
||||
if record.sync_status != SyncStatus::PendingRead {
|
||||
return Err(DomainError::Validation(
|
||||
format!("Sidecar is not pending read (status: {:?})", record.sync_status),
|
||||
));
|
||||
}
|
||||
|
||||
let data = self.writer.read_sidecar(&record.sidecar_storage_path).await?;
|
||||
let metadata = AssetMetadata::new(cmd.asset_id, MetadataSource::ExifExtracted, data.clone());
|
||||
self.metadata_repo.save(&metadata).await?;
|
||||
|
||||
let hash = hash_structured_data(&data);
|
||||
record.mark_synced(hash);
|
||||
self.sidecar_repo.save(&record).await?;
|
||||
|
||||
Ok(metadata)
|
||||
}
|
||||
}
|
||||
6
crates/application/src/sidecar/commands/mod.rs
Normal file
6
crates/application/src/sidecar/commands/mod.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
pub mod export_sidecar;
|
||||
pub mod detect_external_changes;
|
||||
pub mod import_sidecar;
|
||||
pub mod resolve_conflict;
|
||||
pub mod full_export;
|
||||
pub mod full_import;
|
||||
66
crates/application/src/sidecar/commands/resolve_conflict.rs
Normal file
66
crates/application/src/sidecar/commands/resolve_conflict.rs
Normal file
@@ -0,0 +1,66 @@
|
||||
use std::sync::Arc;
|
||||
use domain::{
|
||||
catalog::entities::{AssetMetadata, MetadataSource},
|
||||
catalog::services::resolve_metadata,
|
||||
entities::{ConflictPolicy, SidecarRecord, SyncStatus},
|
||||
errors::DomainError,
|
||||
ports::{AssetMetadataRepository, SidecarRepository, SidecarWriterPort},
|
||||
value_objects::SystemId,
|
||||
};
|
||||
use crate::sidecar::hash_helper::hash_structured_data;
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct ResolveConflictCommand {
|
||||
pub asset_id: SystemId,
|
||||
pub policy: ConflictPolicy,
|
||||
}
|
||||
|
||||
pub struct ResolveConflictHandler {
|
||||
sidecar_repo: Arc<dyn SidecarRepository>,
|
||||
writer: Arc<dyn SidecarWriterPort>,
|
||||
metadata_repo: Arc<dyn AssetMetadataRepository>,
|
||||
}
|
||||
|
||||
impl ResolveConflictHandler {
|
||||
pub fn new(
|
||||
sidecar_repo: Arc<dyn SidecarRepository>,
|
||||
writer: Arc<dyn SidecarWriterPort>,
|
||||
metadata_repo: Arc<dyn AssetMetadataRepository>,
|
||||
) -> Self {
|
||||
Self { sidecar_repo, writer, metadata_repo }
|
||||
}
|
||||
|
||||
pub async fn execute(&self, cmd: ResolveConflictCommand) -> Result<SidecarRecord, DomainError> {
|
||||
let mut record = self.sidecar_repo.find_by_asset(&cmd.asset_id).await?
|
||||
.ok_or_else(|| DomainError::NotFound(format!("Sidecar record for {} not found", cmd.asset_id)))?;
|
||||
|
||||
if record.sync_status != SyncStatus::Conflict {
|
||||
return Err(DomainError::Validation(
|
||||
format!("Sidecar is not in conflict (status: {:?})", record.sync_status),
|
||||
));
|
||||
}
|
||||
|
||||
match cmd.policy {
|
||||
ConflictPolicy::DbWins => {
|
||||
let layers = self.metadata_repo.find_by_asset(&cmd.asset_id).await?;
|
||||
let resolved = resolve_metadata(&layers);
|
||||
self.writer.write_sidecar(&resolved, &record.sidecar_storage_path).await?;
|
||||
let hash = hash_structured_data(&resolved);
|
||||
record.mark_synced(hash);
|
||||
}
|
||||
ConflictPolicy::FileWins => {
|
||||
let data = self.writer.read_sidecar(&record.sidecar_storage_path).await?;
|
||||
let metadata = AssetMetadata::new(cmd.asset_id, MetadataSource::ExifExtracted, data.clone());
|
||||
self.metadata_repo.save(&metadata).await?;
|
||||
let hash = hash_structured_data(&data);
|
||||
record.mark_synced(hash);
|
||||
}
|
||||
ConflictPolicy::RequireUserDecision => {
|
||||
return Err(DomainError::Validation("Manual resolution required".to_string()));
|
||||
}
|
||||
}
|
||||
|
||||
self.sidecar_repo.save(&record).await?;
|
||||
Ok(record)
|
||||
}
|
||||
}
|
||||
11
crates/application/src/sidecar/hash_helper.rs
Normal file
11
crates/application/src/sidecar/hash_helper.rs
Normal file
@@ -0,0 +1,11 @@
|
||||
use domain::value_objects::{Checksum, StructuredData};
|
||||
use sha2::{Sha256, Digest};
|
||||
|
||||
pub fn hash_structured_data(data: &StructuredData) -> Checksum {
|
||||
let json = serde_json::to_string(data).unwrap_or_default();
|
||||
let mut hasher = Sha256::new();
|
||||
hasher.update(json.as_bytes());
|
||||
let result = hasher.finalize();
|
||||
let hex = format!("{:x}", result);
|
||||
Checksum::new(hex).expect("SHA-256 always produces valid 64-char hex")
|
||||
}
|
||||
@@ -1 +1,9 @@
|
||||
// Sidecar commands/queries (future: SyncSidecar, ExportMetadata, etc.)
|
||||
pub mod commands;
|
||||
pub mod hash_helper;
|
||||
|
||||
pub use commands::export_sidecar::{ExportSidecarCommand, ExportSidecarHandler};
|
||||
pub use commands::detect_external_changes::{DetectExternalChangesCommand, DetectExternalChangesHandler};
|
||||
pub use commands::import_sidecar::{ImportSidecarCommand, ImportSidecarHandler};
|
||||
pub use commands::resolve_conflict::{ResolveConflictCommand, ResolveConflictHandler};
|
||||
pub use commands::full_export::{FullExportCommand, FullExportHandler};
|
||||
pub use commands::full_import::{FullImportCommand, FullImportHandler};
|
||||
|
||||
@@ -109,6 +109,41 @@ impl SidecarWriterPort for StubSidecarWriter {
|
||||
}
|
||||
}
|
||||
|
||||
// --- InMemorySidecarWriter ---
|
||||
|
||||
pub struct InMemorySidecarWriter {
|
||||
data: Mutex<HashMap<String, StructuredData>>,
|
||||
}
|
||||
|
||||
impl InMemorySidecarWriter {
|
||||
pub fn new() -> Self {
|
||||
Self { data: Mutex::new(HashMap::new()) }
|
||||
}
|
||||
|
||||
pub async fn get(&self, path: &str) -> Option<StructuredData> {
|
||||
self.data.lock().await.get(path).cloned()
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for InMemorySidecarWriter {
|
||||
fn default() -> Self { Self::new() }
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl SidecarWriterPort for InMemorySidecarWriter {
|
||||
fn format_name(&self) -> &str { "in-memory" }
|
||||
|
||||
async fn write_sidecar(&self, data: &StructuredData, path: &str) -> Result<(), DomainError> {
|
||||
self.data.lock().await.insert(path.to_string(), data.clone());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn read_sidecar(&self, path: &str) -> Result<StructuredData, DomainError> {
|
||||
self.data.lock().await.get(path).cloned()
|
||||
.ok_or_else(|| DomainError::NotFound(format!("Sidecar not found: {path}")))
|
||||
}
|
||||
}
|
||||
|
||||
// --- StubPasswordHasher ---
|
||||
|
||||
pub struct StubPasswordHasher;
|
||||
|
||||
@@ -4,16 +4,18 @@ use tokio::sync::Mutex;
|
||||
use domain::{
|
||||
entities::{
|
||||
Album, Asset, AssetMetadata, AssetTag, DuplicateGroup, DuplicateStatus,
|
||||
Group, IngestSession, InviteCode, Job, JobStatus, LibraryPath,
|
||||
MetadataSource, QuotaDefinition, Role, ShareLink, ShareScope, ShareTarget,
|
||||
Group, IngestSession, InviteCode, Job, JobBatch, JobStatus, LibraryPath,
|
||||
MetadataSource, Plugin, ProcessingPipeline, QuotaDefinition, Role,
|
||||
ShareLink, ShareScope, ShareTarget, SidecarRecord, SyncStatus,
|
||||
StorageVolume, Tag, UsageLedgerEntry, UsageType, User,
|
||||
},
|
||||
errors::DomainError,
|
||||
ports::{
|
||||
AlbumRepository, AssetMetadataRepository, AssetRepository,
|
||||
DuplicateRepository, GroupRepository, IngestSessionRepository,
|
||||
JobRepository, LibraryPathRepository, QuotaRepository,
|
||||
RoleRepository, ShareRepository, StorageVolumeRepository,
|
||||
JobBatchRepository, JobRepository, LibraryPathRepository,
|
||||
PipelineRepository, PluginRepository, QuotaRepository,
|
||||
RoleRepository, ShareRepository, SidecarRepository, StorageVolumeRepository,
|
||||
TagRepository, UsageLedgerRepository, UserRepository,
|
||||
},
|
||||
value_objects::{Checksum, DateTimeStamp, Email, SystemId},
|
||||
@@ -716,3 +718,141 @@ impl DuplicateRepository for InMemoryDuplicateRepository {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
// --- InMemorySidecarRepository ---
|
||||
|
||||
pub struct InMemorySidecarRepository {
|
||||
data: Mutex<HashMap<String, SidecarRecord>>,
|
||||
}
|
||||
|
||||
impl InMemorySidecarRepository {
|
||||
pub fn new() -> Self {
|
||||
Self { data: Mutex::new(HashMap::new()) }
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for InMemorySidecarRepository {
|
||||
fn default() -> Self { Self::new() }
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl SidecarRepository for InMemorySidecarRepository {
|
||||
async fn find_by_asset(&self, asset_id: &SystemId) -> Result<Option<SidecarRecord>, DomainError> {
|
||||
Ok(self.data.lock().await.get(&asset_id.to_string()).cloned())
|
||||
}
|
||||
|
||||
async fn find_by_status(&self, status: SyncStatus) -> Result<Vec<SidecarRecord>, DomainError> {
|
||||
Ok(self.data.lock().await.values()
|
||||
.filter(|r| r.sync_status == status)
|
||||
.cloned()
|
||||
.collect())
|
||||
}
|
||||
|
||||
async fn save(&self, record: &SidecarRecord) -> Result<(), DomainError> {
|
||||
self.data.lock().await.insert(record.asset_id.to_string(), record.clone());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn delete(&self, asset_id: &SystemId) -> Result<(), DomainError> {
|
||||
self.data.lock().await.remove(&asset_id.to_string());
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
// --- InMemoryJobBatchRepository ---
|
||||
|
||||
pub struct InMemoryJobBatchRepository {
|
||||
data: Mutex<HashMap<String, JobBatch>>,
|
||||
}
|
||||
|
||||
impl InMemoryJobBatchRepository {
|
||||
pub fn new() -> Self {
|
||||
Self { data: Mutex::new(HashMap::new()) }
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for InMemoryJobBatchRepository {
|
||||
fn default() -> Self { Self::new() }
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl JobBatchRepository for InMemoryJobBatchRepository {
|
||||
async fn find_by_id(&self, id: &SystemId) -> Result<Option<JobBatch>, DomainError> {
|
||||
Ok(self.data.lock().await.get(&id.to_string()).cloned())
|
||||
}
|
||||
|
||||
async fn save(&self, batch: &JobBatch) -> Result<(), DomainError> {
|
||||
self.data.lock().await.insert(batch.batch_id.to_string(), batch.clone());
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
// --- InMemoryPluginRepository ---
|
||||
|
||||
pub struct InMemoryPluginRepository {
|
||||
data: Mutex<HashMap<String, Plugin>>,
|
||||
}
|
||||
|
||||
impl InMemoryPluginRepository {
|
||||
pub fn new() -> Self {
|
||||
Self { data: Mutex::new(HashMap::new()) }
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for InMemoryPluginRepository {
|
||||
fn default() -> Self { Self::new() }
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl PluginRepository for InMemoryPluginRepository {
|
||||
async fn find_by_id(&self, id: &SystemId) -> Result<Option<Plugin>, DomainError> {
|
||||
Ok(self.data.lock().await.get(&id.to_string()).cloned())
|
||||
}
|
||||
|
||||
async fn find_enabled(&self) -> Result<Vec<Plugin>, DomainError> {
|
||||
Ok(self.data.lock().await.values()
|
||||
.filter(|p| p.is_enabled)
|
||||
.cloned()
|
||||
.collect())
|
||||
}
|
||||
|
||||
async fn save(&self, plugin: &Plugin) -> Result<(), DomainError> {
|
||||
self.data.lock().await.insert(plugin.plugin_id.to_string(), plugin.clone());
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
// --- InMemoryPipelineRepository ---
|
||||
|
||||
pub struct InMemoryPipelineRepository {
|
||||
data: Mutex<HashMap<String, ProcessingPipeline>>,
|
||||
}
|
||||
|
||||
impl InMemoryPipelineRepository {
|
||||
pub fn new() -> Self {
|
||||
Self { data: Mutex::new(HashMap::new()) }
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for InMemoryPipelineRepository {
|
||||
fn default() -> Self { Self::new() }
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl PipelineRepository for InMemoryPipelineRepository {
|
||||
async fn find_by_id(&self, id: &SystemId) -> Result<Option<ProcessingPipeline>, DomainError> {
|
||||
Ok(self.data.lock().await.get(&id.to_string()).cloned())
|
||||
}
|
||||
|
||||
async fn find_by_trigger(&self, event: &str) -> Result<Vec<ProcessingPipeline>, DomainError> {
|
||||
Ok(self.data.lock().await.values()
|
||||
.filter(|p| p.trigger_event == event)
|
||||
.cloned()
|
||||
.collect())
|
||||
}
|
||||
|
||||
async fn save(&self, pipeline: &ProcessingPipeline) -> Result<(), DomainError> {
|
||||
self.data.lock().await.insert(pipeline.pipeline_id.to_string(), pipeline.clone());
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user