コード例 #1
0
ファイル: LoanManager.cs プロジェクト: ihsanrawi/CodingTest
        public async Task <Loan> AddNewLoan(NewLoan newloan)
        {
            newloan.RepaymentAmount = Math.Round(newloan.FundingAmount * (decimal)1.2, 3);

            var loan = new Loan
            {
                BorrowerName    = newloan.BorrowerName,
                FundingAmount   = newloan.FundingAmount,
                RepaymentAmount = newloan.RepaymentAmount,
                Deleted         = false
            };

            return(await _loanRepository.AddNewLoan(loan));
        }
コード例 #2
0
        public IActionResult BorrowList(string bookIdent)
        {
            string      userId         = _userManager.GetUserId(User);
            List <Loan> allUserLoans   = _loanRepository.GetCurrentLoansByUserId(userId).ToList();
            var         userLoansTotal = allUserLoans.Count();
            var         loanAdded      = false;

            // add the new loan to the DB if the user doesn't have five books out on loan
            if (userLoansTotal < 5)
            {
                loanAdded = _loanRepository.AddNewLoan(bookIdent, userId);
            }

            // update loan data following the addition of a new loan to the loan table
            allUserLoans.Clear();
            allUserLoans   = _loanRepository.GetCurrentLoansByUserId(userId).ToList();
            userLoansTotal = allUserLoans.Count();

            if ((userLoansTotal != 5) && (loanAdded == false))
            {
                userLoansTotal = 100;
            }

            // prepare a message for the user advising them of their loan status
            var loansLeft = (5 - userLoansTotal);

            switch (userLoansTotal)
            {
            case int i when i == 0:
                _loansMessage = "You currently have " + userLoansTotal + " loan. You may borrow up to " + loansLeft + " books.";
                break;

            case int i when i == 1:
                _loansMessage = "You currently have " + userLoansTotal + " loan. You may borrow another " + loansLeft + " books.";
                break;

            case int i when i > 1 && i <= 3:
                _loansMessage = "You currently have " + userLoansTotal + " loans. You may borrow another " + loansLeft + " books.";
                break;

            case int i when i == 4:
                _loansMessage = "You currently have " + userLoansTotal + " loans. You may borrow 1 more book.";
                break;

            case int i when i == 5:
                _loansMessage = "You currently have " + userLoansTotal + " loans. This is the maximum number of loans.";
                break;

            case int i when i == 6:
                _loansMessage = "You currently have 5 loans. This is the maximum number of loans.";
                break;

            case int i when i == 100:
                _loansMessage = "Borrow Request Failed";
                break;
            }

            // get the user's loaned books
            List <Book> bookLoans = null;

            if (userLoansTotal > 0)
            {
                bookLoans = _bookRepository.GetUsersLoanedBooks(allUserLoans, userId).ToList();
            }

            // get all available books not existing in the loan table
            List <Book> availableBooks = _bookRepository.GetAvailableBooks().ToList();

            bool tempTextColRed = true;

            if ((userLoansTotal >= 0) && (userLoansTotal < 5))
            {
                tempTextColRed = false;
            }

            var borrowListViewModel = new BorrowListViewModel()
            {
                AvailableBooks  = availableBooks,
                UserLoanedBooks = bookLoans,
                LoanBookCount   = userLoansTotal,
                UserId          = _userManager.GetUserId(User),
                LoanMessage     = _loansMessage,
                TextColorRed    = tempTextColRed
            };

            return(View(borrowListViewModel));
        }