diff --git a/backend/src/main/java/com/funeat/review/application/ReviewService.java b/backend/src/main/java/com/funeat/review/application/ReviewService.java index a65922c8..c0e9596f 100644 --- a/backend/src/main/java/com/funeat/review/application/ReviewService.java +++ b/backend/src/main/java/com/funeat/review/application/ReviewService.java @@ -38,8 +38,10 @@ import com.funeat.review.specification.SortingReviewSpecification; import com.funeat.tag.domain.Tag; import com.funeat.tag.persistence.TagRepository; +import java.util.ArrayList; import java.util.Comparator; import java.util.List; +import java.util.Objects; import java.util.Optional; import java.util.stream.Collectors; import org.springframework.context.ApplicationEventPublisher; @@ -184,10 +186,21 @@ private List getSortingReviews(final Member member, final Prod private List addTagsToSortingReviews( final List sortingReviewDtoWithoutTags) { - return sortingReviewDtoWithoutTags.stream() - .map(reviewDto -> SortingReviewDto.toDto(reviewDto, - tagRepository.findTagsByReviewId(reviewDto.getId()))) + final List reviewIds = sortingReviewDtoWithoutTags.stream() + .map(SortingReviewDtoWithoutTag::getId) .collect(Collectors.toList()); + final List reviewTags = reviewTagRepository.findReviewTagByReviewIds(reviewIds); + + final List sortingReviewDtos = new ArrayList<>(); + for (final SortingReviewDtoWithoutTag sortingReviewDtoWithoutTag : sortingReviewDtoWithoutTags) { + final List tags = reviewTags.stream() + .filter(reviewTag -> Objects.equals(sortingReviewDtoWithoutTag.getId(), reviewTag.getReview().getId())) + .map(ReviewTag::getTag) + .collect(Collectors.toList()); + final SortingReviewDto sortingReviewDto = SortingReviewDto.toDto(sortingReviewDtoWithoutTag, tags); + sortingReviewDtos.add(sortingReviewDto); + } + return sortingReviewDtos; } private Specification getSortingSpecification(final Product product, final String sortOption, diff --git a/backend/src/main/java/com/funeat/review/persistence/ReviewTagRepository.java b/backend/src/main/java/com/funeat/review/persistence/ReviewTagRepository.java index cbdf3c3b..5e1b13e8 100644 --- a/backend/src/main/java/com/funeat/review/persistence/ReviewTagRepository.java +++ b/backend/src/main/java/com/funeat/review/persistence/ReviewTagRepository.java @@ -7,6 +7,7 @@ import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; public interface ReviewTagRepository extends JpaRepository { @@ -21,4 +22,11 @@ public interface ReviewTagRepository extends JpaRepository { void deleteByReview(final Review review); List findByReview(final Review review); + + @Query("SELECT rt " + + "FROM ReviewTag rt " + + "JOIN FETCH rt.review " + + "JOIN FETCH rt.tag " + + "WHERE rt.review.id IN :reviewIds") + List findReviewTagByReviewIds(@Param("reviewIds") final List reviewIds); } diff --git a/backend/src/test/java/com/funeat/acceptance/review/ReviewSteps.java b/backend/src/test/java/com/funeat/acceptance/review/ReviewSteps.java index d1b556d7..45636a44 100644 --- a/backend/src/test/java/com/funeat/acceptance/review/ReviewSteps.java +++ b/backend/src/test/java/com/funeat/acceptance/review/ReviewSteps.java @@ -61,10 +61,10 @@ public class ReviewSteps { .cookie("SESSION", loginCookie) .queryParam("sort", sort) .queryParam("page", page) - .queryParam("lastReviewId", lastReviewId).log().all() + .queryParam("lastReviewId", lastReviewId) .when() .get("/api/products/{product_id}/reviews", productId) - .then().log().all() + .then() .extract(); } @@ -80,7 +80,7 @@ public class ReviewSteps { return given() .when() .get("/api/ranks/products/{product_id}/reviews", productId) - .then().log().all() + .then() .extract(); }