text-document-direct-access 1.4.0

Entity CRUD controllers and DTOs for text-document
Documentation
// Generated by Qleany v1.5.0 from entity_units_of_work.tera

use anyhow::{Ok, Result};
use common::database::{CommandUnitOfWork, QueryUnitOfWork};
use common::database::{db_context::DbContext, transactions::Transaction};
use common::direct_access::repository_factory;
use common::direct_access::root::RootRelationshipField;
use common::direct_access::use_cases;
use common::entities::Root;
use common::event::{AllEvent, DirectAccessEntity, Event, EventBuffer, EventHub, Origin};
use common::snapshot::EntityTreeSnapshot;
use common::types;
use common::types::EntityId;
use std::cell::RefCell;
use std::sync::Arc;

// ===========================================================================
// Write UoW
// ===========================================================================

pub struct RootWriteUoW {
    context: DbContext,
    transaction: Option<Transaction>,
    event_hub: Arc<EventHub>,
    event_buffer: RefCell<EventBuffer>,
}

impl RootWriteUoW {
    pub fn new(db_context: &DbContext, event_hub: &Arc<EventHub>) -> Self {
        RootWriteUoW {
            context: db_context.clone(),
            transaction: None,
            event_hub: event_hub.clone(),
            event_buffer: RefCell::new(EventBuffer::new()),
        }
    }
}

impl CommandUnitOfWork for RootWriteUoW {
    fn begin_transaction(&mut self) -> Result<()> {
        self.transaction = Some(Transaction::begin_write_transaction(&self.context)?);
        self.event_buffer.get_mut().begin_buffering();
        Ok(())
    }

    fn commit(&mut self) -> Result<()> {
        self.transaction.take().unwrap().commit()?;
        for event in self.event_buffer.get_mut().flush() {
            self.event_hub.send_event(event);
        }
        Ok(())
    }

    fn rollback(&mut self) -> Result<()> {
        self.transaction.take().unwrap().rollback()?;
        self.event_buffer.get_mut().discard();
        Ok(())
    }

    fn create_savepoint(&self) -> Result<types::Savepoint> {
        self.transaction.as_ref().unwrap().create_savepoint()
    }

    fn restore_to_savepoint(&mut self, savepoint: types::Savepoint) -> Result<()> {
        let mut transaction = self.transaction.take().unwrap();
        transaction.restore_to_savepoint(savepoint)?;

        // Discard buffered events — savepoint restore invalidated them
        self.event_buffer.get_mut().discard();

        // Send Reset immediately (not buffered — UI must refresh now)
        self.event_hub.send_event(Event {
            origin: Origin::DirectAccess(DirectAccessEntity::All(AllEvent::Reset)),
            ids: vec![],
            data: None,
        });

        // Recreate the transaction after restoring to savepoint
        self.transaction = Some(transaction);

        Ok(())
    }
}

impl use_cases::WriteUoW for RootWriteUoW {
    type Entity = Root;

    fn get(&self, id: &EntityId) -> Result<Option<Root>> {
        let transaction = self.transaction.as_ref().expect("Transaction not started");
        let repo = repository_factory::write::create_root_repository(transaction);
        Ok(repo.get(id)?)
    }

    fn get_multi(&self, ids: &[EntityId]) -> Result<Vec<Option<Root>>> {
        let transaction = self.transaction.as_ref().expect("Transaction not started");
        let repo = repository_factory::write::create_root_repository(transaction);
        Ok(repo.get_multi(ids)?)
    }

    fn get_all(&self) -> Result<Vec<Root>> {
        let transaction = self.transaction.as_ref().expect("Transaction not started");
        let repo = repository_factory::write::create_root_repository(transaction);
        Ok(repo.get_all()?)
    }

    fn create_orphan_multi(&self, entities: &[Root]) -> Result<Vec<Root>> {
        let transaction = self.transaction.as_ref().expect("Transaction not started");
        let mut repo = repository_factory::write::create_root_repository(transaction);
        let mut event_buffer = self.event_buffer.borrow_mut();
        Ok(repo.create_orphan_multi(&mut event_buffer, entities)?)
    }

