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)); }
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")); }