コード例 #1
0
        public Status.Loan PayoffLoan(PayoffLoanDto payoffLoan)
        {
            var loan = _loanRepository.FindLoanById(payoffLoan.LoanId);

            if (loan == null)
            {
                return(Status.Loan.Nonexistent);
            }
            else if (loan.Status == Status.Loan.Repaid)
            {
                return(Status.Loan.AlreadyRepaid);
            }

            loan.Amount -= payoffLoan.Amount;
            loan.Status  = loan.Amount == 0 ? Status.Loan.Repaid : loan.Amount > 0 ? Status.Loan.PartlyPaid : Status.Loan.OverPaid;

            if (loan.Status == Status.Loan.OverPaid)
            {
                return(Status.Loan.OverPaid);
            }

            if (_loanRepository.PayoffLoan(loan))
            {
                _userService.ChangeUserBalance(loan.BorrowerId, payoffLoan.Amount);
                _userService.ChangeUserBalance(loan.LenderId, payoffLoan.Amount * -1);
            }

            return(loan.Status);
        }