public ActionResult <Loan> PayLoan(PayLoanContract contract) { string errorString; if (_loanValidator.PayLoanIsValid(contract, out errorString)) { return(BadRequest(new JsonResult(errorString))); } var loanItem = _loanRepository.GetLoanById(contract.LoanId); if (loanItem == null) { return(NotFound(new JsonResult("Loan is not found."))); } if (loanItem.Quantity - contract.Quantity < 0) { return(BadRequest(new JsonResult("Quantity you want to pay is bigger than amount left on the loan."))); } loanItem.Quantity -= contract.Quantity; _loanRepository.UpdateLoan(loanItem); return(Ok(loanItem)); }
public bool PayLoanIsValid(PayLoanContract contract, out string errorString) { errorString = String.Empty; if (contract.LoanId < 0) { errorString += "Loan Id cannot be smaller than 0."; } if (contract.Quantity < 0) { errorString += "Cannot pay negative quantity."; } return(errorString == String.Empty ? false : true); }