Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SD/feat/#58 Added book delete #60

Merged
merged 7 commits into from
Aug 13, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ public ResponseEntity<?> addBook(@Valid @RequestBody BookDTO bookDTO){
.build();
}

@DeleteMapping("{id}")
public void deleteBookById(@PathVariable("id") Long id){
bookService.deleteBookById(id);
}
@PutMapping("{id}")
public void updateBook(
@PathVariable("id") Long id,
@RequestBody BookDTO updateRequest) {
bookService.updateBook(id, updateRequest);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ public interface BookDAO {

Book addBook(Book book);

boolean existsBookWithIsbn(String isbn);
boolean existsBookByIsbn(String isbn);

Book updateBook(Book book);
boolean existsBookById(Long id);

void deleteBookById(Long id);
Book updateBook(Book book);
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,20 @@ public Book addBook(Book book) {
}

@Override
public boolean existsBookWithIsbn(String isbn) throws DuplicateResourceException {
public boolean existsBookByIsbn(String isbn) throws DuplicateResourceException {
return bookRepository.existsBooksByIsbn(isbn);
}

@Override
public boolean existsBookById(Long id) {
return bookRepository.existsBookById(id);
}

@Override
public void deleteBookById(Long id) {
bookRepository.deleteById(id);
}

public Book updateBook(Book book) {
return bookRepository.save(book);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
public interface BookRepository extends JpaRepository<Book, Long> {

boolean existsBooksByIsbn(String isbn);

boolean existsBookById(Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public BookDTO getBookById(Long id){
public void addBook(BookDTO request) {

// check if book isbn exists
if(bookDAO.existsBookWithIsbn(request.isbn())){
if(bookDAO.existsBookByIsbn(request.isbn())){
throw new DuplicateResourceException(
"Book with ISBN [%s] already exists.".formatted(request.isbn())
);
Expand All @@ -45,8 +45,17 @@ public void addBook(BookDTO request) {

}

public void deleteBookById(Long id) {

public void updateBook(Long id, BookDTO updateRequest) {
// check if book with id is present
if(!bookDAO.existsBookById(id)){
throw new ResourceNotFoundException("Book with id [%s] not found.".formatted(id));
}

bookDAO.deleteBookById(id);
}

public void updateBook(Long id, BookDTO updateRequest) {

Book existingBook = bookDAO.findById(updateRequest.id())
.orElseThrow(() -> new ResourceNotFoundException(
Expand All @@ -60,7 +69,7 @@ public void updateBook(Long id, BookDTO updateRequest) {

// isbn
if (!updateRequest.isbn().equals(existingBook.getIsbn())) {
if (bookDAO.existsBookWithIsbn(updateRequest.isbn())) {
if (bookDAO.existsBookByIsbn(updateRequest.isbn())) {
throw new DuplicateResourceException(
"Book with ISBN [%s] already exists.".formatted(updateRequest.isbn())
);
Expand Down Expand Up @@ -117,5 +126,5 @@ public void updateBook(Long id, BookDTO updateRequest) {


bookDAO.updateBook(existingBook);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,31 @@ void shouldAddBook(){
}

@Test
void shouldDeleteBook() {
// Given
Long id = 1L;

// When
underTest.deleteBookById(id);

// Then
verify(bookRepository).deleteById(id);

}

@Test
void checksExistsBookWithId() {
// Given
Long id = 1L;

// When
underTest.existsBookById(id);

// Then
verify(bookRepository).existsBookById(id);

}

void shouldUpdateBook(){
// Given
Author author = new Author(1L, "Douglas", "Norman");
Expand Down Expand Up @@ -115,16 +140,14 @@ void shouldUpdateBook(){
verify(bookRepository).save(book);
}



@Test
void checksExistsBookWithIsbn() {

// Given
String isbn = "13043953922";

// When
underTest.existsBookWithIsbn(isbn);
underTest.existsBookByIsbn(isbn);

// Then
verify(bookRepository).existsBooksByIsbn(isbn);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.amigoscode.cohort2d.onlinebookstore.author.AuthorRepository;
import com.amigoscode.cohort2d.onlinebookstore.category.Category;
import com.amigoscode.cohort2d.onlinebookstore.category.CategoryRepository;
import jakarta.validation.constraints.NotNull;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -41,8 +42,35 @@ void setUp(){
@Test
void itShouldCheckIfBookExistsByIsbn() {
// Given
Long id = 1L;
String isbn = "1234567891234";

Book book = getBook(id, isbn);
underTest.save(book);

// When
boolean actual = underTest.existsBooksByIsbn(isbn);

// Then
assertThat(actual).isTrue();
}

@Test
void shouldCheckIfBookExistsById() {
// Given
Long id = 1L;
String isbn = "1234567891234";
Book book = getBook(id, isbn);
underTest.save(book);

// When
boolean actual = underTest.existsBookById(id);

// Then
assertThat(actual).isTrue();
}

Book getBook(Long id, String isbn) {
Author author = authorRepository.save(new Author(1L, "Douglas", "Norman"));
List<Author> authors = new ArrayList<>();
authors.add(author);
Expand All @@ -64,12 +92,6 @@ void itShouldCheckIfBookExistsByIsbn() {
authors,
categories
);
underTest.save(book);

// When
boolean actual = underTest.existsBooksByIsbn(isbn);

// Then
assertThat(actual).isTrue();
return book;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ void shouldAddBook() {

// Given
String isbn = "12043953324";
BookDTO request = getBookDTO(null, isbn, "The Lord of the Rings");
given(bookDAO.existsBookWithIsbn(isbn)).willReturn(false);
BookDTO request = getBookDTO(isbn, null);
given(bookDAO.existsBookByIsbn(isbn)).willReturn(false);

// When
underTest.addBook(request);
Expand All @@ -107,7 +107,31 @@ void shouldAddBook() {
}

@Test
void shouldUpdateBook() {
void shouldDeleteBookById() {
// Given
Long id = 1L;
given(bookDAO.existsBookById(id)).willReturn(true);

// When
underTest.deleteBookById(id);

// Then
verify(bookDAO).deleteBookById(id);
}

@Test
void shouldThrowIfBookNotFoundWhenDeleting() {
// Given
Long id = 10L;

// When && Then
assertThatThrownBy(() -> underTest.deleteBookById(id))
.isInstanceOf(ResourceNotFoundException.class)
.hasMessage("Book with id [%s] not found.".formatted(id));
verify(bookDAO, never()).deleteBookById(id);
}

void shouldUpdateBook() {
// Given
Long id = 10L;
String isbn = "1234567891234";
Expand Down Expand Up @@ -196,6 +220,7 @@ void shouldThrowWhenUpdateBookReturnEmptyOptional() {
.hasMessage("Book with id [%s] not found.".formatted(id));
}


@Test
void shouldThrowWhenGetBookReturnEmptyOptional() {
// Given
Expand All @@ -214,7 +239,7 @@ void shouldThrowWhenAddingBookWithExistingIsbn() {
String isbn = "12043953321";
BookDTO request = getBookDTO(1L, isbn, "Lord of the Rings");

given(bookDAO.existsBookWithIsbn(isbn)).willReturn(true);
given(bookDAO.existsBookByIsbn(isbn)).willReturn(true);

// When && Then
assertThatThrownBy(() -> underTest.addBook(request))
Expand Down
Loading