public IActionResult Payments([BindRequired] CreditParams credit) { //Check model if (!ModelState.IsValid) { var res = Json(new { Error = BadRequest(ModelState).Value }); res.StatusCode = 400; return(res); } //Check downpayment else if (credit.Downpayment >= credit.Amount) { var res = Json(new { Error = "Downpayment sould be less than credit amount" }); res.StatusCode = 400; return(res); } //Calculate payments double withoutDownpayment = credit.Amount - credit.Downpayment; double percentsPow = Math.Pow(1d + credit.Interest, credit.Term); double yearPayment = withoutDownpayment * credit.Interest * (1d + 1d / (percentsPow - 1d)); var payments = new PaymentsParams(); payments.MonthlyPayment = yearPayment / 12d; payments.TotalPayment = yearPayment * credit.Term; payments.TotalInterest = payments.TotalPayment - credit.Amount; return(Json(payments)); }
public void PaymentsInvalidModel() { var credit = new CreditParams(); var controller = new CreditController(); controller.ModelState.AddModelError("some", "error"); var actionResult = controller.Payments(credit) as JsonResult; var errorField = actionResult.Value.GetType().GetProperty("Error"); Assert.NotNull(errorField); }
public void PaymentsIncorrectDownpayment() { var credit = new CreditParams() { Amount = 100000, Interest = 0.055, Downpayment = 110000, Term = 30 }; var controller = new CreditController(); var actionResult = controller.Payments(credit) as JsonResult; var errorField = actionResult.Value.GetType().GetProperty("Error"); Assert.NotNull(errorField); }
public void PaymentsWithoutDownpayment() { var credit = new CreditParams() { Amount = 100000, Interest = 0.055, Downpayment = 0, Term = 30 }; var controller = new CreditController(); var actionResult = controller.Payments(credit) as JsonResult; var payments = actionResult.Value as PaymentsParams; Assert.Equal("573.38", payments.MonthlyPayment.ToString("F2", CultureInfo.InvariantCulture)); Assert.Equal("106416.17", payments.TotalInterest.ToString("F2", CultureInfo.InvariantCulture)); Assert.Equal("206416.17", payments.TotalPayment.ToString("F2", CultureInfo.InvariantCulture)); }
public void PaymentsCorrect() { var credit = new CreditParams() { Amount = 100000, Interest = 0.055, Downpayment = 20000, Term = 30 }; var controller = new CreditController(); var actionResult = controller.Payments(credit) as JsonResult; var payments = actionResult.Value as PaymentsParams; Assert.Equal("458.70", payments.MonthlyPayment.ToString("F2", CultureInfo.InvariantCulture)); Assert.Equal("65132.94", payments.TotalInterest.ToString("F2", CultureInfo.InvariantCulture)); Assert.Equal("165132.94", payments.TotalPayment.ToString("F2", CultureInfo.InvariantCulture)); }