コード例 #1
0
        public void Book__InvalidModel_ShouldReloadPageWithErrors()
        {
            //Arrange
            var availability = new UserAvailability()
            {
                Id       = 113,
                DateTime = DateTime.Parse("21/09/2018 15:50"),
                TimeSpan = new TimeSpan(2, 0, 0)
            };

            _mockAvailabilityService.Setup(m => m.GetAvailability(It.IsAny <int>())).Returns(availability);

            var viewModel = new BookFormViewModel()
            {
                Availability = new UserAvailabilityViewModel()
                {
                    Id       = 113,
                    DateTime = DateTime.Parse("21/09/2018 15:50"),
                    TimeSpan = new TimeSpan(2, 0, 0)
                }
            };

            _controller.ModelState.AddModelError("test", "test");

            //Act
            var result = _controller.Book(viewModel) as ViewResult;

            //Assert
            result.Should().NotBeNull();
            result.ViewName.Should().Be("BookForm");
            result.Model.Should().BeOfType <BookFormViewModel>();
            _controller.ModelState.IsValid.Should().Be(false);
        }
コード例 #2
0
        public IActionResult Save(Book book)
        {
            // Summary
            //
            // If new - save, else - update

            if (!ModelState.IsValid)
            {
                var vm = new BookFormViewModel
                {
                    Book   = book,
                    Genres = DatabaseHelper.GetGenres()
                };

                return(View("BookForm", vm));
            }

            if (book.Id == 0)
            {
                DatabaseHelper.AddBook(book);
            }
            else
            {
                DatabaseHelper.UpdateBook(book);
            }

            return(RedirectToAction("Index", "Books"));
        }
コード例 #3
0
        public ActionResult EditBook(Book book)
        {
            var bookInDb = dbContext.Books.Single(b => b.Id == book.Id);

            if (!ModelState.IsValid)
            {
                var bookViewModel = new BookFormViewModel
                {
                    Book   = bookInDb,
                    Genres = dbContext.Genres.ToList()
                };

                return(View(bookViewModel));
            }

            bookInDb.Title          = book.Title;
            bookInDb.Summary        = book.Summary;
            bookInDb.Author         = book.Author;
            bookInDb.GenreId        = book.GenreId;
            bookInDb.NumberInStock  = book.NumberInStock;
            bookInDb.Availability   = book.Availability;
            bookInDb.ImageReference = book.ImageReference;

            dbContext.SaveChanges();

            return(RedirectToAction("Index", "Book"));
        }
コード例 #4
0
        // GET: Bookshelf/Edit/5
        public async Task <ActionResult> Edit(int id)
        {
            var item = await _context.Books.Include(b => b.BookGenres).FirstOrDefaultAsync(b => b.Id == id);

            var loggedInUser = await GetCurrentUserAsync();

            var GenreOptions = await _context.Genres
                               .Select(g => new SelectListItem()
            {
                Text = g.Name, Value = g.Id.ToString()
            })
                               .ToListAsync();

            if (item == null)
            {
                return(NotFound());
            }


            var viewModel = new BookFormViewModel()
            {
                Id             = id,
                Title          = item.Title,
                Author         = item.Author,
                GenreOptions   = GenreOptions,
                SelectGenreIds = item.BookGenres.Select(bg => bg.GenreId).ToList()
            };

            if (item.ApplicationUserId != loggedInUser.Id)
            {
                return(NotFound());
            }

            return(View(viewModel));
        }
コード例 #5
0
        public ActionResult Save(Book book)
        {
            if (!ModelState.IsValid)
            {
                var viewModel = new BookFormViewModel
                {
                    Book = new Book()
                };

                return(View("BookForm", viewModel));
            }

            if (book.Id == 0)
            {
                _context.Books.Add(book);
            }
            else
            {
                var bookInDb = _context.Books.Single(c => c.Id == book.Id);

                bookInDb.Title             = book.Title;
                bookInDb.Author            = book.Author;
                bookInDb.YearOfPublication = book.YearOfPublication;
                bookInDb.NumberInStock     = book.NumberInStock;
                bookInDb.NumberRented      = book.NumberRented;
            }
            _context.SaveChanges();

            return(RedirectToAction("Index", "Books"));
        }
