// Generated by Qleany. Edit at your own risk! Be careful when regenerating this file
// as changes will be lost.

use crate::use_cases::get_file_diff_uc::{
    GetFileDiffUnitOfWorkFactoryTrait, GetFileDiffUnitOfWorkTrait,
};
use anyhow::{Ok, Result};
use common::database::QueryUnitOfWork;
use common::database::{db_context::DbContext, transactions::Transaction};
use common::entities::{File, Root, Workspace};
use common::types::EntityId;
use std::cell::RefCell;

// Unit of work for GetFileDiff

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

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

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

    fn end_transaction(&self) -> Result<()> {
        self.transaction
            .take()
            .ok_or_else(|| anyhow::anyhow!("No active transaction"))?
            .end_read_transaction()?;
        Ok(())
    }
}

#[macros::uow_action(entity = "Root", action = "GetAllRO")]
#[macros::uow_action(entity = "Root", action = "GetRelationshipRO")]
#[macros::uow_action(entity = "Workspace", action = "GetRO")]
#[macros::uow_action(entity = "File", action = "GetRO")]
impl GetFileDiffUnitOfWorkTrait for GetFileDiffUnitOfWork {}

pub struct GetFileDiffUnitOfWorkFactory {
    context: DbContext,
}

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

impl GetFileDiffUnitOfWorkFactoryTrait for GetFileDiffUnitOfWorkFactory {
    fn create(&self) -> Box<dyn GetFileDiffUnitOfWorkTrait> {
        Box::new(GetFileDiffUnitOfWork::new(&self.context))
    }
}
