public IActionResult EditBook(int?id) //It is a short-cut way to write Nullable<int> "?"
        {
            ViewBag.deleteBookId = id;        // need the Id from the database, so I can add the option in the EditBook -View to delete the book
            if (id == null)
            {
                ViewBag.ErrorMessage = $"Book with the respective ID:{id} cannot be found.";
                return(View("NotFound"));
            }
            //Get the book from the database
            BooksDisplayed book = _bookStore.GetSpecificBook(id);

            //Check for the book
            if (book == null)
            {
                ViewBag.ErrorMessage = $"Book with the respective ID:{id} cannot be found.";
                return(View("NotFound"));
            }

            EditBookViewModels model = new EditBookViewModels()
            {
                Id           = book.BookId,
                BooksInStore = book.BooksInStore,
                BookGenre    = book.BookGenre,
                Price        = book.Price,
                StockOfBooks = book.StockOfBooks
            };

            return(View(model));
        }
        public IActionResult DeleteBook(int?id)
        {
            //Get the book from the database
            BooksDisplayed book = _bookStore.GetSpecificBook(id);

            //Check for the book
            if (book == null)
            {
                ViewBag.ErrorMessage = $"Book with the respective ID:{id} cannot be found.";
                return(View("NotFound"));
            }
            _bookStore.DeleteBook(id);
            return(RedirectToAction("ListBooks", "AdministrationBooks"));
        }
 public IActionResult AddBook(AddBookViewModels model)
 {
     if (ModelState.IsValid)
     {
         BooksDisplayed newBook = new BooksDisplayed()
         {
             BooksInStore = model.BooksInStore,
             BookGenre    = model.BookGenre,
             StockOfBooks = model.StockOfBooks,
             Price        = model.Price
         };
         _bookStore.AddBook(newBook);
         return(RedirectToAction("ListBooks", "AdministrationBooks"));
     }
     return(View(model));
 }
Exemplo n.º 4
0
        public IActionResult EditOrder(EditOrderBooksUserViewModels model)
        {
            if (model == null)
            {
                ViewBag.ErrorMessage = $"Something has gone wrong.";
                return(View("NotFound"));
            }
            BooksDisplayed books = _bookStore.GetSpecificBook(model.BookId);

            if (books == null)
            {
                ViewBag.ErrorMessage = $"Something has gone wrong.";
                return(View("NotFound"));
            }

            UserWithBooksDB userBooks = _bookStore.GetUserBookId(model.UserBooksId);

            if (userBooks == null)
            {
                ViewBag.ErrorMessage = $"Something has gone wrong.";
                return(View("NotFound"));
            }

            if (userBooks.NrBooksOrdered < model.NrBooksOrdered)
            {
                var x = model.NrBooksOrdered - userBooks.NrBooksOrdered;
                books.StockOfBooks        = books.StockOfBooks - x;
                userBooks.NrBooksOrdered  = model.NrBooksOrdered;
                userBooks.TotalPriceBooks = model.NrBooksOrdered * books.Price;
                _bookStore.UpdateBook(books);
                _bookStore.UpdateUserBook(userBooks);
            }
            else
            {
                books.StockOfBooks        = userBooks.NrBooksOrdered - model.NrBooksOrdered;
                userBooks.NrBooksOrdered  = model.NrBooksOrdered;
                userBooks.TotalPriceBooks = model.NrBooksOrdered * books.Price;
                _bookStore.UpdateBook(books);
                _bookStore.UpdateUserBook(userBooks);
            }
            return(RedirectToAction("OrderBooks", "Home", new { id = userBooks.UserId }));
        }
Exemplo n.º 5
0
 public IActionResult DeleteBookOrdered(string id)
 {
     if (signInManager.IsSignedIn(User))
     {
         //Get the book from the database
         UserWithBooksDB userBook = _bookStore.GetUserBookId(id);
         //Check for the book
         if (userBook == null)
         {
             ViewBag.ErrorMessage = $"User Book with the respective ID:{id} cannot be found.";
             return(View("NotFound"));
         }
         BooksDisplayed bookUpdate = _bookStore.GetSpecificBook(userBook.BookId);
         bookUpdate.StockOfBooks = bookUpdate.StockOfBooks + userBook.NrBooksOrdered;
         _bookStore.UpdateBook(bookUpdate);
         _bookStore.DeleteUserBookOrder(id);
         return(RedirectToAction("OrderBooks", "Home", new { id = userBook.UserId }));
     }
     return(RedirectToAction("Index", "Home"));
 }
        public IActionResult EditBook(EditBookViewModels model)
        {
            //Get the book from the database
            BooksDisplayed book = _bookStore.GetSpecificBook(model.Id);

            //Check for the book
            if (book == null)
            {
                ViewBag.ErrorMessage = $"Book with the respective ID:{model.Id} cannot be found.";
                return(View("NotFound"));
            }

            if (ModelState.IsValid)
            {
                book.BooksInStore = model.BooksInStore;
                book.BookGenre    = model.BookGenre;
                book.StockOfBooks = model.StockOfBooks;
                book.Price        = model.Price;

                _bookStore.UpdateBook(book);
            }
            return(View(model));
        }
Exemplo n.º 7
0
        public IActionResult AddBookInBag(BookToBuyViewModels model)
        {
            //Get the book from database
            BooksDisplayed book = _bookStore.GetSpecificBook(model.BookId);

            //check if the book is null
            if (book == null)
            {
                ViewBag.ErrorMessage = $"Book with the respective ID:{model.BookId} cannot be found.";
                return(View("NotFound"));
            }

            if (ModelState.IsValid)
            {
                UserWithBooksDB userBooks = new UserWithBooksDB()
                {
                    UserId          = model.UserId,
                    BookId          = model.BookId,
                    BookToBuy       = model.BooksToBuy,
                    NrBooksOrdered  = model.BooksOrdered,
                    TotalPriceBooks = model.BooksOrdered * model.Price
                };
                if (model.BooksOrdered < book.StockOfBooks)
                {
                    book.StockOfBooks = book.StockOfBooks - model.BooksOrdered;
                    _bookStore.UpdateBook(book);
                    _bookStore.AddBookToUser(userBooks);
                    return(RedirectToAction("DisplayBooks", "Home"));
                }
                else
                {
                    return(View(model));
                }
            }
            return(View(model));
        }
Exemplo n.º 8
0
        public async Task <IActionResult> AddBookInBag(int?id)
        {
            if (signInManager.IsSignedIn(User))
            {
                if (id == null)
                {
                    ViewBag.ErrorMessage = $"Book with the respective ID:{id} cannot be found.";
                    return(View("NotFound"));
                }
                //Get the book from the database
                BooksDisplayed book = _bookStore.GetSpecificBook(id);

                //Check for the book
                if (book == null)
                {
                    ViewBag.ErrorMessage = $"Book with the respective ID:{id} cannot be found.";
                    return(View("NotFound"));
                }
                var user = await userManager.GetUserAsync(User);

                BookToBuyViewModels model = new BookToBuyViewModels()
                {
                    UserId      = user.Id,
                    Email       = user.Email,
                    BookId      = book.BookId,
                    StockOfBook = book.StockOfBooks,
                    BooksToBuy  = book.BooksInStore,
                    Price       = book.Price,
                    BookGenre   = book.BookGenre
                };
                ViewBag.bookId = book.BookId;
                ViewBag.userId = user.Id;
                return(View(model));
            }
            return(RedirectToAction("LogIn", "Account"));
        }