qleany 1.7.3

Architecture generator for Rust and C++/Qt applications.
// Generated by Qleany v1.7.0 from frontend_entity_commands.tera

//! Field entity commands
#![allow(unused_imports, dead_code)]

use crate::app_context::AppContext;
use anyhow::{Context, Result};
use common::direct_access::field::FieldRelationshipField;
use common::types::EntityId;
use direct_access::FieldRelationshipDto;
use direct_access::{CreateFieldDto, FieldDto, UpdateFieldDto, field_controller};

/// Create a new field entity (orphan, no parent)
pub fn create_orphan_field(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dto: &CreateFieldDto,
) -> Result<FieldDto> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    field_controller::create_orphan(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dto,
    )
    .context("creating field")
}
/// Create a new field entity as child of owner
pub fn create_field(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dto: &CreateFieldDto,
    owner_id: EntityId,
    index: i32,
) -> Result<FieldDto> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    field_controller::create(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dto,
        owner_id,
        index,
    )
    .context("creating field")
}
/// Create multiple field entities (orphan, no parent)
pub fn create_orphan_field_multi(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dtos: &[CreateFieldDto],
) -> Result<Vec<FieldDto>> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    field_controller::create_orphan_multi(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dtos,
    )
    .context("creating field entities")
}
/// Create multiple field entities as children of owner
pub fn create_field_multi(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dtos: &[CreateFieldDto],
    owner_id: EntityId,
    index: i32,
) -> Result<Vec<FieldDto>> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    field_controller::create_multi(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dtos,
        owner_id,
        index,
    )
    .context("creating field entities")
}
/// Get a field entity by ID
pub fn get_field(ctx: &AppContext, id: &EntityId) -> Result<Option<FieldDto>> {
    field_controller::get(&ctx.db_context, id).context("getting field")
}

/// Get multiple field entities by IDs
pub fn get_field_multi(ctx: &AppContext, ids: &[EntityId]) -> Result<Vec<Option<FieldDto>>> {
    field_controller::get_multi(&ctx.db_context, ids).context("getting field entities")
}

/// Get all field entities.
/// Note: returns entities in database key order (by EntityId), not insertion order
/// or any user-defined sort. For ordered collections, use relationship-based
/// retrieval (e.g. get_*_relationship for ordered_one_to_many fields).
pub fn get_all_field(ctx: &AppContext) -> Result<Vec<FieldDto>> {
    field_controller::get_all(&ctx.db_context).context("getting all field entities")
}

/// Update a field entity
pub fn update_field(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dto: &UpdateFieldDto,
) -> Result<FieldDto> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    field_controller::update(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dto,
    )
    .context("updating field")
}

/// Update multiple field entities
pub fn update_field_multi(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dtos: &[UpdateFieldDto],
) -> Result<Vec<FieldDto>> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    field_controller::update_multi(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dtos,
    )
    .context("updating field entities")
}

/// Update a field entity with relationships
pub fn update_field_with_relationships(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dto: &FieldDto,
) -> Result<FieldDto> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    field_controller::update_with_relationships(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dto,
    )
    .context("updating field with relationships")
}

/// Update multiple field entities with relationships
pub fn update_field_with_relationships_multi(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dtos: &[FieldDto],
) -> Result<Vec<FieldDto>> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    field_controller::update_with_relationships_multi(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dtos,
    )
    .context("updating field entities with relationships")
}

/// Remove a field entity by ID
pub fn remove_field(ctx: &AppContext, stack_id: Option<u64>, id: &EntityId) -> Result<()> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    field_controller::remove(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        id,
    )
    .context("removing field")
}

/// Remove multiple field entities by IDs
pub fn remove_field_multi(ctx: &AppContext, stack_id: Option<u64>, ids: &[EntityId]) -> Result<()> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    field_controller::remove_multi(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        ids,
    )
    .context("removing field entities")
}

/// Get a field relationship
pub fn get_field_relationship(
    ctx: &AppContext,
    id: &EntityId,
    field: &FieldRelationshipField,
) -> Result<Vec<EntityId>> {
    field_controller::get_relationship(&ctx.db_context, id, field)
        .context("getting field relationship")
}

/// Get relationship IDs for multiple field entities at once
pub fn get_field_relationship_many(
    ctx: &AppContext,
    ids: &[EntityId],
    field: &FieldRelationshipField,
) -> Result<std::collections::HashMap<EntityId, Vec<EntityId>>> {
    field_controller::get_relationship_many(&ctx.db_context, ids, field)
        .context("getting field relationships (many)")
}

/// Get relationship count for a field entity
pub fn get_field_relationship_count(
    ctx: &AppContext,
    id: &EntityId,
    field: &FieldRelationshipField,
) -> Result<usize> {
    field_controller::get_relationship_count(&ctx.db_context, id, field)
        .context("getting field relationship count")
}

/// Get relationship IDs for a field entity with pagination
pub fn get_field_relationship_in_range(
    ctx: &AppContext,
    id: &EntityId,
    field: &FieldRelationshipField,
    offset: usize,
    limit: usize,
) -> Result<Vec<EntityId>> {
    field_controller::get_relationship_in_range(&ctx.db_context, id, field, offset, limit)
        .context("getting field relationship range")
}

/// Set a field relationship
pub fn set_field_relationship(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dto: &FieldRelationshipDto,
) -> Result<()> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    field_controller::set_relationship(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dto,
    )
    .context("setting field relationship")
}

/// Move (reorder) IDs within a field relationship
pub fn move_field_relationship(
    ctx: &AppContext,
    stack_id: Option<u64>,
    id: &EntityId,
    field: &FieldRelationshipField,
    ids_to_move: &[EntityId],
    new_index: i32,
) -> Result<Vec<EntityId>> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    field_controller::move_relationship(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        id,
        field,
        ids_to_move,
        new_index,
    )
    .context("moving field relationship")
}