// This file was generated automatically by Qleany's generator, edit at your own risk! 
// If you do, be careful to not overwrite it when you run the generator again.
#include "remove_{{ entity_mappable_with_snake }}_command_handler.h"
#include <qleany/tools/automapper/automapper.h>
#include "repository/interface_{{ entity_mappable_with_snake }}_repository.h"
#include "{{ feature_snake_name }}/validators/remove_{{ entity_mappable_with_snake }}_command_validator.h"

using namespace Qleany;
using namespace {{ application_cpp_domain_name }}::Contracts::DTO::{{ feature_pascal_name }};
using namespace {{ application_cpp_domain_name }}::Contracts::Repository;
using namespace {{ application_cpp_domain_name }}::Contracts::CQRS::{{ feature_pascal_name }}::Commands;
using namespace {{ application_cpp_domain_name }}::Application::Features::{{ feature_pascal_name }}::Commands;
using namespace {{ application_cpp_domain_name }}::Contracts::CQRS::{{ feature_pascal_name }}::Validators;

Remove{{ entity_mappable_with_pascal }}CommandHandler::Remove{{ entity_mappable_with_pascal }}CommandHandler(Interface{{ entity_mappable_with_pascal }}Repository *repository)
    : m_repository(repository)
{
    if (!s_mappingRegistered)
    {
        registerMappings();
        s_mappingRegistered = true;
    }
}

Result<int>Remove{{ entity_mappable_with_pascal }}CommandHandler::handle(QPromise<Result<void> >   & progressPromise,
                                                      const Remove{{ entity_mappable_with_pascal }}Command& request)
{
    Result<int> result;

    try
    {
        result = handleImpl(progressPromise, request);
    }
    catch (const std::exception& ex)
    {
        result = Result<int>(QLN_ERROR_2(Q_FUNC_INFO, Error::Critical, "Unknown error", ex.what()));
        qDebug() << "Error handling Remove{{ entity_mappable_with_pascal }}Command:" << ex.what();
    }
    progressPromise.addResult(Result<void>(result.error()));
    return result;
}

Result<int>Remove{{ entity_mappable_with_pascal }}CommandHandler::restore()
{
    Result<int> result;

    try
    {
        result = restoreImpl();
    }
    catch (const std::exception& ex)
    {
        result = Result<int>(QLN_ERROR_2(Q_FUNC_INFO, Error::Critical, "Unknown error", ex.what()));
        qDebug() << "Error handling Remove{{ entity_mappable_with_pascal }}Command restore:" << ex.what();
    }
    return result;
}

Result<int>Remove{{ entity_mappable_with_pascal }}CommandHandler::handleImpl(QPromise<Result<void> >   & progressPromise,const Remove{{ entity_mappable_with_pascal }}Command& request)
{   
    int {{ entity_mappable_with_camel }}Id = request.id;

    // Validate the command using the validator
    auto validator = Remove{{ entity_mappable_with_pascal }}CommandValidator(m_repository);
    Result<void> validatorResult = validator.validate({{ entity_mappable_with_camel }}Id);

    QLN_RETURN_IF_ERROR(int, validatorResult);



    Result<{{ application_cpp_domain_name }}::Entities::{{ entity_mappable_with_pascal }}> {{ entity_mappable_with_camel }}Result = m_repository->get({{ entity_mappable_with_camel }}Id);

    QLN_RETURN_IF_ERROR(int, {{ entity_mappable_with_camel }}Result)

    // save old entity
    m_oldState = {{ entity_mappable_with_camel }}Result.value();

    auto deleteResult = m_repository->remove(QList<int>() << {{ entity_mappable_with_camel }}Id);

    QLN_RETURN_IF_ERROR(int, deleteResult)

    // repositories handle remove Q_SIGNALS
    // Q_EMIT {{ entity_mappable_with_camel }}Removed(deleteResult.value());

    qDebug() << "{{ entity_mappable_with_pascal }} removed:" << {{ entity_mappable_with_camel }}Id;

    return Result<int>({{ entity_mappable_with_camel }}Id);
}

Result<int>Remove{{ entity_mappable_with_pascal }}CommandHandler::restoreImpl()
{
    // no restore possible
    return Result<int>(0);
}


bool Remove{{ entity_mappable_with_pascal }}CommandHandler::s_mappingRegistered = false;

void Remove{{ entity_mappable_with_pascal }}CommandHandler::registerMappings() 
{
    Qleany::Tools::AutoMapper::AutoMapper::registerMapping<{{ application_cpp_domain_name }}::Entities::{{ entity_mappable_with_pascal }}, Contracts::DTO::{{ feature_pascal_name }}::{{ entity_mappable_with_pascal }}DTO>(true, true);
}
