Skip to content

Commit

Permalink
SD/feat/#59 Added getById, update and delete
Browse files Browse the repository at this point in the history
  • Loading branch information
sarajaned authored and JaafarF committed Aug 13, 2023
1 parent 3eec175 commit 139a52f
Show file tree
Hide file tree
Showing 6 changed files with 256 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,33 @@ public List<AuthorDTO> getAuthors(){
return authorService.getAllAuthors();
}

@GetMapping("{id}")
public AuthorDTO getAuthorById(@PathVariable("id") Long id){
return authorService.getAuthorById(id);
}

@PostMapping
public ResponseEntity<?> addAuthor(@Valid @RequestBody AuthorDTO authorDTO){
authorService.addAuthor(authorDTO);

return ResponseEntity.ok()
.build();
}


@PutMapping("{id}")
public ResponseEntity<?> updateAuthor(
@PathVariable("id") Long id,
@RequestBody AuthorDTO authorDTO
){
authorService.updateAuthor(id, authorDTO);

return ResponseEntity.ok()
.build();
}

@DeleteMapping("{id}")
public void deleteAuthorById(@PathVariable("id") Long id){
authorService.deleteAuthorById(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,10 @@ public interface AuthorDAO {
boolean existsAuthorByName(String s, String s1);

void addAuthor(Author author);

boolean existsAuthorById(Long id);

void updateAuthor(Author existingAuthor);

void deleteAuthorById(Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,31 @@ public List<Author> findAllAuthors() {

@Override
public Optional<Author> findById(long id) {
return Optional.empty();
return authorRepository.findById(id);
}

@Override
public boolean existsAuthorByName(String firstName, String lastName) {
return authorRepository.existsAuthorByFirstNameAndLastName(firstName, lastName);
}

@Override
public boolean existsAuthorById(Long id) {
return authorRepository.existsById(id);
}

@Override
public void addAuthor(Author author) {
authorRepository.save(author);
}

@Override
public void updateAuthor(Author author) {
authorRepository.save(author);
}

@Override
public void deleteAuthorById(Long id) {
authorRepository.deleteById(id);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.amigoscode.cohort2d.onlinebookstore.author;

import com.amigoscode.cohort2d.onlinebookstore.exceptions.DuplicateResourceException;
import com.amigoscode.cohort2d.onlinebookstore.exceptions.RequestValidationException;
import com.amigoscode.cohort2d.onlinebookstore.exceptions.ResourceNotFoundException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

Expand Down Expand Up @@ -29,4 +31,49 @@ public void addAuthor(AuthorDTO authorDTO) {
//save
authorDAO.addAuthor(AuthorDTOMapper.INSTANCE.dtoToModel(authorDTO));
}

public AuthorDTO getAuthorById(Long id) {
return AuthorDTOMapper.INSTANCE.modelToDTO(
authorDAO.findById(id)
.orElseThrow(
() -> new ResourceNotFoundException("Author with id [%s] not found.".formatted(id))));
}

public void updateAuthor(Long id, AuthorDTO updateRequest) {

// find book - check exists
Author existingAuthor = authorDAO.findById(id)
.orElseThrow(() -> new ResourceNotFoundException(
"Author with id [%s] not found.".formatted(id)
));

// check if there are any changes
if(AuthorDTOMapper.INSTANCE.modelToDTO(existingAuthor).equals(updateRequest)){
throw new RequestValidationException("No data changes found.");
}

// first name
if(!updateRequest.firstName().equals(existingAuthor.getFirstName())){
existingAuthor.setFirstName(updateRequest.firstName());
}

// last name
if(!updateRequest.lastName().equals(existingAuthor.getLastName())){
existingAuthor.setLastName(updateRequest.lastName());
}

authorDAO.updateAuthor(existingAuthor);

}

public void deleteAuthorById(Long id) {

// if author does not exist throw
if(!authorDAO.existsAuthorById(id)){
throw new ResourceNotFoundException("Author with id [%s] not found.".formatted(id));
}

//delete
authorDAO.deleteAuthorById(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ void shouldCheckAuthorExistsByName() {
verify(authorRepository).existsAuthorByFirstNameAndLastName(firstName,lastName);
}

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

// When
underTest.existsAuthorById(id);

// Then
verify(authorRepository).existsById(id);
}

@Test
void shouldAddAuthor() {
// Given
Expand All @@ -54,4 +66,43 @@ void shouldAddAuthor() {
// Then
verify(authorRepository).save(author);
}

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

// When
underTest.findById(id);

// Then
verify(authorRepository).findById(id);

}

@Test
void shouldUpdateAuthor() {
// Given
Author author = new Author(1L, "Susan", "Doyle");

// When
underTest.updateAuthor(author);

// Then
verify(authorRepository).save(author);

}

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

// When
underTest.deleteAuthorById(id);

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

}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.amigoscode.cohort2d.onlinebookstore.author;

import com.amigoscode.cohort2d.onlinebookstore.exceptions.DuplicateResourceException;
import com.amigoscode.cohort2d.onlinebookstore.exceptions.RequestValidationException;
import com.amigoscode.cohort2d.onlinebookstore.exceptions.ResourceNotFoundException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -9,6 +11,7 @@
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.List;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
Expand Down Expand Up @@ -69,6 +72,117 @@ void shouldAddAuthor() {

}

@Test
void shouldGetAuthorById() {
// Given
Long id = 1L;
Author author = new Author(id, "Jane", "Austin");
given(authorDAO.findById(id)).willReturn(Optional.of(author));

AuthorDTO expected = AuthorDTOMapper.INSTANCE.modelToDTO(author);

// When
AuthorDTO actual = underTest.getAuthorById(id);

// Then
assertThat(actual.id()).isEqualTo(expected.id());
assertThat(actual.firstName()).isEqualTo(expected.firstName());
assertThat(actual.lastName()).isEqualTo(expected.lastName());
}

@Test
void shouldUpdateAuthorById() {
// Given
Long id = 1L;
Author author = new Author(id, "John", "Doe");
given(authorDAO.findById(id)).willReturn(Optional.of(author));

AuthorDTO request = new AuthorDTO(id, "Jane", "Dawson");

// When
underTest.updateAuthor(id, request);

// Then
ArgumentCaptor<Author> authorArgumentCaptor = ArgumentCaptor.forClass(Author.class);
verify(authorDAO).updateAuthor(authorArgumentCaptor.capture());

Author capturedAuthor = authorArgumentCaptor.getValue();

assertThat(capturedAuthor.getId()).isEqualTo(request.id());
assertThat(capturedAuthor.getFirstName()).isEqualTo(request.firstName());
assertThat(capturedAuthor.getLastName()).isEqualTo(request.lastName());

}

@Test
void shouldDeleteAuthorById() {
// Given
Long id = 10L;
given(authorDAO.existsAuthorById(id)).willReturn(true);

// When
underTest.deleteAuthorById(id);

// Then
verify(authorDAO).deleteAuthorById(id);
}

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

// When && Then
assertThatThrownBy(() -> underTest.getAuthorById(id))
.isInstanceOf(ResourceNotFoundException.class)
.hasMessage("Author with id [%s] not found.".formatted(id));

}

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

// When && Then
assertThatThrownBy(() -> underTest.deleteAuthorById(id))
.isInstanceOf(ResourceNotFoundException.class)
.hasMessage("Author with id [%s] not found.".formatted(id));

verify(authorDAO, never()).deleteAuthorById(id);
}

@Test
void shouldThrowWhenUpdatingNonExistentAuthor() {
// Given
Long id = 1L;
AuthorDTO request = new AuthorDTO(id, "Jane", "Doe");

// When && Then
assertThatThrownBy(() -> underTest.updateAuthor(id, request))
.isInstanceOf(ResourceNotFoundException.class)
.hasMessage("Author with id [%s] not found.".formatted(id));

verify(authorDAO, never()).updateAuthor(any());
}

@Test
void shouldThrowWhenUpdatingWhenNoChangesFound() {
// Given
Long id = 1L;
AuthorDTO request = new AuthorDTO(id, "Jane", "Doe");
Author existingAuthor = new Author(id, "Jane", "Doe");

when(authorDAO.findById(id)).thenReturn(Optional.of(existingAuthor));


// When && Then
assertThatThrownBy(() -> underTest.updateAuthor(id, request))
.isInstanceOf(RequestValidationException.class)
.hasMessage("No data changes found.");

verify(authorDAO, never()).updateAuthor(any());
}

@Test
void shouldThrowWhenAddingAuthorWithExistingFirstNameLastName() {
Expand Down

0 comments on commit 139a52f

Please sign in to comment.