From 2bfea9fd86fab2f251ad98bb51a48d2669849fd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=95=88=EC=A0=95=ED=9B=84?= Date: Tue, 22 Oct 2024 17:10:11 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20method=20=EC=B1=85=EC=9E=84=20?= =?UTF-8?q?=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../review/adapter/ReviewGetAllAdapter.java | 79 ++++++++++++------- 1 file changed, 51 insertions(+), 28 deletions(-) diff --git a/outbound/src/main/java/com/pocket/outbound/adapter/review/adapter/ReviewGetAllAdapter.java b/outbound/src/main/java/com/pocket/outbound/adapter/review/adapter/ReviewGetAllAdapter.java index 3cc714f..3668b86 100644 --- a/outbound/src/main/java/com/pocket/outbound/adapter/review/adapter/ReviewGetAllAdapter.java +++ b/outbound/src/main/java/com/pocket/outbound/adapter/review/adapter/ReviewGetAllAdapter.java @@ -39,36 +39,59 @@ public ReviewGetResponseDto getAllReviews(Long photoboothId, Pageable pageable) Page reviews = reviewRepository.findByPhotoBoothId(photoboothId, pageable); - List reviewPreviews = reviews.stream().map(review -> { - List images = reviewImageRepository.findByReviewId(review.getId()); - String imageUrl = images.isEmpty() ? "" : images.get(0).getImage().getImageUrl(); - int imageCount = images.size(); - - - // Booth Features 찾기 - List boothFeatureIds = reviewBoothFeatureRepository.findBoothFeatureIdByReviewId(review.getId()); - List boothFeatures = boothFeatureIds.stream() - .map(boothFeatureId -> boothFeatureRepository.findById(boothFeatureId) - .orElseThrow(() -> new ReviewCustomException(ReviewErrorCode.BOOTH_FEATURE_NOT_FOUND))) - .toList(); - - // Photo Features 찾기 - List photoFeatureIds = reviewPhotoFeatureRepository.findPhotoFeatureIdByReviewId(review.getId()); - List photoFeatures = photoFeatureIds.stream() - .map(photoFeatureId -> photoFeatureRepository.findById(photoFeatureId) - .orElseThrow(() -> new ReviewCustomException(ReviewErrorCode.PHOTO_FEATURE_NOT_FOUND))) - .toList(); - - List descriptions = new ArrayList<>(); - // BoothFeature descriptions - boothFeatures.forEach(boothFeature -> descriptions.add(boothFeature.getBoothFeature().getDescription())); - // PhotoFeature descriptions - photoFeatures.forEach(photoFeature -> descriptions.add(photoFeature.getPhotoFeature().getDescription())); - - return reviewOutBoundMapper.toReviewPreviewDto(descriptions, review, imageUrl, imageCount); - }).collect(Collectors.toList()); + List reviewPreviews = reviews.stream() + .map(this::createReviewPreview) + .collect(Collectors.toList()); return new ReviewGetResponseDto(totalReviewCount, reviewPreviews); } + private ReviewPreviewDto createReviewPreview(JpaReview review) { + String imageUrl = getFirstImageUrlForReview(review.getId()); + int imageCount = getReviewImageCount(review.getId()); + + List descriptions = getDescriptionsForReview(review); + + return reviewOutBoundMapper.toReviewPreviewDto(descriptions, review, imageUrl, imageCount); + } + + private String getFirstImageUrlForReview(Long reviewId) { + List images = reviewImageRepository.findByReviewId(reviewId); + return images.isEmpty() ? "" : images.get(0).getImage().getImageUrl(); + } + + private int getReviewImageCount(Long reviewId) { + List images = reviewImageRepository.findByReviewId(reviewId); + return images.size(); + } + + private List getDescriptionsForReview(JpaReview review) { + List descriptions = new ArrayList<>(); + + List boothFeatureIds = reviewBoothFeatureRepository.findBoothFeatureIdByReviewId(review.getId()); + List boothFeatures = getBoothFeatures(boothFeatureIds); + boothFeatures.forEach(boothFeature -> descriptions.add(boothFeature.getBoothFeature().getDescription())); + + List photoFeatureIds = reviewPhotoFeatureRepository.findPhotoFeatureIdByReviewId(review.getId()); + List photoFeatures = getPhotoFeatures(photoFeatureIds); + photoFeatures.forEach(photoFeature -> descriptions.add(photoFeature.getPhotoFeature().getDescription())); + + return descriptions; + } + + private List getBoothFeatures(List boothFeatureIds) { + return boothFeatureIds.stream() + .map(boothFeatureId -> boothFeatureRepository.findById(boothFeatureId) + .orElseThrow(() -> new ReviewCustomException(ReviewErrorCode.BOOTH_FEATURE_NOT_FOUND))) + .collect(Collectors.toList()); + } + + private List getPhotoFeatures(List photoFeatureIds) { + return photoFeatureIds.stream() + .map(photoFeatureId -> photoFeatureRepository.findById(photoFeatureId) + .orElseThrow(() -> new ReviewCustomException(ReviewErrorCode.PHOTO_FEATURE_NOT_FOUND))) + .collect(Collectors.toList()); + } + + }