Skip to main content

text_document_direct_access/list/
list_controller.rs

1// Generated by Qleany v1.5.0 from entity_controller.tera
2
3use super::{
4    dtos::{CreateListDto, ListDto, UpdateListDto},
5    units_of_work::{ListReadUoWFactory, ListWriteUoWFactory},
6};
7use anyhow::{Ok, Result};
8use common::direct_access::use_cases;
9use common::undo_redo::UndoRedoManager;
10use common::{database::db_context::DbContext, event::EventHub, types::EntityId};
11use std::sync::Arc;
12
13pub fn create_orphan(
14    db_context: &DbContext,
15    event_hub: &Arc<EventHub>,
16    undo_redo_manager: &mut UndoRedoManager,
17    stack_id: Option<u64>,
18    entity: &CreateListDto,
19) -> Result<ListDto> {
20    let uow_factory = ListWriteUoWFactory::new(db_context, event_hub);
21    let mut uc = use_cases::UndoableCreateOrphanUseCase::new(uow_factory);
22    let entity_in: common::entities::List = entity.into();
23    let result = uc.execute(&entity_in)?;
24    undo_redo_manager.add_command_to_stack(Box::new(uc), stack_id)?;
25    Ok(result.into())
26}
27
28pub fn create_orphan_multi(
29    db_context: &DbContext,
30    event_hub: &Arc<EventHub>,
31    undo_redo_manager: &mut UndoRedoManager,
32    stack_id: Option<u64>,
33    entities: &[CreateListDto],
34) -> Result<Vec<ListDto>> {
35    let uow_factory = ListWriteUoWFactory::new(db_context, event_hub);
36    let entities_in: Vec<common::entities::List> = entities.iter().map(|dto| dto.into()).collect();
37    let mut uc = use_cases::UndoableCreateOrphanUseCase::new(uow_factory);
38    let result = uc.execute_multi(&entities_in)?;
39    undo_redo_manager.add_command_to_stack(Box::new(uc), stack_id)?;
40    Ok(result.into_iter().map(|e| e.into()).collect())
41}
42
43pub fn create(
44    db_context: &DbContext,
45    event_hub: &Arc<EventHub>,
46    undo_redo_manager: &mut UndoRedoManager,
47    stack_id: Option<u64>,
48    entity: &CreateListDto,
49    owner_id: EntityId,
50    index: i32,
51) -> Result<ListDto> {
52    let uow_factory = ListWriteUoWFactory::new(db_context, event_hub);
53    let entity_in: common::entities::List = entity.into();
54    let strategy = use_cases::OwnerStrategy::Appending;
55    let mut uc = use_cases::UndoableCreateUseCase::new(uow_factory, strategy);
56    let result = uc.execute(&entity_in, owner_id, index)?;
57    undo_redo_manager.add_command_to_stack(Box::new(uc), stack_id)?;
58    Ok(result.into())
59}
60
61pub fn create_multi(
62    db_context: &DbContext,
63    event_hub: &Arc<EventHub>,
64    undo_redo_manager: &mut UndoRedoManager,
65    stack_id: Option<u64>,
66    entities: &[CreateListDto],
67    owner_id: EntityId,
68    index: i32,
69) -> Result<Vec<ListDto>> {
70    let uow_factory = ListWriteUoWFactory::new(db_context, event_hub);
71    let entities_in: Vec<common::entities::List> = entities.iter().map(|dto| dto.into()).collect();
72    let strategy = use_cases::OwnerStrategy::Appending;
73    let mut uc = use_cases::UndoableCreateUseCase::new(uow_factory, strategy);
74    let result = uc.execute_multi(&entities_in, owner_id, index)?;
75    undo_redo_manager.add_command_to_stack(Box::new(uc), stack_id)?;
76    Ok(result.into_iter().map(|e| e.into()).collect())
77}
78
79pub fn get(db_context: &DbContext, id: &EntityId) -> Result<Option<ListDto>> {
80    let uow_factory = ListReadUoWFactory::new(db_context);
81    let uc = use_cases::GetUseCase::new(uow_factory);
82    Ok(uc.execute(id)?.map(|e| e.into()))
83}
84
85pub fn get_all(db_context: &DbContext) -> Result<Vec<ListDto>> {
86    let uow_factory = ListReadUoWFactory::new(db_context);
87    let uc = use_cases::GetUseCase::new(uow_factory);
88    Ok(uc.execute_all()?.into_iter().map(|e| e.into()).collect())
89}
90
91pub fn get_multi(db_context: &DbContext, ids: &[EntityId]) -> Result<Vec<Option<ListDto>>> {
92    let uow_factory = ListReadUoWFactory::new(db_context);
93    let uc = use_cases::GetUseCase::new(uow_factory);
94    Ok(uc
95        .execute_multi(ids)?
96        .into_iter()
97        .map(|o| o.map(|e| e.into()))
98        .collect())
99}
100
101pub fn update(
102    db_context: &DbContext,
103    event_hub: &Arc<EventHub>,
104    undo_redo_manager: &mut UndoRedoManager,
105    stack_id: Option<u64>,
106    entity: &UpdateListDto,
107) -> Result<ListDto> {
108    let uow_factory = ListWriteUoWFactory::new(db_context, event_hub);
109    let entity_in: common::entities::List = entity.into();
110    let mut uc = use_cases::UndoableUpdateUseCase::new(uow_factory);
111    let result = uc.execute(&entity_in)?;
112    undo_redo_manager.add_command_to_stack(Box::new(uc), stack_id)?;
113    Ok(result.into())
114}
115
116pub fn update_multi(
117    db_context: &DbContext,
118    event_hub: &Arc<EventHub>,
119    undo_redo_manager: &mut UndoRedoManager,
120    stack_id: Option<u64>,
121    entities: &[UpdateListDto],
122) -> Result<Vec<ListDto>> {
123    let uow_factory = ListWriteUoWFactory::new(db_context, event_hub);
124    let entities_in: Vec<common::entities::List> = entities.iter().map(|dto| dto.into()).collect();
125    let mut uc = use_cases::UndoableUpdateUseCase::new(uow_factory);
126    let result = uc.execute_multi(&entities_in)?;
127    undo_redo_manager.add_command_to_stack(Box::new(uc), stack_id)?;
128    Ok(result.into_iter().map(|e| e.into()).collect())
129}
130
131pub fn update_with_relationships(
132    db_context: &DbContext,
133    event_hub: &Arc<EventHub>,
134    undo_redo_manager: &mut UndoRedoManager,
135    stack_id: Option<u64>,
136    entity: &ListDto,
137) -> Result<ListDto> {
138    let uow_factory = ListWriteUoWFactory::new(db_context, event_hub);
139    let entity_in: common::entities::List = entity.into();
140    let mut uc = use_cases::UndoableUpdateWithRelationshipsUseCase::new(uow_factory);
141    let result = uc.execute(&entity_in)?;
142    undo_redo_manager.add_command_to_stack(Box::new(uc), stack_id)?;
143    Ok(result.into())
144}
145
146pub fn update_with_relationships_multi(
147    db_context: &DbContext,
148    event_hub: &Arc<EventHub>,
149    undo_redo_manager: &mut UndoRedoManager,
150    stack_id: Option<u64>,
151    entities: &[ListDto],
152) -> Result<Vec<ListDto>> {
153    let uow_factory = ListWriteUoWFactory::new(db_context, event_hub);
154    let entities_in: Vec<common::entities::List> = entities.iter().map(|dto| dto.into()).collect();
155    let mut uc = use_cases::UndoableUpdateWithRelationshipsUseCase::new(uow_factory);
156    let result = uc.execute_multi(&entities_in)?;
157    undo_redo_manager.add_command_to_stack(Box::new(uc), stack_id)?;
158    Ok(result.into_iter().map(|e| e.into()).collect())
159}
160
161pub fn remove(
162    db_context: &DbContext,
163    event_hub: &Arc<EventHub>,
164    undo_redo_manager: &mut UndoRedoManager,
165    stack_id: Option<u64>,
166    id: &EntityId,
167) -> Result<()> {
168    let uow_factory = ListWriteUoWFactory::new(db_context, event_hub);
169    let mut uc = use_cases::UndoableRemoveUseCase::new(uow_factory);
170    uc.execute(id)?;
171    undo_redo_manager.add_command_to_stack(Box::new(uc), stack_id)?;
172    Ok(())
173}
174
175pub fn remove_multi(
176    db_context: &DbContext,
177    event_hub: &Arc<EventHub>,
178    undo_redo_manager: &mut UndoRedoManager,
179    stack_id: Option<u64>,
180    ids: &[EntityId],
181) -> Result<()> {
182    let uow_factory = ListWriteUoWFactory::new(db_context, event_hub);
183    let mut uc = use_cases::UndoableRemoveUseCase::new(uow_factory);
184    uc.execute_multi(ids)?;
185    undo_redo_manager.add_command_to_stack(Box::new(uc), stack_id)?;
186    Ok(())
187}