コード例 #6
0
ファイル: BooksController.cs プロジェクト: aborgra/Bookshelf
        // GET: Books/Edit/5
        public async Task <ActionResult> Edit(int id)
        {
            var user = await GetUserAsync();

            var viewModel = new BookFormViewModel();
            var book      = await _context.Book.Include(b => b.BookGenres).FirstOrDefaultAsync(b => b.Id == id);

            var genreOptions = await _context.Genre.Select(g => new SelectListItem()
            {
                Text  = g.Name,
                Value = g.Id.ToString()
            }).ToListAsync();

            viewModel.Id               = book.Id;
            viewModel.Title            = book.Title;
            viewModel.Author           = book.Author;
            viewModel.GenreOptions     = genreOptions;
            viewModel.SelectedGenreIds = book.BookGenres.Select(bg => bg.GenreId).ToList();

            if (book.ApplicationUserId != user.Id)
            {
                return(NotFound());
            }

            return(View(viewModel));
        }
コード例 #7
0
        public void Create_WhenCalled_ShouldAddNewBook()
        {
            // Given
            Clock.Freeze();

            var viewModel = new BookFormViewModel
            {
                Id       = 1,
                Name     = "Selcen's test book",
                Pages    = "10",
                Date     = Clock.Now().Date.ToString("dd/MM/yyyy"),
                Reader   = 1,
                Comments = "-"
            };

            var context    = GetDbContext();
            var controller = new BooksController(context);

            // When
            var result = controller.Create(viewModel);


            // Assert
            Assert.Equal(1, context.Books.Count());
            var book = context.Books.First();

            Assert.Equal(1, book.Id);
            Assert.Equal("10", book.Pages);
            Assert.Equal(Clock.Now(), book.DateTime);
            Assert.Equal(1, book.ReaderId);
            Assert.Equal("-", book.Comments);

            Assert.Equal("Home", result.ControllerName);
            Assert.Equal("Index", result.ActionName);
        }
コード例 #8
0
        public ActionResult Save(Book book)
        {
            if (!ModelState.IsValid)
            {
                var viewModel = new BookFormViewModel(book)
                {
                    BookGenres = context.BookGenres.ToList()
                };
                return(View("BookForm", viewModel));
            }

            if (book.Id == 0)
            {
                context.Books.Add(book);
            }
            else
            {
                var bookInDB = context.Books.Single(c => c.Id == book.Id);
                bookInDB.Name          = book.Name;
                bookInDB.ReleaseDate   = book.ReleaseDate;
                bookInDB.BookGenreId   = book.BookGenreId;
                bookInDB.NumberInStock = book.NumberInStock;
                bookInDB.AddedDate     = book.AddedDate;
            }

            context.SaveChanges();
            return(RedirectToAction("Index", "Books"));
        }
コード例 #9
0
        public ActionResult PostSave(Book book)
        {
            if (!ModelState.IsValid)
            {
                var bookViewModel = new BookFormViewModel(book)
                {
                    Book       = book,
                    GenreTypes = _dbcontext.GenreTypes.ToList()
                };

                return(View("BookForm", bookViewModel));
            }

            if (book.ISBN == 0)
            {
                _dbcontext.Books.Add(book);
            }
            else
            {
                var bookInDB = _dbcontext.Books.Single(b => b.ISBN == book.ISBN);

                bookInDB.Title         = book.Title;
                bookInDB.GenreTypeId   = book.GenreTypeId;
                bookInDB.Author        = book.Author;
                bookInDB.publishedYear = book.publishedYear;
            }
            _dbcontext.SaveChanges();

            return(RedirectToAction("Library", "Books"));
        }
コード例 #10
0
        public ActionResult Save(Book book)
        {
            if (!ModelState.IsValid)
            {
                var viewModel = new BookFormViewModel
                {
                    Genres = _context.Genres.ToList()
                };
                return(View("MemberForm", viewModel));
            }

            if (book.ID == 0)
            {
                book.NumberAvailable = book.NumberInStock;
                _context.Books.Add(book);
            }
            else
            {
                var bookInDb = _context.Books.SingleOrDefault(m => m.ID == book.ID);
                bookInDb.Title       = book.Title;
                bookInDb.Author      = book.Author;
                bookInDb.ISBN        = bookInDb.ISBN;
                book.GenreID         = book.GenreID;
                book.NumberInStock   = book.NumberInStock;
                book.NumberAvailable = book.NumberInStock;
            }

            _context.SaveChanges();
            return(RedirectToAction("Index"));
        }
