Exemple #1
0
        public Quote CreateQuote(LoanRequest request)
        {
            // Validation
            _loanRequestValidator.ValidateAndThrow(request);

            // Get lenders with required balance
            var lenders = _lendersService.GetAll().Where(x => x.Balance >= request.Amount);

            if (lenders == null || !lenders.Any())
            {
                throw new BusinessLogicException("No lenders found for the requested loan amount");
            }

            // Get cheapest lender
            var lender = lenders.OrderBy(x => x.AnnualPercentageYield).First();

            // Calculate monthly repayment
            var monthlyRepayment = _paymentsCalculator.GetMonthlyRepayments(request.Amount, lender.AnnualPercentageYield, request.Months);

            return(new Quote()
            {
                Id = new Random().Next(999), // In the real world, we could store quote and hence returning an id
                Amount = request.Amount,
                Months = request.Months,
                AnnualInterestRate = Math.Round(lender.AnnualPercentageYield * 100, 1),
                TotalRepayment = Math.Round(monthlyRepayment * request.Months, 2),
                MonthlyPayment = Math.Round(monthlyRepayment, 2),
            });
        }