text_document_frontend/commands/
table_commands.rs1#![allow(unused_imports, dead_code)]
5
6use crate::app_context::AppContext;
7use anyhow::{Context, Result};
8use common::direct_access::table::TableRelationshipField;
9use common::types::EntityId;
10use direct_access::TableRelationshipDto;
11use direct_access::{CreateTableDto, TableDto, UpdateTableDto, table_controller};
12
13pub fn create_orphan_table(
15 ctx: &AppContext,
16 stack_id: Option<u64>,
17 dto: &CreateTableDto,
18) -> Result<TableDto> {
19 let mut undo_redo_manager = ctx.undo_redo_manager.lock();
20 table_controller::create_orphan(
21 &ctx.db_context,
22 &ctx.event_hub,
23 &mut undo_redo_manager,
24 stack_id,
25 dto,
26 )
27 .context("creating table")
28}
29pub fn create_table(
31 ctx: &AppContext,
32 stack_id: Option<u64>,
33 dto: &CreateTableDto,
34 owner_id: EntityId,
35 index: i32,
36) -> Result<TableDto> {
37 let mut undo_redo_manager = ctx.undo_redo_manager.lock();
38 table_controller::create(
39 &ctx.db_context,
40 &ctx.event_hub,
41 &mut undo_redo_manager,
42 stack_id,
43 dto,
44 owner_id,
45 index,
46 )
47 .context("creating table")
48}
49pub fn create_orphan_table_multi(
51 ctx: &AppContext,
52 stack_id: Option<u64>,
53 dtos: &[CreateTableDto],
54) -> Result<Vec<TableDto>> {
55 let mut undo_redo_manager = ctx.undo_redo_manager.lock();
56 table_controller::create_orphan_multi(
57 &ctx.db_context,
58 &ctx.event_hub,
59 &mut undo_redo_manager,
60 stack_id,
61 dtos,
62 )
63 .context("creating table entities")
64}
65pub fn create_table_multi(
67 ctx: &AppContext,
68 stack_id: Option<u64>,
69 dtos: &[CreateTableDto],
70 owner_id: EntityId,
71 index: i32,
72) -> Result<Vec<TableDto>> {
73 let mut undo_redo_manager = ctx.undo_redo_manager.lock();
74 table_controller::create_multi(
75 &ctx.db_context,
76 &ctx.event_hub,
77 &mut undo_redo_manager,
78 stack_id,
79 dtos,
80 owner_id,
81 index,
82 )
83 .context("creating table entities")
84}
85pub fn get_table(ctx: &AppContext, id: &EntityId) -> Result<Option<TableDto>> {
87 table_controller::get(&ctx.db_context, id).context("getting table")
88}
89
90pub fn get_table_multi(ctx: &AppContext, ids: &[EntityId]) -> Result<Vec<Option<TableDto>>> {
92 table_controller::get_multi(&ctx.db_context, ids).context("getting table entities")
93}
94
95pub fn get_all_table(ctx: &AppContext) -> Result<Vec<TableDto>> {
100 table_controller::get_all(&ctx.db_context).context("getting all table entities")
101}
102
103pub fn update_table(
105 ctx: &AppContext,
106 stack_id: Option<u64>,
107 dto: &UpdateTableDto,
108) -> Result<TableDto> {
109 let mut undo_redo_manager = ctx.undo_redo_manager.lock();
110 table_controller::update(
111 &ctx.db_context,
112 &ctx.event_hub,
113 &mut undo_redo_manager,
114 stack_id,
115 dto,
116 )
117 .context("updating table")
118}
119
120pub fn update_table_multi(
122 ctx: &AppContext,
123 stack_id: Option<u64>,
124 dtos: &[UpdateTableDto],
125) -> Result<Vec<TableDto>> {
126 let mut undo_redo_manager = ctx.undo_redo_manager.lock();
127 table_controller::update_multi(
128 &ctx.db_context,
129 &ctx.event_hub,
130 &mut undo_redo_manager,
131 stack_id,
132 dtos,
133 )
134 .context("updating table entities")
135}
136
137pub fn update_table_with_relationships(
139 ctx: &AppContext,
140 stack_id: Option<u64>,
141 dto: &TableDto,
142) -> Result<TableDto> {
143 let mut undo_redo_manager = ctx.undo_redo_manager.lock();
144 table_controller::update_with_relationships(
145 &ctx.db_context,
146 &ctx.event_hub,
147 &mut undo_redo_manager,
148 stack_id,
149 dto,
150 )
151 .context("updating table with relationships")
152}
153
154pub fn update_table_with_relationships_multi(
156 ctx: &AppContext,
157 stack_id: Option<u64>,
158 dtos: &[TableDto],
159) -> Result<Vec<TableDto>> {
160 let mut undo_redo_manager = ctx.undo_redo_manager.lock();
161 table_controller::update_with_relationships_multi(
162 &ctx.db_context,
163 &ctx.event_hub,
164 &mut undo_redo_manager,
165 stack_id,
166 dtos,
167 )
168 .context("updating table entities with relationships")
169}
170
171pub fn remove_table(ctx: &AppContext, stack_id: Option<u64>, id: &EntityId) -> Result<()> {
173 let mut undo_redo_manager = ctx.undo_redo_manager.lock();
174 table_controller::remove(
175 &ctx.db_context,
176 &ctx.event_hub,
177 &mut undo_redo_manager,
178 stack_id,
179 id,
180 )
181 .context("removing table")
182}
183
184pub fn remove_table_multi(ctx: &AppContext, stack_id: Option<u64>, ids: &[EntityId]) -> Result<()> {
186 let mut undo_redo_manager = ctx.undo_redo_manager.lock();
187 table_controller::remove_multi(
188 &ctx.db_context,
189 &ctx.event_hub,
190 &mut undo_redo_manager,
191 stack_id,
192 ids,
193 )
194 .context("removing table entities")
195}
196
197pub fn get_table_relationship(
199 ctx: &AppContext,
200 id: &EntityId,
201 field: &TableRelationshipField,
202) -> Result<Vec<EntityId>> {
203 table_controller::get_relationship(&ctx.db_context, id, field)
204 .context("getting table relationship")
205}
206
207pub fn get_table_relationship_many(
209 ctx: &AppContext,
210 ids: &[EntityId],
211 field: &TableRelationshipField,
212) -> Result<std::collections::HashMap<EntityId, Vec<EntityId>>> {
213 table_controller::get_relationship_many(&ctx.db_context, ids, field)
214 .context("getting table relationships (many)")
215}
216
217pub fn get_table_relationship_count(
219 ctx: &AppContext,
220 id: &EntityId,
221 field: &TableRelationshipField,
222) -> Result<usize> {
223 table_controller::get_relationship_count(&ctx.db_context, id, field)
224 .context("getting table relationship count")
225}
226
227pub fn get_table_relationship_in_range(
229 ctx: &AppContext,
230 id: &EntityId,
231 field: &TableRelationshipField,
232 offset: usize,
233 limit: usize,
234) -> Result<Vec<EntityId>> {
235 table_controller::get_relationship_in_range(&ctx.db_context, id, field, offset, limit)
236 .context("getting table relationship range")
237}
238
239pub fn set_table_relationship(
241 ctx: &AppContext,
242 stack_id: Option<u64>,
243 dto: &TableRelationshipDto,
244) -> Result<()> {
245 let mut undo_redo_manager = ctx.undo_redo_manager.lock();
246 table_controller::set_relationship(
247 &ctx.db_context,
248 &ctx.event_hub,
249 &mut undo_redo_manager,
250 stack_id,
251 dto,
252 )
253 .context("setting table relationship")
254}
255
256pub fn move_table_relationship(
258 ctx: &AppContext,
259 stack_id: Option<u64>,
260 id: &EntityId,
261 field: &TableRelationshipField,
262 ids_to_move: &[EntityId],
263 new_index: i32,
264) -> Result<Vec<EntityId>> {
265 let mut undo_redo_manager = ctx.undo_redo_manager.lock();
266 table_controller::move_relationship(
267 &ctx.db_context,
268 &ctx.event_hub,
269 &mut undo_redo_manager,
270 stack_id,
271 id,
272 field,
273 ids_to_move,
274 new_index,
275 )
276 .context("moving table relationship")
277}