diff --git a/dao/src/main/java/greencity/repository/EcoNewsCommentRepo.java b/dao/src/main/java/greencity/repository/EcoNewsCommentRepo.java index b7595a5aa0..110b137095 100644 --- a/dao/src/main/java/greencity/repository/EcoNewsCommentRepo.java +++ b/dao/src/main/java/greencity/repository/EcoNewsCommentRepo.java @@ -8,6 +8,8 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; +import java.util.List; +import java.util.Optional; @Repository public interface EcoNewsCommentRepo extends JpaRepository { @@ -22,6 +24,15 @@ public interface EcoNewsCommentRepo extends JpaRepository Page findAllByParentCommentIsNullAndEcoNewsIdOrderByCreatedDateDesc(Pageable pageable, Long ecoNewsId); + /** + * Method returns all replies to comment, specified by parentCommentId. + * + * @param parentCommentId id of comment, replies to which we get. + * @return all replies to comment, specified by parentCommentId. + */ + @Query("SELECT ec from EcoNewsComment ec where ec.parentComment.id = :parentCommentId ") + Optional> findAllByParentCommentId(Long parentCommentId); + /** * Method returns all replies to comment, specified by parentCommentId and by * page. diff --git a/service-api/src/main/java/greencity/constant/ErrorMessage.java b/service-api/src/main/java/greencity/constant/ErrorMessage.java index fe74c1a69f..1320e16fc8 100644 --- a/service-api/src/main/java/greencity/constant/ErrorMessage.java +++ b/service-api/src/main/java/greencity/constant/ErrorMessage.java @@ -128,6 +128,8 @@ public final class ErrorMessage { public static final String PLACE_NOT_FOUND_BY_ID = "The place does not exist by this id: "; public static final String PLACE_STATUS_NOT_DIFFERENT = "Place with id: %d already has this status: %s"; public static final String COMMENT_NOT_FOUND_EXCEPTION = "The comment with entered id doesn't exist"; + public static final String COMMENT_NOT_FOUND_BY_PARENT_COMMENT_ID = + "The comment with entered parent_comment_id doesn't exist"; public static final String COMMENT_PROPERTY_TYPE_NOT_FOUND = "For type comment not found this property :"; public static final String CANNOT_REPLY_THE_REPLY = "Can not make a reply to a reply"; public static final String NOT_A_CURRENT_USER = "You can't perform actions with the data of other user"; diff --git a/service/src/main/java/greencity/service/EcoNewsCommentServiceImpl.java b/service/src/main/java/greencity/service/EcoNewsCommentServiceImpl.java index aa21087d25..43bdcbc2a0 100644 --- a/service/src/main/java/greencity/service/EcoNewsCommentServiceImpl.java +++ b/service/src/main/java/greencity/service/EcoNewsCommentServiceImpl.java @@ -34,6 +34,7 @@ import org.springframework.transaction.annotation.Transactional; import java.util.List; +import java.util.Optional; import java.util.concurrent.CompletableFuture; import java.util.stream.Collectors; @@ -132,6 +133,10 @@ public PageableDto findAllComments(Pageable pageable, UserVO */ @Override public PageableDto findAllReplies(Pageable pageable, Long parentCommentId, UserVO userVO) { + Optional> checkExistComment = ecoNewsCommentRepo.findAllByParentCommentId(parentCommentId); + if (checkExistComment.isEmpty()) { + throw new NotFoundException(ErrorMessage.COMMENT_NOT_FOUND_BY_PARENT_COMMENT_ID); + } Page pages = ecoNewsCommentRepo .findAllByParentCommentIdOrderByCreatedDateDesc(pageable, parentCommentId); List ecoNewsCommentDtos = pages diff --git a/service/src/test/java/greencity/service/EcoNewsCommentServiceImplTest.java b/service/src/test/java/greencity/service/EcoNewsCommentServiceImplTest.java index 01760bd84d..9bea221c9e 100644 --- a/service/src/test/java/greencity/service/EcoNewsCommentServiceImplTest.java +++ b/service/src/test/java/greencity/service/EcoNewsCommentServiceImplTest.java @@ -202,13 +202,15 @@ void findAllReplies() { int pageSize = 3; Pageable pageable = PageRequest.of(pageNumber, pageSize); UserVO userVO = getUserVO(); - User user = getUser(); Long parentCommentId = 1L; EcoNewsComment ecoNewsCommentChild = ModelUtils.getEcoNewsComment(); ecoNewsCommentChild.setParentComment(ModelUtils.getEcoNewsComment()); ecoNewsCommentChild.setUsersLiked(new HashSet<>()); Page pages = new PageImpl<>(Collections.singletonList(ecoNewsCommentChild), pageable, 1); + when(ecoNewsCommentRepo.findAllByParentCommentId(parentCommentId)) + .thenReturn(Optional.of(List.of(ecoNewsCommentChild))); + when(ecoNewsCommentRepo.findAllByParentCommentIdOrderByCreatedDateDesc(pageable, parentCommentId)) .thenReturn(pages); when(modelMapper.map(ecoNewsCommentChild, EcoNewsCommentDto.class)) @@ -220,6 +222,28 @@ void findAllReplies() { assertEquals(4, allReplies.getTotalElements()); assertEquals(1, allReplies.getCurrentPage()); assertEquals(1, allReplies.getPage().size()); + verify(ecoNewsCommentRepo).findAllByParentCommentId(parentCommentId); + verify(ecoNewsCommentRepo).findAllByParentCommentIdOrderByCreatedDateDesc(pageable, parentCommentId); + verify(modelMapper).map(ecoNewsCommentChild, EcoNewsCommentDto.class); + } + + @Test + void findAllRepliesThrewException() { + int pageNumber = 1; + int pageSize = 3; + Pageable pageable = PageRequest.of(pageNumber, pageSize); + UserVO userVO = getUserVO(); + Long parentCommentId = 1L; + EcoNewsComment ecoNewsCommentChild = ModelUtils.getEcoNewsComment(); + ecoNewsCommentChild.setParentComment(ModelUtils.getEcoNewsComment()); + ecoNewsCommentChild.setUsersLiked(new HashSet<>()); + + when(ecoNewsCommentRepo.findAllByParentCommentId(parentCommentId)).thenReturn(Optional.empty()); + + assertThrows(NotFoundException.class, + () -> ecoNewsCommentService.findAllReplies(pageable, parentCommentId, userVO)); + + verify(ecoNewsCommentRepo).findAllByParentCommentId(parentCommentId); } @Test