Ejemplo n.º 1
0
        //GET: Create Loan
        public IActionResult CreateLoan(int id)
        {
            var vm = new LoanCreateVm();

            vm.MemberList = new SelectList(memberService.GetAllMembers(), "ID", "Name");
            vm.ID         = id;
            return(View(vm));
        }
Ejemplo n.º 2
0
        //GET: Create Loan
        public IActionResult Create()
        {
            var vm = new LoanCreateVm();

            vm.BookList   = new SelectList(bookService.GetAllBooks(), "ID", "Title");
            vm.MemberList = new SelectList(memberService.GetAllMembers(), "ID", "Name");
            return(View(vm));
        }
        public ActionResult Create()
        {
            var vm = new LoanCreateVm();

            vm.SelectListBookCopy1 = new SelectList(_bookcopyService.GetAllBookCopies().Where(b => b.IsAvailable == true), "BookCopyId", "BookCopyId");
            vm.SelectListBookCopy2 = new SelectList(_bookcopyService.GetAllBookCopies().Where(b => b.IsAvailable == true), "BookCopyId", "BookCopyId");
            vm.SelectListBookCopy3 = new SelectList(_bookcopyService.GetAllBookCopies().Where(b => b.IsAvailable == true), "BookCopyId", "BookCopyId");

            vm.SelectListMemberId = new SelectList(_memberService.GetAllmembers(), "Id", "Id");

            return(View(vm));
        }
