Skip to main content

text_document_frontend/commands/
table_commands.rs

1// Generated by Qleany v1.5.1 from frontend_entity_commands.tera
2
3//! Table entity commands
4#![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
13/// Create a new table entity (orphan, no parent)
14pub 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}
29/// Create a new table entity as child of owner
30pub 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}
49/// Create multiple table entities (orphan, no parent)
50pub 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}
65/// Create multiple table entities as children of owner
66pub 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}
85/// Get a table entity by ID
86pub fn get_table(ctx: &AppContext, id: &EntityId) -> Result<Option<TableDto>> {
87    table_controller::get(&ctx.db_context, id).context("getting table")
88}
89
90/// Get multiple table entities by IDs
91pub 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
95/// Get all table entities.
96/// Note: returns entities in database key order (by EntityId), not insertion order
97/// or any user-defined sort. For ordered collections, use relationship-based
98/// retrieval (e.g. get_*_relationship for ordered_one_to_many fields).
99pub fn get_all_table(ctx: &AppContext) -> Result<Vec<TableDto>> {
100    table_controller::get_all(&ctx.db_context).context("getting all table entities")
101}
102
103/// Update a table entity
104pub 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
120/// Update multiple table entities
121pub 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
137/// Update a table entity with relationships
138pub 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
154/// Update multiple table entities with relationships
155pub 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
171/// Remove a table entity by ID
172pub 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
184/// Remove multiple table entities by IDs
185pub 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
197/// Get a table relationship
198pub 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
207/// Get relationship IDs for multiple table entities at once
208pub 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
217/// Get relationship count for a table entity
218pub 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
227/// Get relationship IDs for a table entity with pagination
228pub 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
239/// Set a table relationship
240pub 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
256/// Move (reorder) IDs within a table relationship
257pub 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}