Exemplo n.º 1
0
        public ActionResult Checkout(int bookId)
        {
            BookCheckout thisCheckout = _db.BookCheckout.FirstOrDefault(checkouts => checkouts.CheckoutId == bookId);

            ViewBag.BookId = new SelectList(_db.Books, "BookId", "Title");
            return(View(thisCheckout));
        }
        private static BookCheckoutDocument Map(BookCheckout checkout)
        {
            var result = Mapper.Map <BookCheckoutDocument>(checkout);

            result.Borrower = Mapper.Map <BorrowerDocument>(checkout.Borrower);
            return(result);
        }
        public async Task <(List <ValidationResult> Result, CheckoutDto Checkout)> CheckoutBook(CheckoutDto vm)
        {
            results.Clear();
            try
            {
                var user = await _userServ.GetUser(vm.UserId);

                if (user == null)
                {
                    results.Add(new ValidationResult("Invalid User ID."));
                    return(results, null);
                }
                //calculate the elapsed date
                int days        = 10;
                var ElapsedDate = DateTime.Now.AddBusinessDays(days);

                var checkout = new Checkout();
                checkout.Id           = Guid.NewGuid();
                checkout.UserId       = vm.UserId;
                checkout.CheckoutDate = DateTime.Now.GetDateUtcNow();
                checkout.ReturnDate   = ElapsedDate;
                checkout.CreatedOn    = DateTime.Now.GetDateUtcNow();
                checkout.ModifiedOn   = DateTime.Now.GetDateUtcNow();

                this.UnitOfWork.BeginTransaction();

                if (vm.SelectedBooks?.Count > 0)
                {
                    foreach (var userStore in vm.SelectedBooks)
                    {
                        //get details of selected book
                        var checkBook = _bookServ.FirstOrDefault(s => s.Id == userStore.Book_Id);
                        if (checkBook == null)
                        {
                            results.Add(new ValidationResult("Selected Book's are Wrong"));
                            return(results, null);
                        }
                        checkBook.ISBN = checkBook.ISBN;

                        var bookCheckout = new BookCheckout()
                        {
                            Book       = checkBook,
                            CheckoutId = checkout.Id,
                            CreatedOn  = DateTime.Now.GetDateUtcNow(),
                            ModifiedOn = DateTime.Now.GetDateUtcNow()
                        };

                        checkout.BookCheckouts.Add(bookCheckout);
                    }
                }
                await this.AddAsync(checkout);

                await this.UnitOfWork.CommitAsync();
            }
            catch (Exception ex)
            {
                results.Add(new ValidationResult($"Unable to Checkout! \n {ex.Message}"));
            }
            return(results, vm);
        }
        public BookCheckoutDocument Put(int bookshelfId, int bookId, int borrowerId, BookCheckoutDocument document)
        {
            var bookShelf = bookShelfRepository.Get(bookshelfId);

            if (bookShelf == null)
            {
                throw new ResourceNotFoundException("Book shelf not Found");
            }

            var book = bookRepository.Get(bookId);

            if (book == null)
            {
                throw new ResourceNotFoundException("Book not Found");
            }

            var bookOnShelf = bookInShelfRepository.Query()
                              .FirstOrDefault(x => x.BookShelfId == bookshelfId && x.BookId == bookId);

            if (bookOnShelf == null)
            {
                throw new ResourceNotFoundException("Book is not in the self specified");
            }

            var borrower = borrowerRepository.Get(borrowerId);

            if (borrower == null)
            {
                throw new ResourceNotFoundException("Borrower not Found");
            }


            var checkouts = checkoutRepository.Query().Where(x => x.BookOnShelfId == bookOnShelf.Id && (x.ReturnOn == null || x.ReturnOn > DateTime.Today))
                            .ToList();

            if (checkouts.Any(x => x.BorrowerId != borrowerId))
            {
                throw new ResourceNotFoundException("Book is already checked out to a diffrent borrower");
            }

            checkouts.ForEach(c => checkoutRepository.Delete(c));

            var checkout = new BookCheckout
            {
                BorrowerId    = borrowerId,
                CheckedOutAt  = document.CheckedOutAt,
                BookOnShelfId = bookOnShelf.Id,
                BookOnShelf   = bookOnShelf,
                Comment       = document.Comment,
                ReturnOn      = document.ReturnOn,
                Borrower      = borrower
            };

            checkoutRepository.Save(checkout);

            return(Map(checkout));
        }
        public async Task <(List <ValidationResult> Result, CheckoutDto Checkout)> CheckInBook(CheckoutDto vm, Guid checkId)
        {
            results.Clear();
            try
            {
                var checkout = this.GetAll(1, 1, a => a.Id, a => a.Id == checkId, OrderBy.Descending).FirstOrDefault();
                if (checkout == null || checkout.Id != checkId)
                {
                    results.Add(new ValidationResult("Invalid Checkout ID."));
                    return(results, null);
                }

                var user = await _userServ.GetUser(vm.UserId);

                if (user == null)
                {
                    results.Add(new ValidationResult("Invalid User ID."));
                    return(results, null);
                }
                decimal pay = 0;
                if (DateTime.Today > checkout.ReturnDate)
                {
                    pay += 200;
                }
                else
                {
                    pay += 0;
                }

                if (checkout.BookCheckouts.Any())
                {
                    checkout.BookCheckouts.Clear();
                }

                if (vm.SelectedBooks?.Count > 0)
                {
                    foreach (var userStore in vm.SelectedBooks)
                    {
                        //get details of selected book
                        var checkBook = _bookServ.FirstOrDefault(s => s.Id == userStore.Book_Id);
                        if (checkBook == null)
                        {
                            results.Add(new ValidationResult("Selected Book's are Wrong"));
                            return(results, null);
                        }
                        checkBook.ISBN = checkBook.ISBN;

                        var bookCheckout = new BookCheckout()
                        {
                            Book       = checkBook,
                            CheckoutId = checkout.Id,
                            CreatedOn  = DateTime.Now.GetDateUtcNow(),
                            ModifiedOn = DateTime.Now.GetDateUtcNow()
                        };

                        checkout.BookCheckouts.Add(bookCheckout);
                    }
                }
                checkout.ModifiedOn    = DateTime.Now.GetDateUtcNow();
                checkout.CheckInDate   = DateTime.Now.GetDateUtcNow();
                checkout.OverDueAmount = pay;

                this.UnitOfWork.BeginTransaction();
                await this.UpdateAsync(checkout);

                await this.UnitOfWork.CommitAsync();

                return(results, vm);
            }
            catch (Exception ex)
            {
                results.Add(new ValidationResult($"Unable to Checkout! \n {ex.Message}"));
            }
            return(results, vm);
        }