//AutoReorderCreate
        public async Task <IActionResult> AutoReorderCreate(BookReorder bo, int?bookId, int intReorderQuantity)
        {
            Reorder neworder = new Reorder();


            //Stores newly created order into order detail
            bo.Reorder = neworder;

            bo.Reorder.IsPending = true;

            //Stores most recently updated book price into order detail price
            //TODO: Check if we need to do this lol
            bo.Price = bo.Book.BookPrice;

            //Stores order quantity and necessary order details
            bo.ReorderQuantity = intReorderQuantity;

            if (ModelState.IsValid)
            {
                _context.Add(neworder);
                _context.BookReorders.Add(bo);
                await _context.SaveChangesAsync();

                ViewBag.AddedOrder  = "Your order has been added!";
                ViewBag.CartMessage = "View your cart below";

                return(RedirectToAction("Details", new { id = bo.Reorder.ReorderID }));
            }

            return(RedirectToAction("Details", "Books", new { id = bookId }));
        }
        public async Task <IActionResult> AutoReorder(ReorderUpdateModel model)
        {
            if (ModelState.IsValid)
            {
                List <Book> books = await _context.Books.Where(o => o.CopiesOnHand <= o.ReorderPoint).ToListAsync();

                BookReorder neworder = new BookReorder();

                //var books = from e in _context.Books
                //            from p in _context.Books
                //            where e.ReorderPoint >= p.CopiesOnHand
                //            select p;

                foreach (Book x in books)
                {
                    await AddToReorder(neworder, x.BookID, 5);
                }


                await _context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(model));
        }
        public async Task <IActionResult> ManualReorder([Bind("BookReorderID,Price,ReorderQuantity")] BookReorder bookOrder)
        {
            if (ModelState.IsValid)
            {
                _context.Add(bookOrder);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(bookOrder));
        }
        public async Task <IActionResult> AddToReorder(BookReorder bo, int?bookId, int intReorderQuantity)
        {
            if (bookId == null)
            {
                return(NotFound());
            }

            if (intReorderQuantity < 1)
            {
                return(RedirectToAction("Details", "Books", new { id = bookId }));
            }

            //Finding book matching book id passed from book details page
            Book book = _context.Books.Find(bookId);

            //Stores book in book order detail
            bo.Book            = book;
            bo.ReorderQuantity = intReorderQuantity;

            //Finds if user already has an order pending
            //Assigning user to user id
            //get user info
            String id   = User.Identity.Name;
            User   user = _context.Users.Include(o => o.Reorders).ToList().FirstOrDefault(u => u.UserName == id); //NOTE: Relational data

            //If user has an order already
            if (user.Reorders.Count != 0)
            {
                if (user.Reorders.Exists(o => o.IsPending == true))
                {
                    //TODO

                    //Finds current pending order for this user and stores it in order
                    Reorder order = _context.Reorders.Include(us => us.User).Include(o => o.BookReorders).ThenInclude(o => o.Book).FirstOrDefault(u => u.User.UserName == user.UserName && u.IsPending == true);


                    BookReorder bookOrderToUpdate = new BookReorder();


                    //This is the path for updating a book already in the cart
                    try
                    {
                        //Iterating through list of book orders to add book order quantity instead of
                        //just adding a new book instance
                        foreach (BookReorder bookOrder in order.BookReorders)
                        {
                            if (bookOrder.Book == bo.Book)
                            {
                                bookOrder.ReorderQuantity = bookOrder.ReorderQuantity + bo.ReorderQuantity;
                                bookOrder.Price           = bookOrder.Book.BookPrice;


                                bookOrderToUpdate         = bookOrder;
                                bookOrderToUpdate.Reorder = order;


                                _context.Update(bookOrderToUpdate);
                                await _context.SaveChangesAsync();

                                ViewBag.AddedOrder  = "Your order has been added!";
                                ViewBag.CartMessage = "View your cart below";

                                return(RedirectToAction("Details", new { id = bookOrderToUpdate.Reorder.ReorderID }));
                            }
                        }
                    }
                    catch
                    {
                        throw;
                    }

                    //This is the path for adding a new book to the cart
                    //Stores matched order in order detail order property
                    bo.Reorder         = order;
                    bo.Price           = bo.Book.BookPrice;
                    bo.ReorderQuantity = intReorderQuantity;

                    _context.Add(bo);
                    await _context.SaveChangesAsync();

                    ViewBag.AddedOrder  = "Your order has been added!";
                    ViewBag.CartMessage = "View your cart below";

                    return(RedirectToAction("Details", new { id = bo.Reorder.ReorderID }));
                }


                //This is the path if user does not have a pending order
                Reorder neworder = new Reorder();

                neworder.User = user;

                //Stores newly created order into order detail
                bo.Reorder = neworder;

                bo.Reorder.IsPending = true;

                //Stores most recently updated book price into order detail price
                //TODO: Check if we need to do this lol
                bo.Price = bo.Book.BookPrice;

                //Stores order quantity and necessary order details
                bo.ReorderQuantity = intReorderQuantity;

                if (ModelState.IsValid)
                {
                    _context.Add(neworder);
                    _context.BookReorders.Add(bo);
                    await _context.SaveChangesAsync();

                    ViewBag.AddedOrder  = "Your order has been added!";
                    ViewBag.CartMessage = "View your cart below";

                    return(RedirectToAction("Details", new { id = bo.Reorder.ReorderID }));
                }

                return(RedirectToAction("Details", "Books", new { id = bookId }));
            }


            //This is the path if this is the user's first order
            Reorder firstorder = new Reorder();

            //Assigns user to order
            firstorder.User = user;

            //Stores order to order detail order navigational property
            bo.Reorder = firstorder;

            //Sets order to is pending so cart can persist
            bo.Reorder.IsPending = true;

            //Sets order quantity
            bo.ReorderQuantity = intReorderQuantity;

            //Stores most recently updated book price into order detail price & other stuff
            bo.Price = bo.Book.BookPrice;

            _context.Add(bo);
            await _context.SaveChangesAsync();

            ViewBag.AddedOrder  = "Your order has been added!";
            ViewBag.CartMessage = "View your cart below";

            return(RedirectToAction("Details", new { id = bo.Reorder.ReorderID }));
        }