    fn update_multi(&self, entities: &[Root]) -> Result<Vec<Root>> {
        let transaction = self.transaction.as_ref().expect("Transaction not started");
        let mut repo = repository_factory::write::create_root_repository(transaction);
        let mut event_buffer = self.event_buffer.borrow_mut();
        Ok(repo.update_multi(&mut event_buffer, entities)?)
    }

    fn update_with_relationships_multi(&self, entities: &[Root]) -> Result<Vec<Root>> {
        let transaction = self.transaction.as_ref().expect("Transaction not started");
        let mut repo = repository_factory::write::create_root_repository(transaction);
        let mut event_buffer = self.event_buffer.borrow_mut();
        Ok(repo.update_with_relationships_multi(&mut event_buffer, entities)?)
    }

    fn remove(&self, id: &EntityId) -> Result<()> {
        let transaction = self.transaction.as_ref().expect("Transaction not started");
        let mut repo = repository_factory::write::create_root_repository(transaction);
        let mut event_buffer = self.event_buffer.borrow_mut();
        Ok(repo.remove(&mut event_buffer, id)?)
    }

    fn remove_multi(&self, ids: &[EntityId]) -> Result<()> {
        let transaction = self.transaction.as_ref().expect("Transaction not started");
        let mut repo = repository_factory::write::create_root_repository(transaction);
        let mut event_buffer = self.event_buffer.borrow_mut();
        Ok(repo.remove_multi(&mut event_buffer, ids)?)
    }

    fn snapshot(&self, ids: &[EntityId]) -> Result<EntityTreeSnapshot> {
        let transaction = self.transaction.as_ref().expect("Transaction not started");
        let repo = repository_factory::write::create_root_repository(transaction);
        Ok(repo.snapshot(ids)?)
    }

    fn restore(&self, snap: &EntityTreeSnapshot) -> Result<()> {
        let transaction = self.transaction.as_ref().expect("Transaction not started");
        let mut repo = repository_factory::write::create_root_repository(transaction);
        let mut event_buffer = self.event_buffer.borrow_mut();
        Ok(repo.restore(&mut event_buffer, snap)?)
    }
}

impl use_cases::WriteRelUoW<RootRelationshipField> for RootWriteUoW {
    fn set_relationship(
        &self,
        id: &EntityId,
        field: &RootRelationshipField,
        right_ids: &[EntityId],
    ) -> Result<()> {
        let transaction = self.transaction.as_ref().expect("Transaction not started");
        let mut repo = repository_factory::write::create_root_repository(transaction);
        let mut event_buffer = self.event_buffer.borrow_mut();
        repo.set_relationship(&mut event_buffer, id, field, right_ids)?;
        Ok(())
    }

    fn move_relationship(
        &self,
        id: &EntityId,
        field: &RootRelationshipField,
        ids_to_move: &[EntityId],
        new_index: i32,
    ) -> Result<Vec<EntityId>> {
        let transaction = self.transaction.as_ref().expect("Transaction not started");
        let mut repo = repository_factory::write::create_root_repository(transaction);
        let mut event_buffer = self.event_buffer.borrow_mut();
        Ok(repo.move_relationship_ids(&mut event_buffer, id, field, ids_to_move, new_index)?)
    }
}

// ---------------------------------------------------------------------------
// Write UoW Factory
// ---------------------------------------------------------------------------

pub struct RootWriteUoWFactory {
    context: DbContext,
    event_hub: Arc<EventHub>,
}

impl RootWriteUoWFactory {
    pub fn new(db_context: &DbContext, event_hub: &Arc<EventHub>) -> Self {
        RootWriteUoWFactory {
            context: db_context.clone(),
            event_hub: event_hub.clone(),
        }
    }
}

impl use_cases::WriteUoWFactory for RootWriteUoWFactory {
    type Entity = Root;
    fn create(&self) -> Box<dyn use_cases::WriteUoW<Entity = Root>> {
        Box::new(RootWriteUoW::new(&self.context, &self.event_hub))
    }
}

impl use_cases::WriteRelUoWFactory<RootRelationshipField> for RootWriteUoWFactory {
    fn create(&self) -> Box<dyn use_cases::WriteRelUoW<RootRelationshipField>> {
        Box::new(RootWriteUoW::new(&self.context, &self.event_hub))
    }
}

