Esempio n. 1
0
        public async Task <IActionResult> Make(int id, LoanMakeVm vm)
        {
            if (ModelState.IsValid)
            {
                var  book = bookService.GetBook(id);
                Loan loan = new Loan();

                foreach (var copy in book.Copies)
                {
                    if (!copy.OnLoan)
                    {
                        loan.BookCopyId = copy.Id;
                        copy.OnLoan     = true;
                        bookService.UpdateBookCopy(copy);
                        break;
                    }
                }

                DateTime dateOfLoan   = DateTime.Today.ToLocalTime();
                DateTime dateOfReturn = DateTime.Today.ToLocalTime().AddDays(14);
                loan.DateOfLoan   = dateOfLoan;
                loan.DateOfReturn = dateOfReturn;
                loan.MemberId     = vm.MemberId;
                loanService.AddLoan(loan);

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

            return(View(vm));
        }
Esempio n. 2
0
        public ActionResult GrantBorrowRequest(int requestId)
        {
            var request = _requestService.GetItemRequest(requestId);

            if (_requestService.UpdateItemRequest(request, true))
            {
                //update item  status to not available
                var item = _itemService.GetItemByID(request.Item.ID);
                item.Status = ItemStatus.Borrowed;
                _itemService.UpdateItem(item);

                //create loan

                var loan = new LoanViewModel();
                loan.BeginDate  = DateTime.Now;
                loan.Request    = request;
                loan.Request_ID = requestId;

                var data = Mapper.Map <LoanViewModel, Loan>(loan);
                _loanService.AddLoan(data);


                return(Json("true"));
            }
            else
            {
                return(Json("false"));
            }
        }
Esempio n. 3
0
 public IActionResult Create(Loan loan)
 {
     if (ModelState.IsValid)
     {
         _loanService.AddLoan(loan);
     }
     return(RedirectToAction(nameof(Index)));
 }
        public async Task <IActionResult> AddLoan([FromBody] LoanDto loanToAdd)
        {
            if (loanToAdd.Description == null)
            {
                return(BadRequest("The loan description is mandatory!"));
            }
            var result = await _loanService.AddLoan(loanToAdd);

            if (!result.Succeeded)
            {
                return(BadRequest(result));
            }

            return(Ok(result));
        }
Esempio 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", ""));
        }
Esempio 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", ""));
        }