public async Task <IActionResult> CreateTemplateConversionUnit(AppTemplateConversionUnitCreateDto dto)
        {
            var response = await _appTemplateConversionUnitService.CreateAppTemplateConversionUnit(dto);

            return(Ok(response));
        }
        public async Task <ApiResponse <AppTemplateConversionUnitGenericGetDto> > CreateAppTemplateConversionUnit(AppTemplateConversionUnitCreateDto dto)
        {
            AppTemplateConversionUnitGenericGetDto resultDto = new AppTemplateConversionUnitGenericGetDto();

            Metadata metadata = new Metadata
            {
                IsValid = true,
                Message = ""
            };

            ApiResponse <AppTemplateConversionUnitGenericGetDto> response = new ApiResponse <AppTemplateConversionUnitGenericGetDto>(resultDto);


            try
            {
                AppVariables AppVariablesFind = await _appVariablesService.GetById(dto.AppVariableId);

                if (AppVariablesFind == null)
                {
                    metadata.IsValid = false;
                    metadata.Message = "Variable no existe";
                    response.Data    = resultDto;
                    response.Meta    = metadata;
                    return(response);
                }


                AppUnits appUnitIdSinceFind = await _appUnitsService.GetById(dto.AppUnitIdSince);

                if (appUnitIdSinceFind == null)
                {
                    metadata.IsValid = false;
                    metadata.Message = "Unidad de Medida desde no existe!!";
                    response.Data    = resultDto;
                    response.Meta    = metadata;
                    return(response);
                }

                AppUnits appUnitIdUntilFind = await _appUnitsService.GetById(dto.AppUnitIdUntil);

                if (appUnitIdUntilFind == null)
                {
                    metadata.IsValid = false;
                    metadata.Message = "Unidad de Medida hasta no existe!!";
                    response.Data    = resultDto;
                    response.Meta    = metadata;
                    return(response);
                }


                AppTemplateConversionUnit template = new AppTemplateConversionUnit();

                template.AppVariableId = dto.AppVariableId;


                template.Code           = AppVariablesFind.Code;
                template.Description    = AppVariablesFind.Description;
                template.Formula        = dto.Formula;
                template.Value          = dto.Value;
                template.SumValue       = dto.SumValue;
                template.OrderCalculate = dto.OrderCalculate;
                template.AppUnitIdSince = dto.AppUnitIdSince;
                template.AppUnitIdUntil = dto.AppUnitIdUntil;



                var inserted = await Insert(template);

                resultDto = _mapper.Map <AppTemplateConversionUnitGenericGetDto>(inserted);

                if (appUnitIdSinceFind != null)
                {
                    AppUnitsGetDto appUnitsGetDto = _mapper.Map <AppUnitsGetDto>(appUnitIdSinceFind);
                    resultDto.AppUnitIdSinceGetDto = appUnitsGetDto;
                }

                if (appUnitIdUntilFind != null)
                {
                    AppUnitsGetDto appUnitsGetDto = _mapper.Map <AppUnitsGetDto>(appUnitIdUntilFind);
                    resultDto.AppUnitIdUntilGetDto = appUnitsGetDto;
                }
                if (AppVariablesFind != null)
                {
                    AppVariablesGetDto appVariablesGetDto = _mapper.Map <AppVariablesGetDto>(AppVariablesFind);
                    resultDto.AppVariablesGetDto = appVariablesGetDto;
                }


                response.Data = resultDto;
                response.Meta = metadata;
                return(response);
            }
            catch (Exception ex)
            {
                metadata.IsValid = false;
                metadata.Message = ex.InnerException.Message;
                response.Data    = null;
                response.Meta    = metadata;
                return(response);
            }
        }