コード例 #11
0
ファイル: BooksController.cs プロジェクト: cldu/BookStore
        public ActionResult Save(Book book)
        {
            if (!ModelState.IsValid)
            {
                var viewModel = new BookFormViewModel
                {
                    Book       = book,
                    Categories = _context.Categories
                };
                return(View("BooksForm", viewModel));
            }

            if (book.Id == 0)
            {
                book.AddedDate = DateTime.Now;
                _context.Books.Add(book);
            }
            else
            {
                var dbBook = _context.Books.Single(b => b.Id == book.Id);

                dbBook.Name          = book.Name;
                dbBook.PublishedDate = book.PublishedDate;
                dbBook.AddedDate     = book.AddedDate;
                dbBook.NumberInStock = book.NumberInStock;
                dbBook.CategoryId    = book.CategoryId;
            }

            _context.SaveChanges();

            return(RedirectToAction("Index", "Books"));
        }
コード例 #12
0
        public IActionResult Save(Book book)
        {
            if (!ModelState.IsValid)
            {
                var viewModel = new BookFormViewModel(book);

                return(View("BookForm", viewModel));
            }

            if (book.Id == 0)
            {
                _context.Books.Add(book);
            }
            else
            {
                var bookInDb = _context.Books.Single(b => b.Id == book.Id);

                bookInDb.Name        = book.Name;
                bookInDb.ReleaseDate = book.ReleaseDate;
            };

            _context.SaveChanges();

            return(RedirectToAction("Index", "Books"));
        }
コード例 #13
0
        // GET: Books/Edit
        public ViewResult Edit(int id)
        {
            //var book = _context.Books.Single(b => b.Id == id);
            var book = _unitOfWork.Books.GetBook(id);

            var viewModel = new BookFormViewModel
            {
                Heading = "Edit a Book",
                Id      = book.Id,
                //Levels = _context.Levels.ToList(),
                Levels = _unitOfWork.Levels.GetLevels(),
                //Readers = _context.Readers.ToList(),
                Readers = _unitOfWork.Readers.GetReaders(),
                //Ratings = _context.Ratings.ToList(),
                //Ratings = _ratingRepository.GetRatings(),
                Ratings  = _unitOfWork.Ratings.GetRatings(),
                Date     = book.DateTime.ToString("d MMM yyyy"),
                Name     = book.Name,
                Pages    = book.Pages,
                Reader   = book.ReaderId,
                Level    = book.LevelId,
                Rating   = book.RatingId,
                Comments = book.Comments
            };

            return(View("BookForm", viewModel));
        }
コード例 #14
0
        public ActionResult Delete(BookFormViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                viewModel.Books = _context.Books.ToList();
                return(View("BookForm", viewModel));
            }
            //var book = _context.Books.Single(b => b.Id == viewModel.Id);
            var book = _unitOfWork.Books.GetBook(viewModel.Id);

            {
                book.Name     = viewModel.Name;
                book.Pages    = viewModel.Pages;
                book.ReaderId = viewModel.Reader;
                book.LevelId  = viewModel.Level;
                book.RatingId = viewModel.Rating;
                book.DateTime = viewModel.GetDateTime();
                book.Comments = viewModel.Comments;
            };

            //_context.Books.Remove(book);
            _unitOfWork.Books.Remove(book);
            // _context.SaveChanges();
            _unitOfWork.Complete();

            return(RedirectToAction("Index", "Home"));
        }
コード例 #15
0
        public IActionResult Edit(int?id)
        {
            ViewBag.Categories = new SelectList(db.Categories.ToList(), "CategoryID", "Name");
            ViewBag.Authors    = new MultiSelectList(db.Authors.ToList(), "AuthorID", "Name");

            var book = db.Books.SingleOrDefault(p => p.ISBN.Equals(id));

            BookFormViewModel item = new BookFormViewModel();

            item.ISBN        = book.ISBN;
            item.Title       = book.Title;
            item.PublishDate = book.PublishDate;
            item.Price       = book.Price;
            item.Quantity    = book.Quantity;
            item.CategoryID  = book.CategoryID;

            var        authorList = db.BooksAuthors.Where(p => p.ISBN.Equals(book.ISBN)).ToList();
            List <int> authors    = new List <int>();

            foreach (BookAuthor bookAuthor in authorList)
            {
                authors.Add(bookAuthor.AuthorID);
            }
            item.AuthorIDs = authors.ToArray();

            return(View(item));
        }
