application: config_snapshots, admin, providers, iptv
This commit is contained in:
43
crates/application/src/config_snapshots/restore.rs
Normal file
43
crates/application/src/config_snapshots/restore.rs
Normal file
@@ -0,0 +1,43 @@
|
||||
use domain::models::Channel;
|
||||
use domain::value_objects::ChannelId;
|
||||
use domain::{DomainError, DomainResult};
|
||||
|
||||
use super::commands::RestoreSnapshotCommand;
|
||||
use super::deps::ConfigSnapshotDeps;
|
||||
|
||||
/// Restore a channel's config from a snapshot.
|
||||
///
|
||||
/// Flow: find snapshot -> find channel -> snapshot current config (backup) ->
|
||||
/// apply snapshot config to channel -> save channel -> return updated channel.
|
||||
pub async fn execute(
|
||||
deps: &ConfigSnapshotDeps,
|
||||
cmd: RestoreSnapshotCommand,
|
||||
) -> DomainResult<Channel> {
|
||||
let channel_id = ChannelId::from(cmd.channel_id);
|
||||
|
||||
let snapshot = deps
|
||||
.channel_query
|
||||
.get_config_snapshot(channel_id, cmd.snapshot_id)
|
||||
.await?
|
||||
.ok_or(DomainError::ValidationError(format!(
|
||||
"Snapshot {} not found",
|
||||
cmd.snapshot_id
|
||||
)))?;
|
||||
|
||||
let mut channel = deps
|
||||
.channel_query
|
||||
.find_by_id(channel_id)
|
||||
.await?
|
||||
.ok_or(DomainError::ChannelNotFound(cmd.channel_id))?;
|
||||
|
||||
// Auto-snapshot the current config before overwriting
|
||||
deps.channel_command
|
||||
.save_config_snapshot(channel_id, channel.schedule_config(), None)
|
||||
.await?;
|
||||
|
||||
// Apply the snapshot's config
|
||||
channel.set_schedule_config(snapshot.config().clone());
|
||||
deps.channel_command.save(&channel).await?;
|
||||
|
||||
Ok(channel)
|
||||
}
|
||||
Reference in New Issue
Block a user