Skip to content

Commit

Permalink
SD/feat/#59 refactored resource not found in AuthorService
Browse files Browse the repository at this point in the history
  • Loading branch information
sarajaned authored and JaafarF committed Aug 13, 2023
1 parent 139a52f commit 30ad953
Showing 1 changed file with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,15 @@ public AuthorDTO getAuthorById(Long id) {
return AuthorDTOMapper.INSTANCE.modelToDTO(
authorDAO.findById(id)
.orElseThrow(
() -> new ResourceNotFoundException("Author with id [%s] not found.".formatted(id))));
() -> getResourceNotFoundException(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)
));
.orElseThrow(
() -> getResourceNotFoundException(id));

// check if there are any changes
if(AuthorDTOMapper.INSTANCE.modelToDTO(existingAuthor).equals(updateRequest)){
Expand All @@ -69,11 +68,19 @@ public void updateAuthor(Long id, AuthorDTO updateRequest) {
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));
}
checkIfAuthorExistsOrThrow(id);

//delete
authorDAO.deleteAuthorById(id);
}

private void checkIfAuthorExistsOrThrow(Long id) {
if(!authorDAO.existsAuthorById(id)) {
throw getResourceNotFoundException(id);
}
}

private static ResourceNotFoundException getResourceNotFoundException(Long id) {
return new ResourceNotFoundException("Author with id [%s] not found.".formatted(id));
}
}

0 comments on commit 30ad953

Please sign in to comment.