public async Task <IActionResult> Create(CreateLoanCalculationModel model) { _logger.LogInformation("LoanCalculationController::Create"); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } return(Ok(await _loanCalculationService.CalculateLoansAsync(model))); }
/// <summary> /// Calculates available loans for a user /// </summary> /// <param name="model"></param> /// <returns>A collection of available loans for a user</returns> public async Task <List <ProductResourceModel> > CalculateLoansAsync(CreateLoanCalculationModel model) { _logger.LogInformation("LoanCalculationService::CalculateLoansAsync"); List <ProductResourceModel> resourceModel = new List <ProductResourceModel>(); var user = await _userDetailService.GetById(model.UserId); if (user == null) { throw new LoanCalculationServiceException($"User with ID {model.UserId} is not found"); } if (!_validationService.ValidateAdultAge(user.DateOfBirth)) { return(resourceModel); } var ltv = _financeCalculationsService.CalculateLTVPercentage(model.PropertyDetail.PropertyValue, model.PropertyDetail.DepositAmount); var loanCalculation = _mapper.Map <LoanCalculationResourceModel>(await _loanCalculationRepository.CreateAsync(_mapper.Map <LoanCalculation>(model))); if (ltv < 90) { var products = await _productService.GetAll(); var productListToAdd = new List <LoanCalculationProductModel>(); foreach (var product in products) { productListToAdd.Add(new LoanCalculationProductModel() { LoanCalculationId = loanCalculation.Id, ProductId = product.Id }); } loanCalculation.Products = new List <LoanCalculationProductModel>(productListToAdd); resourceModel.AddRange(products); } loanCalculation.UserDetailsId = model.UserId; loanCalculation.PropertyDetails = model.PropertyDetail; // TODO:: Save changes to DB return(resourceModel); }