// ===========================================================================
// Read-Only UoW
// ===========================================================================

pub struct RootReadUoW {
    context: DbContext,
    transaction: RefCell<Option<Transaction>>,
}

impl RootReadUoW {
    pub fn new(db_context: &DbContext) -> Self {
        RootReadUoW {
            context: db_context.clone(),
            transaction: RefCell::new(None),
        }
    }
}

impl QueryUnitOfWork for RootReadUoW {
    fn begin_transaction(&self) -> Result<()> {
        self.transaction
            .replace(Some(Transaction::begin_read_transaction(&self.context)?));
        Ok(())
    }

    fn end_transaction(&self) -> Result<()> {
        self.transaction.take().unwrap().end_read_transaction()?;
        Ok(())
    }
}

impl use_cases::ReadUoW for RootReadUoW {
    type Entity = Root;

    fn get(&self, id: &EntityId) -> Result<Option<Root>> {
        let transaction = self.transaction.borrow();
        let repo = repository_factory::read::create_root_repository(transaction.as_ref().unwrap());
        Ok(repo.get(id)?)
    }

    fn get_multi(&self, ids: &[EntityId]) -> Result<Vec<Option<Root>>> {
        let transaction = self.transaction.borrow();
        let repo = repository_factory::read::create_root_repository(transaction.as_ref().unwrap());
        Ok(repo.get_multi(ids)?)
    }

    fn get_all(&self) -> Result<Vec<Root>> {
        let transaction = self.transaction.borrow();
        let repo = repository_factory::read::create_root_repository(transaction.as_ref().unwrap());
        Ok(repo.get_all()?)
    }
}

impl use_cases::ReadRelUoW<RootRelationshipField> for RootReadUoW {
    fn get_relationship(
        &self,
        id: &EntityId,
        field: &RootRelationshipField,
    ) -> Result<Vec<EntityId>> {
        let transaction = self.transaction.borrow();
        let repo = repository_factory::read::create_root_repository(transaction.as_ref().unwrap());
        Ok(repo.get_relationship(id, field)?)
    }

    fn get_relationship_many(
        &self,
        ids: &[EntityId],
        field: &RootRelationshipField,
    ) -> Result<std::collections::HashMap<EntityId, Vec<EntityId>>> {
        let transaction = self.transaction.borrow();
        let repo = repository_factory::read::create_root_repository(transaction.as_ref().unwrap());
        Ok(repo.get_relationship_many(ids, field)?)
    }

    fn get_relationship_count(
        &self,
        id: &EntityId,
        field: &RootRelationshipField,
    ) -> Result<usize> {
        let transaction = self.transaction.borrow();
        let repo = repository_factory::read::create_root_repository(transaction.as_ref().unwrap());
        Ok(repo.get_relationship_count(id, field)?)
    }

    fn get_relationship_in_range(
        &self,
        id: &EntityId,
        field: &RootRelationshipField,
        offset: usize,
        limit: usize,
    ) -> Result<Vec<EntityId>> {
        let transaction = self.transaction.borrow();
        let repo = repository_factory::read::create_root_repository(transaction.as_ref().unwrap());
        Ok(repo.get_relationship_in_range(id, field, offset, limit)?)
    }
}

// ---------------------------------------------------------------------------
// Read-Only UoW Factory
// ---------------------------------------------------------------------------

pub struct RootReadUoWFactory {
    context: DbContext,
}

impl RootReadUoWFactory {
    pub fn new(db_context: &DbContext) -> Self {
        RootReadUoWFactory {
            context: db_context.clone(),
        }
    }
}

impl use_cases::ReadUoWFactory for RootReadUoWFactory {
    type Entity = Root;
    fn create(&self) -> Box<dyn use_cases::ReadUoW<Entity = Root>> {
        Box::new(RootReadUoW::new(&self.context))
    }
}

impl use_cases::ReadRelUoWFactory<RootRelationshipField> for RootReadUoWFactory {
    fn create(&self) -> Box<dyn use_cases::ReadRelUoW<RootRelationshipField>> {
        Box::new(RootReadUoW::new(&self.context))
    }
}