Пример #1
0
        public void Delete(int id)
        {
            HttpResponseMessage response = new HttpResponseMessage();

            try
            {
                LibraryRepository libraryRepository = new LibraryRepository(Constants.ConnectionString);
                libraryRepository.DeleteBook(id);
            }
            catch (DataDependencyException ex)
            {
                response.StatusCode = HttpStatusCode.InternalServerError;
                response.Content    = new StringContent(ex.ErrorMessage);
                //return response;
            }
            catch (DataLayerException ex)
            {
                response.StatusCode = HttpStatusCode.InternalServerError;
                response.Content    = new StringContent(ex.ErrorMessage);
                //return response;
            }

            response.StatusCode = HttpStatusCode.OK;
            response.Content    = new StringContent("Book deleted.");
            //return response;
        }
Пример #2
0
        public IActionResult DeleteBookForAuthor(Guid authorId, Guid id)
        {
            if (LibraryRepository.AuthorNotExists(authorId))
            {
                return(NotFound());
            }

            var book = LibraryRepository.GetBookForAuthor(authorId, id);

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

            LibraryRepository.DeleteBook(book);

            if (LibraryRepository.NotSave())
            {
                throw new Exception($"Deleting book {id} for author {authorId} failed on save.");
            }

            Logger.LogInformation(100, $"Book {id} for author {authorId} was deleted.");

            return(NoContent());
        }
Пример #3
0
        public async Task <IActionResult> DeleteConfirmed(int id)
        {
            var book = _repository.GetBook(id);

            _repository.DeleteBook(id);

            return(RedirectToAction(nameof(Index)));
        }
        public void DeleteBookTest()
        {
            AddBookTest();
            Assert.AreEqual(3, _dataLayer.GetAllBooks().Count());
            Book book = _dataLayer.GetAllBooks().First();

            _dataLayer.DeleteBook(book);
            Assert.AreEqual(2, _dataLayer.GetAllBooks().Count());
            Assert.ThrowsException <KeyNotFoundException>(() => _dataLayer.GetBook(4));
        }
        public void SaveBookTest()
        {
            DatabasePackage databasePackage = new DatabasePackage(_ConnectionString);
            SqlDataReader   reader          = null;

            try
            {
                LibraryRepository libraryRepository = new LibraryRepository(_ConnectionString);

                // Test Add Book

                Book newBook = new Book();
                newBook.Name = "Unit Test Book";

                libraryRepository.SaveBook(newBook);

                databasePackage.CommandText = "Select ID From Book Where Name = 'Unit Test Book'";
                reader = databasePackage.GetReader();

                reader.Read();

                int bookID = databasePackage.FieldToInt(reader["ID"]);

                Assert.AreNotEqual(0, bookID);

                // Test Modify Book

                Book existingBook = libraryRepository.GetBookByID(bookID);
                existingBook.Name = "Unit Test Book (Modified)";
                libraryRepository.SaveBook(existingBook);

                Book modifiedBook = libraryRepository.GetBookByID(bookID);

                Assert.AreEqual("Unit Test Book (Modified)", modifiedBook.Name);

                // Delete newly added book

                libraryRepository.DeleteBook(bookID);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
        }
        public void GetBookByIDTest()
        {
            DatabasePackage databasePackage = new DatabasePackage(_ConnectionString);
            SqlDataReader   reader          = null;

            LibraryRepository libraryRepository = new LibraryRepository(_ConnectionString);

            try
            {
                // Add a new book

                Book newBook = new Book();
                newBook.Name = "Unit Test Book";

                libraryRepository.SaveBook(newBook);

                databasePackage.CommandText = "Select ID From Book Where Name = 'Unit Test Book'";
                reader = databasePackage.GetReader();
                reader.Read();

                int bookID = databasePackage.FieldToInt(reader["ID"]);

                // Test the function

                Book book = libraryRepository.GetBookByID(bookID);
                Assert.AreEqual(book.ID, bookID);

                // Delete newly added book

                libraryRepository.DeleteBook(bookID);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
        }
        public void ReturnBookTest()
        {
            DatabasePackage databasePackage = new DatabasePackage(_ConnectionString);
            SqlDataReader   reader          = null;

            LibraryRepository libraryRepository = new LibraryRepository(_ConnectionString);

            try
            {
                // Add a new Book

                Book newBook = new Book();
                newBook.Name = "Unit Test Book";

                libraryRepository.SaveBook(newBook);

                databasePackage.CommandText = "Select ID From Book Where Name = 'Unit Test Book'";
                reader = databasePackage.GetReader();
                reader.Read();

                int bookID = databasePackage.FieldToInt(reader["ID"]);

                // Add a new Member

                Member newMember = new Member();
                newMember.Name = "Unit Test Member";

                libraryRepository.SaveMember(newMember);

                databasePackage.CommandText = "Select ID From Member Where Name = 'Unit Test Member'";
                reader = databasePackage.GetReader();
                reader.Read();

                int memberID = databasePackage.FieldToInt(reader["ID"]);

                // Issue this Book to a Member

                libraryRepository.IssueBook(bookID, memberID, DateTime.Now);

                // Test the function

                libraryRepository.ReturnBook(bookID);

                Book returnBook = libraryRepository.GetBookByID(bookID);

                Assert.AreEqual(returnBook.MemberID, 0);

                // Delete newly added book

                libraryRepository.DeleteBook(bookID);

                // Delete newly added Member

                libraryRepository.DeleteMember(memberID);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
        }