From e9ea47751342bb65cc26cb9e3749f821f6a0377b Mon Sep 17 00:00:00 2001 From: 70825 Date: Mon, 16 Oct 2023 16:12:47 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EB=A6=AC=EB=B7=B0=20=EC=A0=95?= =?UTF-8?q?=EB=A0=AC=20=ED=81=B4=EB=9E=98=EC=8A=A4=20=EC=82=AD=EC=A0=9C,?= =?UTF-8?q?=20=EC=83=81=EC=88=98=20=EC=9D=B4=EB=A6=84=EC=9D=84=20=EB=AA=A9?= =?UTF-8?q?=EC=A0=81=EC=97=90=20=EB=94=B0=EB=9D=BC=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../review/application/ReviewService.java | 46 +++++++++++-- .../application/SortingReviewService.java | 66 ------------------- 2 files changed, 41 insertions(+), 71 deletions(-) delete mode 100644 backend/src/main/java/com/funeat/review/application/SortingReviewService.java 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 fdfe83a1e..8fd68728b 100644 --- a/backend/src/main/java/com/funeat/review/application/ReviewService.java +++ b/backend/src/main/java/com/funeat/review/application/ReviewService.java @@ -27,12 +27,14 @@ import com.funeat.review.dto.ReviewCreateRequest; import com.funeat.review.dto.ReviewFavoriteRequest; import com.funeat.review.dto.SortingReviewDto; +import com.funeat.review.dto.SortingReviewDtoWithoutTag; import com.funeat.review.dto.SortingReviewRequest; import com.funeat.review.dto.SortingReviewsResponse; import com.funeat.review.exception.ReviewException.NotAuthorOfReviewException; import com.funeat.review.exception.ReviewException.ReviewNotFoundException; import com.funeat.review.persistence.ReviewRepository; import com.funeat.review.persistence.ReviewTagRepository; +import com.funeat.review.specification.SortingReviewSpecification; import com.funeat.tag.domain.Tag; import com.funeat.tag.persistence.TagRepository; import java.util.List; @@ -43,6 +45,7 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; +import org.springframework.data.jpa.domain.Specification; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; @@ -51,7 +54,8 @@ @Transactional(readOnly = true) public class ReviewService { - private static final int START = 0; + private static final int FIRST_PAGE = 0; + private static final int START_INDEX = 0; private static final int ONE = 1; private static final String EMPTY_URL = ""; private static final int REVIEW_PAGE_SIZE = 10; @@ -138,11 +142,11 @@ public void updateProductImage(final Long productId) { final Product product = productRepository.findById(productId) .orElseThrow(() -> new ProductNotFoundException(PRODUCT_NOT_FOUND, productId)); - final PageRequest pageRequest = PageRequest.of(START, ONE); + final PageRequest pageRequest = PageRequest.of(FIRST_PAGE, ONE); final List topFavoriteReview = reviewRepository.findPopularReviewWithImage(productId, pageRequest); if (!topFavoriteReview.isEmpty()) { - final String topFavoriteReviewImage = topFavoriteReview.get(START).getImage(); + final String topFavoriteReviewImage = topFavoriteReview.get(START_INDEX).getImage(); product.updateImage(topFavoriteReviewImage); } } @@ -154,15 +158,47 @@ public SortingReviewsResponse sortingReviews(final Long productId, final Long me final Product findProduct = productRepository.findById(productId) .orElseThrow(() -> new ProductNotFoundException(PRODUCT_NOT_FOUND, productId)); - final List sortingReviews = sortingReviewService.getSortingReviews(findMember, findProduct, request); + final List sortingReviews = getSortingReviews(findMember, findProduct, request); final int resultSize = getResultSize(sortingReviews); - final List resizeSortingReviews = sortingReviews.subList(START, resultSize); + final List resizeSortingReviews = sortingReviews.subList(START_INDEX, resultSize); final Boolean hasNext = hasNextPage(sortingReviews); return SortingReviewsResponse.toResponse(resizeSortingReviews, hasNext); } + private List getSortingReviews(final Member member, final Product product, + final SortingReviewRequest request) { + final Long lastReviewId = request.getLastReviewId(); + final String sortOption = request.getSort(); + + final Specification specification = getSortingSpecification(product, sortOption, lastReviewId); + final List sortingReviewDtoWithoutTags = reviewRepository.getSortingReview(member, + specification, sortOption); + + return addTagsToSortingReviews(sortingReviewDtoWithoutTags); + } + + private List addTagsToSortingReviews( + final List sortingReviewDtoWithoutTags) { + return sortingReviewDtoWithoutTags.stream() + .map(reviewDto -> SortingReviewDto.toDto(reviewDto, + tagRepository.findTagsByReviewId(reviewDto.getId()))) + .collect(Collectors.toList()); + } + + private Specification getSortingSpecification(final Product product, final String sortOption, + final Long lastReviewId) { + if (lastReviewId == FIRST_PAGE) { + return SortingReviewSpecification.sortingFirstPageBy(product); + } + + final Review lastReview = reviewRepository.findById(lastReviewId) + .orElseThrow(() -> new ReviewNotFoundException(REVIEW_NOT_FOUND, lastReviewId)); + + return SortingReviewSpecification.sortingBy(product, sortOption, lastReview); + } + private int getResultSize(final List sortingReviews) { if (sortingReviews.size() <= REVIEW_PAGE_SIZE) { return sortingReviews.size(); diff --git a/backend/src/main/java/com/funeat/review/application/SortingReviewService.java b/backend/src/main/java/com/funeat/review/application/SortingReviewService.java deleted file mode 100644 index 83cf8a8c5..000000000 --- a/backend/src/main/java/com/funeat/review/application/SortingReviewService.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.funeat.review.application; - -import static com.funeat.review.exception.ReviewErrorCode.REVIEW_NOT_FOUND; - -import com.funeat.member.domain.Member; -import com.funeat.product.domain.Product; -import com.funeat.review.domain.Review; -import com.funeat.review.dto.SortingReviewDto; -import com.funeat.review.dto.SortingReviewDtoWithoutTag; -import com.funeat.review.dto.SortingReviewRequest; -import com.funeat.review.exception.ReviewException.ReviewNotFoundException; -import com.funeat.review.persistence.ReviewRepository; -import com.funeat.review.specification.SortingReviewSpecification; -import com.funeat.tag.persistence.TagRepository; -import java.util.List; -import java.util.stream.Collectors; -import org.springframework.data.jpa.domain.Specification; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - -@Service -@Transactional(readOnly = true) -public class SortingReviewService { - - private static final int FIRST_PAGE = 0; - - private final ReviewRepository reviewRepository; - private final TagRepository tagRepository; - - public SortingReviewService(final ReviewRepository reviewRepository, final TagRepository tagRepository) { - this.reviewRepository = reviewRepository; - this.tagRepository = tagRepository; - } - - public List getSortingReviews(final Member member, final Product product, - final SortingReviewRequest request) { - final Long lastReviewId = request.getLastReviewId(); - final String sortOption = request.getSort(); - - final Specification specification = getSortingSpecification(product, sortOption, lastReviewId); - final List sortingReviewDtoWithoutTags = reviewRepository.getSortingReview(member, - specification, sortOption); - - return addTagsToSortingReviews(sortingReviewDtoWithoutTags); - } - - private List addTagsToSortingReviews( - final List sortingReviewDtoWithoutTags) { - return sortingReviewDtoWithoutTags.stream() - .map(reviewDto -> SortingReviewDto.toDto(reviewDto, - tagRepository.findTagsByReviewId(reviewDto.getId()))) - .collect(Collectors.toList()); - } - - private Specification getSortingSpecification(final Product product, final String sortOption, - final Long lastReviewId) { - if (lastReviewId == FIRST_PAGE) { - return SortingReviewSpecification.sortingFirstPageBy(product); - } - - final Review lastReview = reviewRepository.findById(lastReviewId) - .orElseThrow(() -> new ReviewNotFoundException(REVIEW_NOT_FOUND, lastReviewId)); - - return SortingReviewSpecification.sortingBy(product, sortOption, lastReview); - } -}