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}
193
194#[cfg(test)]
195mod tests {
196 #![allow(dead_code)]
197 #![allow(unused_imports)]
198
199 use super::*;
200 use common::database::db_context::DbContext;
201 use common::event::EventHub;
202 use common::types::EntityId;
203 use common::undo_redo::UndoRedoManager;
204 use std::sync::Arc;
205
206 struct TestContext {
207 db: DbContext,
208 hub: Arc<EventHub>,
209 undo: UndoRedoManager,
210 }
211
212 impl TestContext {
213 fn new() -> Self {
214 let db = DbContext::new().expect("Failed to create in-memory DB");
215 let hub = Arc::new(EventHub::new());
216 let mut undo = UndoRedoManager::new();
217 undo.set_event_hub(&hub);
218 TestContext { db, hub, undo }
219 }
220 }
221
222 fn create_one(ctx: &mut TestContext) -> RootDto {
224 create_orphan(&ctx.db, &ctx.hub, &CreateRootDto::default()).unwrap()
225 }
226
227 #[test]
232 fn test_create_orphan_and_get() {
233 let mut ctx = TestContext::new();
234 let created = create_one(&mut ctx);
235 assert!(created.id > 0);
236
237 let fetched = get(&ctx.db, &created.id).unwrap();
238 assert!(fetched.is_some());
239 assert_eq!(fetched.unwrap().id, created.id);
240 }
241
242 #[test]
247 fn test_get_nonexistent() {
248 let ctx = TestContext::new();
249 assert!(get(&ctx.db, &999999).unwrap().is_none());
250 }
251
252 #[test]
257 fn test_get_all() {
258 let mut ctx = TestContext::new();
259 create_one(&mut ctx);
260 let all = get_all(&ctx.db).unwrap();
261 assert!(!all.is_empty());
262 }
263
264 #[test]
269 fn test_get_multi() {
270 let mut ctx = TestContext::new();
271 let a = create_one(&mut ctx);
272 let results = get_multi(&ctx.db, &[a.id, 999999]).unwrap();
273 assert_eq!(results.len(), 2);
274 assert!(results[0].is_some());
275 assert!(results[1].is_none());
276 }
277
278 #[test]
283 fn test_update() {
284 let mut ctx = TestContext::new();
285 let created = create_one(&mut ctx);
286 let update_dto: UpdateRootDto = created.into();
287 let updated = update(&ctx.db, &ctx.hub, &update_dto).unwrap();
288 assert_eq!(updated.id, update_dto.id);
289 }
290
291 #[test]
296 fn test_remove() {
297 let mut ctx = TestContext::new();
298 let created = create_one(&mut ctx);
299 remove(&ctx.db, &ctx.hub, &created.id).unwrap();
300 assert!(get(&ctx.db, &created.id).unwrap().is_none());
301 }
302
303 #[test]
308 fn test_remove_multi() {
309 let mut ctx = TestContext::new();
310 let a = create_one(&mut ctx);
311 remove_multi(&ctx.db, &ctx.hub, &[a.id]).unwrap();
312 assert!(get(&ctx.db, &a.id).unwrap().is_none());
313 }
314
315 #[test]
320 fn test_get_relationship_default() {
321 let mut ctx = TestContext::new();
322 let created = create_one(&mut ctx);
323 let rel_ids =
324 get_relationship(&ctx.db, &created.id, &RootRelationshipField::Document).unwrap();
325 assert_eq!(rel_ids.len(), 1);
327 }
328
329 #[test]
334 fn test_get_relationship_count_default() {
335 let mut ctx = TestContext::new();
336 let created = create_one(&mut ctx);
337 let count =
338 get_relationship_count(&ctx.db, &created.id, &RootRelationshipField::Document).unwrap();
339 assert_eq!(count, 1);
341 }
342}