Пример #1
0
        public ActionResult MarkOrderAsReturned(int id)
        {

            var order = uow.OrderRepository.Get(m => m.OrderId == id);



            order.OrderStatus = 4;
            order.Payment = uow.PaymentRepository.Get(m => m.PaymentID == order.PaymentRefID);

            uow.OrderRepository.Update(order);



            foreach (var b in order.OrderLine)
            {
                if (b.BookCopy.BookCopyStatus == 3)
                {
                    ReservationList reservationList = uow.ReservationListRepository.Get(m => m.bookId == b.BookId);

                    List<ReservationListUser> usersOnList =
                        uow.ReservationListUserRepository.GetAll()
                        .Where(m => m.ReservationListId == reservationList.ReservationListId)
                        .OrderByDescending(m => m.DateAddedToList).ToList();

                    ReservationListUser firstOnList = usersOnList.FirstOrDefault();


                }

                b.BookCopy.BookCopyStatus = 0;

                b.BookCopy.Book.StockLevel++;

                uow.BookRepository.Update(b.BookCopy.Book);

                uow.BookCopyRepository.Update(b.BookCopy);

                uow.OrderLineRepository.Update(b);
            }


            ApplicationUser user = uow.UserRepository.Get(m => m.Id.Equals(order.UserOrderId));

            user.CanPlaceOrder = true;

            user.AccountRestricted = false;

            uow.UserRepository.Update(user);

            uow.SaveChanges();

            TempData["ReturnedMessage"] = "Order number: " + order.OrderId + " has now been marked as returned";

            return RedirectToAction("ViewAllActiveOrders");
        }
        /***************************CLASS METHODS*************************/



        /*************************** WAITING LIST (UNFINISHED FUNCTIONALITY) *************************/

        
        /// <summary>
        /// Method which adds a user to a waiting list for a particular book
        /// </summary>
        /// <param name="user">user to be added to list</param>
        /// <param name="book">book which the waiting list applies to</param>
        /// <returns>a message stating if they have been added or not</returns>
        public string AddToWaitingList(ApplicationUser user, Book book)
        {

            var waitingList = uow.ReservationListRepository.Get(m => m.bookId == book.BookId);


            string message;


            //entity to be added to waiting list
            //includes a date added, the list they are being added to and the user being added
            ReservationListUser reservationListUser = new ReservationListUser
            {
                ReservationList = waitingList,
                User = user,
                UserId = user.Id,
                DateAddedToList = DateTime.Now

                //POSSIBLY NEED TO ADD BOOK COPY?

            };


            //if no waiting list exists create one
            if (waitingList == null)
            {
                ReservationList newReservationList = new ReservationList
                {
                    ReservationListUser = new List<ReservationListUser>(),
                    Book = book,
                    bookId = book.BookId
                };

                newReservationList.ReservationListUser.Add(reservationListUser);

                uow.ReservationListRepository.Add(newReservationList);
                uow.ReservationListUserRepository.Add(reservationListUser);

                var bookCopyToBeReserved = uow.BookCopyRepository.Get(m => m.Book.BookId == book.BookId && m.BookCopyStatus == 2);

                bookCopyToBeReserved.BookCopyStatus = 3;
                bookCopyToBeReserved.Book = book;

                uow.BookCopyRepository.Update(bookCopyToBeReserved);

                message = "added to list";


                uow.SaveChanges();


                return message;
            }

            //if waiting list exists add user to it
            else if (waitingList.ReservationListUser.Contains(reservationListUser) == false)
            {
                uow.ReservationListUserRepository.Add(reservationListUser);
                waitingList.ReservationListUser.Add(reservationListUser);
                waitingList.Book = book;
                uow.ReservationListRepository.Update(waitingList);
                uow.SaveChanges();

                message = "added to list";

                return message;
            }
            else
            {
                message = "Already on this waiting list";
                return message;
            }
        }