From f370b08f01d112a1246f214aff45fff6b2a6073f Mon Sep 17 00:00:00 2001 From: 70825 Date: Fri, 27 Oct 2023 10:11:58 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=EB=A6=AC=EB=B7=B0=20=EB=AA=A9?= =?UTF-8?q?=EB=A1=9D=20=EC=A1=B0=ED=9A=8C=EC=97=90=EC=84=9C=20=ED=83=9C?= =?UTF-8?q?=EA=B7=B8=20=EC=A0=95=EB=B3=B4=EB=A5=BC=20=EA=B0=80=EC=A0=B8?= =?UTF-8?q?=EC=98=AC=20=EB=95=8C=20=EB=B0=9C=EC=83=9D=ED=95=98=EB=8A=94=20?= =?UTF-8?q?N+1=20=EB=AC=B8=EC=A0=9C=20=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../review/application/ReviewService.java | 19 ++++++++++++++++--- .../persistence/ReviewTagRepository.java | 8 ++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) 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 a65922c82..c0e9596fe 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 cbdf3c3bf..5e1b13e8f 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); } From 9e5494ac7f70e7c6d8324b9d81edc30a9d370e2a Mon Sep 17 00:00:00 2001 From: 70825 Date: Fri, 27 Oct 2023 10:14:05 +0900 Subject: [PATCH 2/2] =?UTF-8?q?refactor:=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EB=A1=9C=EA=B7=B8=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test/java/com/funeat/acceptance/review/ReviewSteps.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 d1b556d7d..45636a445 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(); }