Ejemplo n.º 4
0
        // GET: Loan/Create
        public IActionResult Create()
        {
            var vm = new LoanCreateVm();

            vm.MemberList = new SelectList(memberservice.GetAllMembers(), "Id", "Name");
            //vm.MemberList = new SelectList(memberservice.GetAllMembers(), "Id", "Name");
            vm.AvailableBooksList = new SelectList(bookcopyservice.GetAllAvailableBooks(loanservice.GetAllCurrentLoans()), "Id", "Details.Title");
            foreach (var item in vm.AvailableBooksList)
            {
                item.Text += ", book-id: " + item.Value;
            }
            return(View(vm));
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> Create(LoanCreateVm vm)
        {
            if (ModelState.IsValid)
            {
                //Skapa nytt lån
                var newLoan = new Loan();
                newLoan.MemberId   = vm.MemberId;
                newLoan.LoanDate   = datetimeservice.Now;
                newLoan.bookCopyId = vm.BookCopyId;

                loanservice.AddLoan(newLoan);

                return(RedirectToAction(nameof(Index)));
            }

            return(RedirectToAction("Error", "Home", ""));
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> CreateLoan(LoanCreateVm vm)
        {
            if (ModelState.IsValid)
            {
                var newLoan  = new Loan();
                var book     = bookService.GetBookById(vm.ID);
                var bookCopy = bookService.GetCopyOfBook(book);
                newLoan.BookCopy        = bookCopy;
                newLoan.BookCopyID      = bookCopy.ID;
                newLoan.BookCopy.OnLoan = true;
                newLoan.Returned        = false;
                newLoan.LoanTime        = vm.LoanTime;
                newLoan.ReturnTime      = vm.ReturnTime;
                newLoan.MemberID        = vm.MemberID;
                book.CopiesAvailable    = bookService.GetNumberOfAvailableCopies(book);
                bookService.UpdateBookDetails(book);
                loanService.AddLoan(newLoan);

                return(RedirectToAction(nameof(Index)));
            }

            return(RedirectToAction("Error", "Home", ""));
        }
        public ActionResult Create(LoanCreateVm vm)
        {
            if (ModelState.IsValid)
            {
                var memberToCheck = _memberService.FindMember(vm.MemberId);

                // We need to check how many bookcopies the member already has borrowed
                int loanCount = 0;
                if (memberToCheck.Loans != null)
                {
                    foreach (var loan in memberToCheck.Loans)
                    {
                        // it should only take into account active loans
                        if (loan.ReturnDate < loan.StartDate)
                        {
                            var bookCopies = loan.BookCopyLoans.Where(l => l.LoanId == loan.LoanId);

                            if (bookCopies != null)
                            {
                                loanCount += bookCopies.Count();
                            }
                        }
                    }
                }

                // okay the user might not have more then the max amount of books to borrow
                // but we also need to prevent that the new loan entry doesn't go over the max amount

                vm.BookCopyID1 ??= 0;
                vm.BookCopyID2 ??= 0;
                vm.BookCopyID3 ??= 0;
                int[] bookCopyIDs = new[] { (int)vm.BookCopyID1, (int)vm.BookCopyID2, (int)vm.BookCopyID3 };

                var nrBookCopiesToBorrow = 0;

                for (var i = 0; i < bookCopyIDs.Length; i++)
                {
                    if (bookCopyIDs[i] != 0)
                    {
                        nrBookCopiesToBorrow++;
                    }
                }

                // So let's say the member gets to borrow 3 books per loan and 9 books max in total
                // So the member can add loans untill the total amount of loans reaches over 9 books
                // We therefore count the total amount of books in order to decide if this loan request is valid
                // Loan count stands for the books already borrowed, the nr of copies to borrow is the number of books he/she now wants to borrow
                var totalBookCountToCheck = loanCount + nrBookCopiesToBorrow;

                // If the totalBookCountToCheck is higher then 9 no new loan, we return the view. The member might need to take away books.
                // We also perform a check if the entered books to borrow are not the same - we also take into account the situation where the member borrows only
                // one  book - results in two zero's, which should not lead to redirect to view...
                if (memberToCheck.Loans != null && totalBookCountToCheck > 9 || (vm.BookCopyID1 == vm.BookCopyID2 && vm.BookCopyID1 != 0 || vm.BookCopyID1 == vm.BookCopyID3 && vm.BookCopyID1 != 0 || vm.BookCopyID2 == vm.BookCopyID3 && vm.BookCopyID2 != 0) || (vm.BookCopyID1 == 0 && vm.BookCopyID2 == 0 && vm.BookCopyID3 == 0))
                {
                    // We need to declare the viewmodel again , because else we'll get an exception saying the vm the bookcopyId of vm is of value int and not type selectlist
                    vm.SelectListBookCopy1 = new SelectList(_bookcopyService.GetAllBookCopies().Where(b => b.IsAvailable == true), "BookCopyId", "BookCopyId");
                    vm.SelectListBookCopy2 = new SelectList(_bookcopyService.GetAllBookCopies().Where(b => b.IsAvailable == true), "BookCopyId", "BookCopyId");
                    vm.SelectListBookCopy3 = new SelectList(_bookcopyService.GetAllBookCopies().Where(b => b.IsAvailable == true), "BookCopyId", "BookCopyId");
                    vm.SelectListMemberId  = new SelectList(_memberService.GetAllmembers(), "Id", "Id");

                    return(View(vm));
                }
                else
                {
                    //Create new loan
                    var newLoan = new Loan();
                    newLoan.MemberID = Convert.ToInt32(vm.MemberId);

                    _loanService.AddLoan(newLoan);

                    var allLoansIncludingTheNewLoan = _loanService.GetAllLoans();
                    var allLoansOrdered             = allLoansIncludingTheNewLoan.OrderBy(x => x.LoanId);
                    var lastItem = allLoansOrdered.Last();

                    var LoanId = lastItem.LoanId;

                    // Needs a sort in descending order here if the first index is 0 - if first bookcopy is not filled in but 2 and 3 for instance are
                    // If we don't do this it will generate exception concerning a problem in db 'bookcopyloans'
                    if (bookCopyIDs[0] == 0)
                    {
                        Array.Sort(bookCopyIDs);
                        Array.Reverse(bookCopyIDs);
                    }

                    for (var i = 0; i < nrBookCopiesToBorrow; i++)
                    {
                        var newBookCopyLoan = new BookCopyLoan
                        {
                            LoanId     = LoanId,
                            BookCopyId = bookCopyIDs[i]
                        };

                        _bookCopyLoanService.AddBookCopyLoan(newBookCopyLoan);

                        _bookcopyService.UpdateBookCopyDetails(bookCopyIDs[i]);
                    }

                    return(RedirectToAction(nameof(Index)));
                }
            }

            return(RedirectToAction("Error", "Home", ""));
        }