text_document_direct_access/block/
block_controller.rs1use super::{
4 dtos::{BlockDto, CreateBlockDto, UpdateBlockDto},
5 units_of_work::{BlockReadUoWFactory, BlockWriteUoWFactory},
6};
7use crate::BlockRelationshipDto;
8use anyhow::{Ok, Result};
9use common::direct_access::block::BlockRelationshipField;
10use common::direct_access::use_cases;
11use common::undo_redo::UndoRedoManager;
12use common::{database::db_context::DbContext, event::EventHub, types::EntityId};
13use std::sync::Arc;
14
15pub fn create_orphan(
16 db_context: &DbContext,
17 event_hub: &Arc<EventHub>,
18 undo_redo_manager: &mut UndoRedoManager,
19 stack_id: Option<u64>,
20 entity: &CreateBlockDto,
21) -> Result<BlockDto> {
22 let uow_factory = BlockWriteUoWFactory::new(db_context, event_hub);
23 let mut uc = use_cases::UndoableCreateOrphanUseCase::new(uow_factory);
24 let entity_in: common::entities::Block = entity.into();
25 let result = uc.execute(&entity_in)?;
26 undo_redo_manager.add_command_to_stack(Box::new(uc), stack_id)?;
27 Ok(result.into())
28}
29
30pub fn create_orphan_multi(
31 db_context: &DbContext,
32 event_hub: &Arc<EventHub>,
33 undo_redo_manager: &mut UndoRedoManager,
34 stack_id: Option<u64>,
35 entities: &[CreateBlockDto],
36) -> Result<Vec<BlockDto>> {
37 let uow_factory = BlockWriteUoWFactory::new(db_context, event_hub);
38 let entities_in: Vec<common::entities::Block> = entities.iter().map(|dto| dto.into()).collect();
39 let mut uc = use_cases::UndoableCreateOrphanUseCase::new(uow_factory);
40 let result = uc.execute_multi(&entities_in)?;
41 undo_redo_manager.add_command_to_stack(Box::new(uc), stack_id)?;
42 Ok(result.into_iter().map(|e| e.into()).collect())
43}
44
45pub fn create(
46 db_context: &DbContext,
47 event_hub: &Arc<EventHub>,
48 undo_redo_manager: &mut UndoRedoManager,
49 stack_id: Option<u64>,
50 entity: &CreateBlockDto,
51 owner_id: EntityId,
52 index: i32,
53) -> Result<BlockDto> {
54 let uow_factory = BlockWriteUoWFactory::new(db_context, event_hub);
55 let entity_in: common::entities::Block = entity.into();
56 let strategy = use_cases::OwnerStrategy::Appending;
57 let mut uc = use_cases::UndoableCreateUseCase::new(uow_factory, strategy);
58 let result = uc.execute(&entity_in, owner_id, index)?;
59 undo_redo_manager.add_command_to_stack(Box::new(uc), stack_id)?;
60 Ok(result.into())
61}
62
63pub fn create_multi(
64 db_context: &DbContext,
65 event_hub: &Arc<EventHub>,
66 undo_redo_manager: &mut UndoRedoManager,
67 stack_id: Option<u64>,
68 entities: &[CreateBlockDto],
69 owner_id: EntityId,
70 index: i32,
71) -> Result<Vec<BlockDto>> {
72 let uow_factory = BlockWriteUoWFactory::new(db_context, event_hub);
73 let entities_in: Vec<common::entities::Block> = entities.iter().map(|dto| dto.into()).collect();
74 let strategy = use_cases::OwnerStrategy::Appending;
75 let mut uc = use_cases::UndoableCreateUseCase::new(uow_factory, strategy);
76 let result = uc.execute_multi(&entities_in, owner_id, index)?;
77 undo_redo_manager.add_command_to_stack(Box::new(uc), stack_id)?;
78 Ok(result.into_iter().map(|e| e.into()).collect())
79}
80
81pub fn get(db_context: &DbContext, id: &EntityId) -> Result<Option<BlockDto>> {
82 let uow_factory = BlockReadUoWFactory::new(db_context);
83 let uc = use_cases::GetUseCase::new(uow_factory);
84 Ok(uc.execute(id)?.map(|e| e.into()))
85}
86
87pub fn get_all(db_context: &DbContext) -> Result<Vec<BlockDto>> {
88 let uow_factory = BlockReadUoWFactory::new(db_context);
89 let uc = use_cases::GetUseCase::new(uow_factory);
90 Ok(uc.execute_all()?.into_iter().map(|e| e.into()).collect())
91}
92
93pub fn get_multi(db_context: &DbContext, ids: &[EntityId]) -> Result<Vec<Option<BlockDto>>> {
94 let uow_factory = BlockReadUoWFactory::new(db_context);
95 let uc = use_cases::GetUseCase::new(uow_factory);
96 Ok(uc
97 .execute_multi(ids)?
98 .into_iter()
99 .map(|o| o.map(|e| e.into()))
100 .collect())
101}
102
103pub fn update(
104 db_context: &DbContext,
105 event_hub: &Arc<EventHub>,
106 undo_redo_manager: &mut UndoRedoManager,
107 stack_id: Option<u64>,
108 entity: &UpdateBlockDto,
109) -> Result<BlockDto> {
110 let uow_factory = BlockWriteUoWFactory::new(db_context, event_hub);
111 let entity_in: common::entities::Block = entity.into();
112 let mut uc = use_cases::UndoableUpdateUseCase::new(uow_factory);
113 let result = uc.execute(&entity_in)?;
114 undo_redo_manager.add_command_to_stack(Box::new(uc), stack_id)?;
115 Ok(result.into())
116}
117
118pub fn update_multi(
119 db_context: &DbContext,
120 event_hub: &Arc<EventHub>,
121 undo_redo_manager: &mut UndoRedoManager,
122 stack_id: Option<u64>,
123 entities: &[UpdateBlockDto],
124) -> Result<Vec<BlockDto>> {
125 let uow_factory = BlockWriteUoWFactory::new(db_context, event_hub);
126 let entities_in: Vec<common::entities::Block> = entities.iter().map(|dto| dto.into()).collect();
127 let mut uc = use_cases::UndoableUpdateUseCase::new(uow_factory);
128 let result = uc.execute_multi(&entities_in)?;
129 undo_redo_manager.add_command_to_stack(Box::new(uc), stack_id)?;
130 Ok(result.into_iter().map(|e| e.into()).collect())
131}
132
133pub fn update_with_relationships(
134 db_context: &DbContext,
135 event_hub: &Arc<EventHub>,
136 undo_redo_manager: &mut UndoRedoManager,
137 stack_id: Option<u64>,
138 entity: &BlockDto,
139) -> Result<BlockDto> {
140 let uow_factory = BlockWriteUoWFactory::new(db_context, event_hub);
141 let entity_in: common::entities::Block = entity.into();
142 let mut uc = use_cases::UndoableUpdateWithRelationshipsUseCase::new(uow_factory);
143 let result = uc.execute(&entity_in)?;
144 undo_redo_manager.add_command_to_stack(Box::new(uc), stack_id)?;
145 Ok(result.into())
146}
147
148pub fn update_with_relationships_multi(
149 db_context: &DbContext,
150 event_hub: &Arc<EventHub>,
151 undo_redo_manager: &mut UndoRedoManager,
152 stack_id: Option<u64>,
153 entities: &[BlockDto],
154) -> Result<Vec<BlockDto>> {
155 let uow_factory = BlockWriteUoWFactory::new(db_context, event_hub);
156 let entities_in: Vec<common::entities::Block> = entities.iter().map(|dto| dto.into()).collect();
157 let mut uc = use_cases::UndoableUpdateWithRelationshipsUseCase::new(uow_factory);
158 let result = uc.execute_multi(&entities_in)?;
159 undo_redo_manager.add_command_to_stack(Box::new(uc), stack_id)?;
160 Ok(result.into_iter().map(|e| e.into()).collect())
161}
162
163pub fn remove(
164 db_context: &DbContext,
165 event_hub: &Arc<EventHub>,
166 undo_redo_manager: &mut UndoRedoManager,
167 stack_id: Option<u64>,
168 id: &EntityId,
169) -> Result<()> {
170 let uow_factory = BlockWriteUoWFactory::new(db_context, event_hub);
171 let mut uc = use_cases::UndoableRemoveUseCase::new(uow_factory);
172 uc.execute(id)?;
173 undo_redo_manager.add_command_to_stack(Box::new(uc), stack_id)?;
174 Ok(())
175}
176
177pub fn remove_multi(
178 db_context: &DbContext,
179 event_hub: &Arc<EventHub>,
180 undo_redo_manager: &mut UndoRedoManager,
181 stack_id: Option<u64>,
182 ids: &[EntityId],
183) -> Result<()> {
184 let uow_factory = BlockWriteUoWFactory::new(db_context, event_hub);
185 let mut uc = use_cases::UndoableRemoveUseCase::new(uow_factory);
186 uc.execute_multi(ids)?;
187 undo_redo_manager.add_command_to_stack(Box::new(uc), stack_id)?;
188 Ok(())
189}
190
191pub fn get_relationship(
192 db_context: &DbContext,
193 id: &EntityId,
194 field: &BlockRelationshipField,
195) -> Result<Vec<EntityId>> {
196 let uow_factory = BlockReadUoWFactory::new(db_context);
197 let uc = use_cases::GetRelationshipUseCase::<BlockRelationshipField, _>::new(uow_factory);
198 uc.execute(id, field)
199}
200
201pub fn get_relationship_many(
202 db_context: &DbContext,
203 ids: &[EntityId],
204 field: &BlockRelationshipField,
205) -> Result<std::collections::HashMap<EntityId, Vec<EntityId>>> {
206 let uow_factory = BlockReadUoWFactory::new(db_context);
207 let uc = use_cases::GetRelationshipManyUseCase::<BlockRelationshipField, _>::new(uow_factory);
208 uc.execute(ids, field)
209}
210
211pub fn get_relationship_count(
212 db_context: &DbContext,
213 id: &EntityId,
214 field: &BlockRelationshipField,
215) -> Result<usize> {
216 let uow_factory = BlockReadUoWFactory::new(db_context);
217 let uc = use_cases::GetRelationshipCountUseCase::<BlockRelationshipField, _>::new(uow_factory);
218 uc.execute(id, field)
219}
220
221pub fn get_relationship_in_range(
222 db_context: &DbContext,
223 id: &EntityId,
224 field: &BlockRelationshipField,
225 offset: usize,
226 limit: usize,
227) -> Result<Vec<EntityId>> {
228 let uow_factory = BlockReadUoWFactory::new(db_context);
229 let uc =
230 use_cases::GetRelationshipInRangeUseCase::<BlockRelationshipField, _>::new(uow_factory);
231 uc.execute(id, field, offset, limit)
232}
233
234pub fn set_relationship(
235 db_context: &DbContext,
236 event_hub: &Arc<EventHub>,
237 undo_redo_manager: &mut UndoRedoManager,
238 stack_id: Option<u64>,
239 dto: &BlockRelationshipDto,
240) -> Result<()> {
241 let uow_factory = BlockWriteUoWFactory::new(db_context, event_hub);
242 let mut uc =
243 use_cases::UndoableSetRelationshipUseCase::<BlockRelationshipField, _>::new(uow_factory);
244 uc.execute(&dto.id, &dto.field, dto.right_ids.as_slice())?;
245 undo_redo_manager.add_command_to_stack(Box::new(uc), stack_id)?;
246 Ok(())
247}
248
249#[allow(clippy::too_many_arguments)]
250pub fn move_relationship(
251 db_context: &DbContext,
252 event_hub: &Arc<EventHub>,
253 undo_redo_manager: &mut UndoRedoManager,
254 stack_id: Option<u64>,
255 id: &EntityId,
256 field: &BlockRelationshipField,
257 ids_to_move: &[EntityId],
258 new_index: i32,
259) -> Result<Vec<EntityId>> {
260 let uow_factory = BlockWriteUoWFactory::new(db_context, event_hub);
261 let mut uc =
262 use_cases::UndoableMoveRelationshipUseCase::<BlockRelationshipField, _>::new(uow_factory);
263 let result = uc.execute(id, field, ids_to_move, new_index)?;
264 undo_redo_manager.add_command_to_stack(Box::new(uc), stack_id)?;
265 Ok(result)
266}