text_document_direct_access/root/
root_controller.rs1use super::{
4 dtos::{CreateRootDto, RootDto, UpdateRootDto},
5 units_of_work::{RootReadUoWFactory, RootWriteUoWFactory},
6};
7use crate::RootRelationshipDto;
8use anyhow::{Ok, Result};
9use common::direct_access::root::RootRelationshipField;
10use common::direct_access::use_cases;
11
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 entity: &CreateRootDto,
19) -> Result<RootDto> {
20 let uow_factory = RootWriteUoWFactory::new(db_context, event_hub);
21 let mut uc = use_cases::CreateOrphanUseCase::new(uow_factory);
22 let entity_in: common::entities::Root = entity.into();
23 let result = uc.execute(&entity_in)?;
24 Ok(result.into())
25}
26
27pub fn create_orphan_multi(
28 db_context: &DbContext,
29 event_hub: &Arc<EventHub>,
30 entities: &[CreateRootDto],
31) -> Result<Vec<RootDto>> {
32 let uow_factory = RootWriteUoWFactory::new(db_context, event_hub);
33 let entities_in: Vec<common::entities::Root> = entities.iter().map(|dto| dto.into()).collect();
34 let mut uc = use_cases::CreateOrphanUseCase::new(uow_factory);
35 let result = uc.execute_multi(&entities_in)?;
36 Ok(result.into_iter().map(|e| e.into()).collect())
37}
38
39pub fn get(db_context: &DbContext, id: &EntityId) -> Result<Option<RootDto>> {
40 let uow_factory = RootReadUoWFactory::new(db_context);
41 let uc = use_cases::GetUseCase::new(uow_factory);
42 Ok(uc.execute(id)?.map(|e| e.into()))
43}
44
45pub fn get_all(db_context: &DbContext) -> Result<Vec<RootDto>> {
46 let uow_factory = RootReadUoWFactory::new(db_context);
47 let uc = use_cases::GetUseCase::new(uow_factory);
48 Ok(uc.execute_all()?.into_iter().map(|e| e.into()).collect())
49}
50
51pub fn get_multi(db_context: &DbContext, ids: &[EntityId]) -> Result<Vec<Option<RootDto>>> {
52 let uow_factory = RootReadUoWFactory::new(db_context);
53 let uc = use_cases::GetUseCase::new(uow_factory);
54 Ok(uc
55 .execute_multi(ids)?
56 .into_iter()
57 .map(|o| o.map(|e| e.into()))
58 .collect())
59}
60
61pub fn update(
62 db_context: &DbContext,
63 event_hub: &Arc<EventHub>,
64 entity: &UpdateRootDto,
65) -> Result<RootDto> {
66 let uow_factory = RootWriteUoWFactory::new(db_context, event_hub);
67 let entity_in: common::entities::Root = entity.into();
68 let mut uc = use_cases::UpdateUseCase::new(uow_factory);
69 let result = uc.execute(&entity_in)?;
70 Ok(result.into())
71}
72
73pub fn update_multi(
74 db_context: &DbContext,
75 event_hub: &Arc<EventHub>,
76 entities: &[UpdateRootDto],
77) -> Result<Vec<RootDto>> {
78 let uow_factory = RootWriteUoWFactory::new(db_context, event_hub);
79 let entities_in: Vec<common::entities::Root> = entities.iter().map(|dto| dto.into()).collect();
80 let mut uc = use_cases::UpdateUseCase::new(uow_factory);
81 let result = uc.execute_multi(&entities_in)?;
82 Ok(result.into_iter().map(|e| e.into()).collect())
83}
84
85pub fn update_with_relationships(
86 db_context: &DbContext,
87 event_hub: &Arc<EventHub>,
88 entity: &RootDto,
89) -> Result<RootDto> {
90 let uow_factory = RootWriteUoWFactory::new(db_context, event_hub);
91 let entity_in: common::entities::Root = entity.into();
92 let mut uc = use_cases::UpdateWithRelationshipsUseCase::new(uow_factory);
93 let result = uc.execute(&entity_in)?;
94 Ok(result.into())
95}
96
97pub fn update_with_relationships_multi(
98 db_context: &DbContext,
99 event_hub: &Arc<EventHub>,
100 entities: &[RootDto],
101) -> Result<Vec<RootDto>> {
102 let uow_factory = RootWriteUoWFactory::new(db_context, event_hub);
103 let entities_in: Vec<common::entities::Root> = entities.iter().map(|dto| dto.into()).collect();
104 let mut uc = use_cases::UpdateWithRelationshipsUseCase::new(uow_factory);
105 let result = uc.execute_multi(&entities_in)?;
106 Ok(result.into_iter().map(|e| e.into()).collect())
107}
108
109pub fn remove(db_context: &DbContext, event_hub: &Arc<EventHub>, id: &EntityId) -> Result<()> {
110 let uow_factory = RootWriteUoWFactory::new(db_context, event_hub);
111 let mut uc = use_cases::RemoveUseCase::new(uow_factory);
112 uc.execute(id)?;
113 Ok(())
114}
115
116pub fn remove_multi(
117 db_context: &DbContext,
118 event_hub: &Arc<EventHub>,
119 ids: &[EntityId],
120) -> Result<()> {
121 let uow_factory = RootWriteUoWFactory::new(db_context, event_hub);
122 let mut uc = use_cases::RemoveUseCase::new(uow_factory);
123 uc.execute_multi(ids)?;
124 Ok(())
125}
126
127pub fn get_relationship(
128 db_context: &DbContext,
129 id: &EntityId,
130 field: &RootRelationshipField,
131) -> Result<Vec<EntityId>> {
132 let uow_factory = RootReadUoWFactory::new(db_context);
133 let uc = use_cases::GetRelationshipUseCase::<RootRelationshipField, _>::new(uow_factory);
134 uc.execute(id, field)
135}
136
137pub fn get_relationship_many(
138 db_context: &DbContext,
139 ids: &[EntityId],
140 field: &RootRelationshipField,
141) -> Result<std::collections::HashMap<EntityId, Vec<EntityId>>> {
142 let uow_factory = RootReadUoWFactory::new(db_context);
143 let uc = use_cases::GetRelationshipManyUseCase::<RootRelationshipField, _>::new(uow_factory);
144 uc.execute(ids, field)
145}
146
147pub fn get_relationship_count(
148 db_context: &DbContext,
149 id: &EntityId,
150 field: &RootRelationshipField,
151) -> Result<usize> {
152 let uow_factory = RootReadUoWFactory::new(db_context);
153 let uc = use_cases::GetRelationshipCountUseCase::<RootRelationshipField, _>::new(uow_factory);
154 uc.execute(id, field)
155}
156
157pub fn get_relationship_in_range(
158 db_context: &DbContext,
159 id: &EntityId,
160 field: &RootRelationshipField,
161 offset: usize,
162 limit: usize,
163) -> Result<Vec<EntityId>> {
164 let uow_factory = RootReadUoWFactory::new(db_context);
165 let uc = use_cases::GetRelationshipInRangeUseCase::<RootRelationshipField, _>::new(uow_factory);
166 uc.execute(id, field, offset, limit)
167}
168
169pub fn set_relationship(
170 db_context: &DbContext,
171 event_hub: &Arc<EventHub>,
172 dto: &RootRelationshipDto,
173) -> Result<()> {
174 let uow_factory = RootWriteUoWFactory::new(db_context, event_hub);
175 let mut uc = use_cases::SetRelationshipUseCase::<RootRelationshipField, _>::new(uow_factory);
176 uc.execute(&dto.id, &dto.field, dto.right_ids.as_slice())?;
177 Ok(())
178}
179
180pub fn move_relationship(
181 db_context: &DbContext,
182 event_hub: &Arc<EventHub>,
183 id: &EntityId,
184 field: &RootRelationshipField,
185 ids_to_move: &[EntityId],
186 new_index: i32,
187) -> Result<Vec<EntityId>> {
188 let uow_factory = RootWriteUoWFactory::new(db_context, event_hub);
189 let mut uc = use_cases::MoveRelationshipUseCase::<RootRelationshipField, _>::new(uow_factory);
190 let result = uc.execute(id, field, ids_to_move, new_index)?;
191 Ok(result)
192}