Skip to main content

text_document_common/direct_access/use_cases/
remove.rs

1// Generated by Qleany v1.4.8 from common_da_use_cases_remove.tera
2
3use super::traits::WriteUoWFactory;
4use crate::snapshot::EntityTreeSnapshot;
5use crate::types::EntityId;
6use crate::undo_redo::UndoRedoCommand;
7use anyhow::{Ok, Result};
8use std::any::Any;
9
10// ---------------------------------------------------------------------------
11// Non-undoable remove
12// ---------------------------------------------------------------------------
13
14pub struct RemoveUseCase<F: WriteUoWFactory> {
15    uow_factory: F,
16}
17
18impl<F: WriteUoWFactory> RemoveUseCase<F> {
19    pub fn new(uow_factory: F) -> Self {
20        RemoveUseCase { uow_factory }
21    }
22
23    pub fn execute(&mut self, id: &EntityId) -> Result<()> {
24        self.execute_multi(&[*id])
25    }
26
27    pub fn execute_multi(&mut self, ids: &[EntityId]) -> Result<()> {
28        let mut uow = self.uow_factory.create();
29        uow.begin_transaction()?;
30        for id in ids {
31            if uow.get(id)?.is_none() {
32                return Err(anyhow::anyhow!("Entity with id {} does not exist", id));
33            }
34        }
35        uow.remove_multi(ids)?;
36        uow.commit()?;
37        Ok(())
38    }
39}
40
41// ---------------------------------------------------------------------------
42// Undoable remove
43// ---------------------------------------------------------------------------
44
45pub struct UndoableRemoveUseCase<F: WriteUoWFactory> {
46    uow_factory: F,
47    entity_tree_snapshot: Option<EntityTreeSnapshot>,
48    original_ids: Option<Vec<EntityId>>,
49}
50
51impl<F: WriteUoWFactory> UndoableRemoveUseCase<F> {
52    pub fn new(uow_factory: F) -> Self {
53        UndoableRemoveUseCase {
54            uow_factory,
55            entity_tree_snapshot: None,
56            original_ids: None,
57        }
58    }
59
60    pub fn execute(&mut self, id: &EntityId) -> Result<()> {
61        self.execute_multi(&[*id])
62    }
63
64    pub fn execute_multi(&mut self, ids: &[EntityId]) -> Result<()> {
65        let mut uow = self.uow_factory.create();
66        uow.begin_transaction()?;
67        for id in ids {
68            if uow.get(id)?.is_none() {
69                return Err(anyhow::anyhow!("Entity with id {} does not exist", id));
70            }
71        }
72        self.entity_tree_snapshot = Some(uow.snapshot(ids)?);
73        uow.remove_multi(ids)?;
74        uow.commit()?;
75        self.original_ids = Some(ids.to_vec());
76        Ok(())
77    }
78}
79
80impl<F: WriteUoWFactory + Send + 'static> UndoRedoCommand for UndoableRemoveUseCase<F>
81where
82    F::Entity: 'static,
83{
84    fn undo(&mut self) -> Result<()> {
85        if let Some(ref snap) = self.entity_tree_snapshot {
86            let mut uow = self.uow_factory.create();
87            uow.begin_transaction()?;
88            uow.restore(snap)?;
89            uow.commit()?;
90        }
91        Ok(())
92    }
93
94    fn redo(&mut self) -> Result<()> {
95        if let Some(ref ids) = self.original_ids {
96            let mut uow = self.uow_factory.create();
97            uow.begin_transaction()?;
98            self.entity_tree_snapshot = Some(uow.snapshot(ids)?);
99            uow.remove_multi(ids)?;
100            uow.commit()?;
101        }
102        Ok(())
103    }
104
105    fn as_any(&self) -> &dyn Any {
106        self
107    }
108}