Пример #1
0
        public async Task <OperationResult <TaxCalculationResponse> > CalculateTax(TaxCalculationRequest request)
        {
            OperationResult <TaxCalculationResponse> validatorResult = _validationRuleEngine.Validate(request);

            if (validatorResult.HasErrors)
            {
                return(validatorResult);
            }

            var calculationTypeMapping = await _calculationMappingRepository.GetByPostalCodeAsync(request.PostalCode).ConfigureAwait(false);

            var taxCalculator = _taxCalculatorFactory.GetCalculator(calculationTypeMapping.CalculationType);
            var taxYear       = await _taxYearRepository.GetTaxYearAsync(_clock.GetCurrentDateTime()).ConfigureAwait(false);

            var taxAmountResult = await taxCalculator.CalculateTaxAsync(taxYear, request.AnnualIncome).ConfigureAwait(false);

            if (taxAmountResult.HasErrors)
            {
                return(taxAmountResult.MapErrors <TaxCalculationResponse>());
            }

            var response = new TaxCalculationResponse
            {
                TaxYear         = taxYear.ToString(),
                CalculationType = calculationTypeMapping.CalculationType,
                TaxAmount       = taxAmountResult.Response
            };

            await SaveCalculation(request, response, taxYear);

            return(new OperationResult <TaxCalculationResponse>(response));
        }
Пример #2
0
        protected override OperationResult Handle(AddCoinCommand command)
        {
            var result = new OperationResult();

            try
            {
                ICoinFactory coinFactory = CoinFactoryProducer.GetFactory(command.Currency);

                ICoin coin = coinFactory.GetCoin(command.Amount);

                OperationResult jarValidatorResult = _coinJarValidatorRulesEngine.Validate(coin);

                if (jarValidatorResult.HasErrors)
                {
                    result.AddErrorMessages(jarValidatorResult.GetErrorMessages());
                    return(result);
                }

                _db.AddCoin(coin);
                return(result);
            }
            catch (ApplicationException e)
            {
                result.AddErrorMessage(e.Key, e.Message);
            }

            return(result);
        }