コード例 #16
0
        public async Task <ActionResult> Create(BookFormViewModel book)
        {
            try
            {
                var user = await GetUserAsync();

                var newBook = new Book()
                {
                    Title             = book.Title,
                    Authur            = book.Authur,
                    ApplicationUserId = user.Id
                };

                newBook.ListOfBookGenres = book.SelectedGenreIds
                                           .Select(genreId => new BookGenre()
                {
                    Book    = newBook,
                    GenreId = genreId
                }).ToList();

                _context.Book.Add(newBook);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            catch (Exception ex)
            {
                return(View());
            }
        }
コード例 #17
0
ファイル: BooksController.cs プロジェクト: aborgra/Bookshelf
        public async Task <ActionResult> Edit(int id, BookFormViewModel bookViewModel)
        {
            try
            {
                var bookDataModel = await _context.Book.Include(b => b.BookGenres).FirstOrDefaultAsync(b => b.Id == id);

                bookDataModel.Author = bookViewModel.Author;
                bookDataModel.Title  = bookViewModel.Title;
                bookDataModel.BookGenres.Clear();
                bookDataModel.BookGenres = bookViewModel.SelectedGenreIds.Select(genreId => new BookGenre()
                {
                    BookId  = bookDataModel.Id,
                    GenreId = genreId
                }).ToList();

                _context.Book.Update(bookDataModel);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            catch
            {
                return(View());
            }
        }
コード例 #18
0
        public ActionResult Save(Book book)
        {
            if (!ModelState.IsValid)
            {
                BookFormViewModel viewModel = new BookFormViewModel(book)
                {
                    //Genres = _context.Genres.ToList()
                    Genres = _unitOfWork.Genres.GetGenres()
                };
                return(View("New", viewModel));
            }

            if (book.Id == 0)
            {
                book.DateAdded       = DateTime.Now;
                book.NumberAvailable = book.NumberInStock;
                //_context.Books.Add(book);
                _unitOfWork.Books.Add(book);
            }
            else
            {
                var bookInDb = _unitOfWork.Books.Get(book.Id);
                //var bookInDb = _context.Books.Single(m => m.Id == book.Id);
                bookInDb.Name          = book.Name;
                bookInDb.GenreId       = book.GenreId;
                bookInDb.NumberInStock = book.NumberInStock;
                bookInDb.ReleaseDate   = book.ReleaseDate;
            }

            _unitOfWork.Complete();
            //_context.SaveChanges();
            return(RedirectToAction("Index", "Books"));
        }
コード例 #19
0
        public ActionResult Save(Book book)
        {
            if (!ModelState.IsValid)
            {
                var viewModel = new BookFormViewModel(book)
                {
                    BookGenres = _context.BookGenres.ToList()
                };
                return(View("BookForm", viewModel));
            }
            if (book.Id == 0)
            {
                book.DateAdded = DateTime.Now;
                _context.Books.Add(book);
            }
            else
            {
                var bookIsInDb = _context.Books.Single(b => b.Id == book.Id);
                bookIsInDb.Name          = book.Name;
                bookIsInDb.Author        = book.Author;
                bookIsInDb.NumberInStock = book.NumberInStock;
                bookIsInDb.ReleaseDate   = book.ReleaseDate;
                bookIsInDb.BookGenreId   = book.BookGenreId;
            }

            _context.SaveChanges();

            return(RedirectToAction("Index", "Books"));
        }
コード例 #20
0
        public ActionResult Save(Book book)
        {
            // the book object is stored in the request body
            if (!ModelState.IsValid)
            {
                var viewModel = new BookFormViewModel(book)
                {
                    Genres = _context.Genres.ToList()
                };
            }

            if (book.Id == 0)
            {
                book.DateAdded       = DateTime.Now;
                book.NumberAvailable = book.NumberInStock;
                _context.Books.Add(book);
            }
            else
            {
                var bookInDb = _context.Books.Single(b => b.Id == book.Id);
                bookInDb.Title           = book.Title;
                bookInDb.AuthorFirstName = book.AuthorFirstName;
                bookInDb.AuthorLastName  = book.AuthorLastName;
                bookInDb.PublishDate     = book.PublishDate;
                bookInDb.NumberInStock   = book.NumberInStock;
            }

            _context.SaveChanges();

            return(RedirectToAction("Index", "Books"));
        }
コード例 #21
0
        public async Task <ActionResult> Create(BookFormViewModel bookViewModel)
        {
            try
            {
                var user = await GetCurrentUserAsync();

                var book = new Book
                {
                    Title             = bookViewModel.Title,
                    Author            = bookViewModel.Author,
                    ApplicationUserId = user.Id,
                };

                book.BookGenres = bookViewModel.SelectGenreIds.Select(genreId => new BookGenre()
                {
                    Book    = book,
                    GenreId = genreId
                }).ToList();

                _context.Books.Add(book);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            catch
            {
                return(View());
            }
        }
コード例 #22
0
 public ActionResult Save(Book book)
 {
     if (!ModelState.IsValid)
     {
         var viewModel = new BookFormViewModel(book)
         {
             Genres = _context.Genres.ToList()
         };
         return(View("CustomerForm", viewModel));
     }
     //Create New
     if (book.Id == 0)
     {
         book.DayAdded = DateTime.Now;
         _context.Books.Add(book);
     }
     //Edit
     else
     {
         var bookInDb = _context.Books.Single(b => b.Id == book.Id);
         bookInDb.Name          = book.Name;
         bookInDb.GenreId       = book.GenreId;
         bookInDb.NumberInStock = book.NumberInStock;
         bookInDb.ReleaseDay    = book.ReleaseDay;
     }
     _context.SaveChanges();
     return(RedirectToAction("Index", "Books"));
 }
コード例 #23
0
        public ActionResult Edit(int id)
        {
            ViewBag.Years = new SelectList(Enumerable.Range(DateTime.Today.Year - 100, 101).Select(x =>

                                                                                                   new SelectListItem()
            {
                Text  = x.ToString(),
                Value = x.ToString()
            }), "Value", "Text");


            var book = _context.Books.SingleOrDefault(c => c.Id == id);

            if (book == null)
            {
                return(HttpNotFound());
            }

            var viewModel = new BookFormViewModel
            {
                Book = book
            };

            return(View("BookForm", viewModel));
        }
コード例 #24
0
        public async Task <IActionResult> PostEditAsync(Guid id, BookFormViewModel vm)
        {
            if (!ModelState.IsValid)
            {
                return(BackWithError("Error", "An error occurred while processing your request."));
            }

            try
            {
                var book = await _books.UpdateAsync(
                    new UpdateBookCommand(
                        id,
                        vm.Title,
                        vm.Slug,
                        vm.Description,
                        vm.PublicRead ? Access.Public : Access.Private,
                        vm.PublicWrite ? Access.Public : Access.Private));

                return(RedirectToAction("Book", "Notes", new { bookSlug = book.Slug }));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error");
                return(BackWithError("Error", "An error occurred while processing your request."));
            }
        }
コード例 #25
0
ファイル: BooksController.cs プロジェクト: drraken/ForteBook
        public ActionResult Save(Book book)
        {
            if (!ModelState.IsValid)
            {
                var viewModel = new BookFormViewModel
                {
                    Book       = book,
                    GenreTypes = _context.GenreTypes.ToList()
                };
                return(View("BookForm", viewModel));
            }
            if (book.Id == 0)
            {
                _context.Books.Add(book);
            }
            else
            {
                var bookInDb = _context.Books.Single(b => b.Id == book.Id);

                bookInDb.Name        = book.Name;
                bookInDb.GenreTypeId = book.GenreTypeId;
                bookInDb.Description = book.Description;
            }
            try
            {
                _context.SaveChanges();
            }
            catch (DbEntityValidationException e)
            {
                Console.WriteLine(e);
            }


            return(RedirectToAction("Index", "Books"));
        }
コード例 #26
0
ファイル: BooksController.cs プロジェクト: HKacharov/bookie
 public ActionResult Save(Book book)
 {
     if (!ModelState.IsValid)
     {
         var viewModel = new BookFormViewModel()
         {
             Book   = book,
             Genres = _context.Genres.ToList()
         };
         return(View("BookForm", viewModel));
     }
     if (book.Id == 0)
     {
         book.DateAdded       = DateTime.Now;
         book.NumberAvailable = book.NumberInStock;
         _context.Books.Add(book);
     }
     else
     {
         var bookInDb = _context.Books.Single(b => b.Id == book.Id);
         var loaned   = _context.Loans.Where(l => l.DateReturned == null && l.Book.Id == book.Id).ToList();
         bookInDb.Name            = book.Name;
         bookInDb.Author          = book.Author;
         bookInDb.GenreId         = book.GenreId;
         bookInDb.NumberInStock   = book.NumberInStock;
         bookInDb.NumberAvailable = (byte)(book.NumberInStock - loaned.Count());
     }
     _context.SaveChanges();
     return(RedirectToAction("Index", "Books"));
 }
コード例 #27
0
ファイル: BooksController.cs プロジェクト: vrkorat/MyBooks
        public ActionResult Save(Book book)
        {
            if (!ModelState.IsValid)
            {
                var viewModel = new BookFormViewModel
                {
                    Book   = book,
                    Genres = _context.Genres.ToList()
                };
                return(View("BookForm", viewModel));
            }

            //we are using Book.Id as identifier for Add New Book(Id = 0)
            if (book.Id == 0)
            {
                book.DateAdded = DateTime.Now;
                _context.Books.Add(book);
            }
            //for Update Book (Id != 0)
            else
            {
                var bookInDb = _context.Books.Single(bdata => bdata.Id == book.Id);

                bookInDb.BookName        = book.BookName;
                bookInDb.AuthorName      = book.AuthorName;
                bookInDb.PublisherName   = book.PublisherName;
                bookInDb.PublishDate     = book.PublishDate;
                bookInDb.NumberInStock   = book.NumberInStock;
                bookInDb.GenreId         = book.GenreId;
                bookInDb.NumberAvailable = book.NumberInStock;
            }
            _context.SaveChanges();
            return(RedirectToAction("Index", "Books"));
        }
コード例 #28
0
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            var book = _context.Books.Find(id);

            if (book == null)
            {
                return(HttpNotFound());
            }

            var viewModel = new BookFormViewModel
            {
                Id          = book.Id,
                Title       = book.Title,
                Author      = book.Author,
                CategoryId  = book.CategoryId,
                Description = book.Description,
                Categories  = _context.Categories.Where(m => m.IsActive).ToList()
            };

            return(View("BookForm", viewModel));
        }
コード例 #29
0
 public ActionResult Save(Book book)
 {
     if (!ModelState.IsValid)
     {
         var viewModel = new BookFormViewModel
         {
             Book   = book,
             Genres = _context.Genres.ToList()
         };
         return(View("BookForm", viewModel));
     }
     if (book.id == 0)
     {
         _context.Books.Add(book);
     }
     else
     {
         var BooksInDb = _context.Books.Single(g => g.id == book.id);
         BooksInDb.id          = book.id;
         BooksInDb.bname       = book.bname;
         BooksInDb.GenreId     = book.GenreId;
         BooksInDb.ReleaseDate = book.ReleaseDate;
         BooksInDb.AvailCopies = book.AvailCopies;
     }
     _context.SaveChanges();
     return(RedirectToAction("BooksList", "Books"));
 }
コード例 #30
0
ファイル: BooksController.cs プロジェクト: emintahir/LibMan
        public ActionResult Save(Book book)
        {
            if (!ModelState.IsValid)
            {
                var bookFormViewModel = new BookFormViewModel
                {
                    Book       = new Book(),
                    Categories = _db.Categories.ToList()
                };
                return(View("BookForm", bookFormViewModel));
            }

            if (book.BookId == 0)
            {
                book.DateAddedToLibrary = DateTime.Today;
                _db.Books.Add(book);
            }
            else //book.Id != 0
            {
                var bookInDb = _db.Books.Single(b => b.BookId == book.BookId);
                bookInDb.Title         = book.Title;
                bookInDb.Author        = book.Author;
                bookInDb.Publisher     = book.Publisher;
                bookInDb.ImageAddress  = book.ImageAddress;
                bookInDb.NumberInStock = book.NumberInStock;
                bookInDb.YearPublished = book.YearPublished;
                bookInDb.Page          = book.Page;
                bookInDb.CategoryId    = book.CategoryId;
            }
            _db.SaveChanges();
            return(RedirectToAction("Index", "Books"));
        }