Example #1
0
        public async Task RemoveBook_Test()
        {
            var isbn = "1234567890";

            var options = TestUtilities.GetOptions(nameof(ThrowBookException_WhenPublishDateIsIncorrect_Test));

            using (var actContext = new LibrarySystemContext(options))
            {
                var book = await actContext.Books.AddAsync(new Book { ISBN = isbn });

                await actContext.SaveChangesAsync();
            }

            var removeBookDto = new RemoveBookDto
            {
                ISBN = isbn
            };

            using (var assertContext = new LibrarySystemContext(options))
            {
                var sut = new BookWebService(assertContext);
                await sut.RemoveBookAsync(removeBookDto);

                Assert.AreEqual(0, assertContext.Books.Count());
            }
        }
        public async Task RemoveBookAsync(RemoveBookDto removeBookDto)
        {
            var book = await dbContext.Books
                       .FirstOrDefaultAsync(bookIsbn => bookIsbn.ISBN == removeBookDto.ISBN);

            if (book == null)
            {
                throw new BookException("The book was not found!");
            }

            this.dbContext.Books.Remove(book);
            await this.dbContext.SaveChangesAsync();
        }
        public async Task <IActionResult> RemoveBook(BookIsbnViewModel vm)
        {
            try
            {
                var removeDto = new RemoveBookDto
                {
                    ISBN = vm.ISBN,
                };

                await this.service.RemoveBookAsync(removeDto);
            }
            catch (BookException ex)
            {
                return(View("Message", new MessageViewModel {
                    Message = ex.Message
                }));
            }

            return(View("Message", new MessageViewModel {
                Message = $"The book {vm.ISBN} was removed successfully!", IsSuccess = true
            }));
        }