Skip to content

Commit

Permalink
refactor: 리뷰 정렬 클래스 삭제, 상수 이름을 목적에 따라 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
70825 committed Oct 16, 2023
1 parent 1c89357 commit e9ea477
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<Review> 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);
}
}
Expand All @@ -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<SortingReviewDto> sortingReviews = sortingReviewService.getSortingReviews(findMember, findProduct, request);
final List<SortingReviewDto> sortingReviews = getSortingReviews(findMember, findProduct, request);
final int resultSize = getResultSize(sortingReviews);

final List<SortingReviewDto> resizeSortingReviews = sortingReviews.subList(START, resultSize);
final List<SortingReviewDto> resizeSortingReviews = sortingReviews.subList(START_INDEX, resultSize);
final Boolean hasNext = hasNextPage(sortingReviews);

return SortingReviewsResponse.toResponse(resizeSortingReviews, hasNext);
}

private List<SortingReviewDto> getSortingReviews(final Member member, final Product product,
final SortingReviewRequest request) {
final Long lastReviewId = request.getLastReviewId();
final String sortOption = request.getSort();

final Specification<Review> specification = getSortingSpecification(product, sortOption, lastReviewId);
final List<SortingReviewDtoWithoutTag> sortingReviewDtoWithoutTags = reviewRepository.getSortingReview(member,
specification, sortOption);

return addTagsToSortingReviews(sortingReviewDtoWithoutTags);
}

private List<SortingReviewDto> addTagsToSortingReviews(
final List<SortingReviewDtoWithoutTag> sortingReviewDtoWithoutTags) {
return sortingReviewDtoWithoutTags.stream()
.map(reviewDto -> SortingReviewDto.toDto(reviewDto,
tagRepository.findTagsByReviewId(reviewDto.getId())))
.collect(Collectors.toList());
}

private Specification<Review> 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<SortingReviewDto> sortingReviews) {
if (sortingReviews.size() <= REVIEW_PAGE_SIZE) {
return sortingReviews.size();
Expand Down

This file was deleted.

0 comments on commit e9ea477

Please sign in to comment.