コード例 #1
0
        public ActionResult Create(BookCreateVm vm)
        {
            if (ModelState.IsValid)
            {
                //Create new book
                var newBook = new BookDetails();
                newBook.AuthorID    = vm.AuthorId;
                newBook.Description = vm.Description;
                newBook.ISBN        = vm.ISBN;
                newBook.Title       = vm.Title;
                _bookService.AddBook(newBook);

                if (vm.NumberOfCopies != 0)
                {
                    var allBooksIncludingNewBookDetails = _bookService.GetAllBooks();
                    var allBooksOrdered = allBooksIncludingNewBookDetails.OrderBy(x => x.ID);
                    var lastItem        = allBooksOrdered.Last();

                    for (var i = 0; i < vm.NumberOfCopies; i++)
                    {
                        var newBookCopy = new BookCopy();

                        var BookDetailsID = lastItem.ID;
                        newBookCopy.DetailsId = BookDetailsID;

                        _bookCopyService.AddBookCopy(newBookCopy);
                        newBookCopy = null;
                    }
                }

                return(RedirectToAction(nameof(Index)));
            }

            return(RedirectToAction("Error", "Home", ""));
        }
コード例 #2
0
 public IActionResult AddBookCopy(int id)
 {
     _bookCopyService.AddBookCopy(id);
     return(RedirectToAction(nameof(Index)));
 }