Skip to content

Commit

Permalink
refactor: 리뷰 수정 API 리팩터링
Browse files Browse the repository at this point in the history
  • Loading branch information
punchdrunkard committed Aug 28, 2024
1 parent 9c65ad9 commit 776677e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public ResponseEntity<BasicResponse<ReviewLikeResponse>> updateReviewLike(
@Operation(summary = "리뷰 수정", description = "리뷰를 수정합니다.")
public ResponseEntity<BasicResponse<ReviewIdResponse>> updateReview(
@AuthUser User user,
@Parameter(name = "data", schema = @Schema(type = "string", format = "binary"), description = "등록할 리뷰 데이터")
@Parameter(name = "data", schema = @Schema(type = "string", format = "binary"), description = "수정할 리뷰 데이터")
@RequestPart(value = "data") @Valid ReviewUpdateRequest request,
@RequestPart(value = "images", required = false) List<MultipartFile> images,
@PathVariable Long reviewId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public interface ReviewImageRepository extends JpaRepository<ReviewImage, Long>
@Query("SELECT ri.imageUrl FROM ReviewImage ri WHERE ri.reviewId = :reviewId")
List<String> findImageUrlByReviewId(@Param("reviewId") Long reviewId);

@Query("SELECT ri.imageUrl FROM ReviewImage ri WHERE ri.id IN :imageIds")
List<String> findImageUrlByIdIn(List<Long> imageIds);

void deleteByIdIn(List<Long> imageIds);

void deleteByReviewId(Long reviewId);
Expand Down
60 changes: 43 additions & 17 deletions src/main/java/com/odiga/fiesta/review/service/ReviewService.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,40 +132,66 @@ public ReviewIdResponse updateReview(Long userId, Long reviewId, ReviewUpdateReq
validateMyReview(userId, reviewId);
validateDeletedImages(request.getDeletedImages(), reviewId);

long totalImageCount =
reviewImageRepository.countByReviewId(reviewId) + (isNull(images) ? 0L : images.size())
- (isNull(request.getDeletedImages()) ? 0L : request.getDeletedImages().size());
long existingImageCount = reviewImageRepository.countByReviewId(reviewId);
long newImageCount = (images == null) ? 0L : images.size();
long deletedImageCount = (request.getDeletedImages() == null) ? 0L : request.getDeletedImages().size();

long totalImageCount = existingImageCount + newImageCount - deletedImageCount;

if (totalImageCount > 3) {
throw new CustomException(REVIEW_IMAGE_COUNT_EXCEEDED);
}

Review review = reviewRepository.findById(reviewId).orElseThrow(() -> new CustomException(REVIEW_NOT_FOUND));
Review review = reviewRepository.findById(reviewId)
.orElseThrow(() -> new CustomException(REVIEW_NOT_FOUND));

// 리뷰 내용 수정
review.updateRating((int)(request.getRating() * 10));
review.updateContent(request.getContent());
updateReviewContent(request, review);

// 리뷰 키워드 수정
reviewKeywordRepository.deleteByReviewId(reviewId);
saveReviewKeywords(request.getKeywordIds(), review);
updateReviewKeywords(request, review);

// 리뷰 이미지 수정

// delete 된 이미지를 s3 에서 제거
reviewImageRepository.findImageUrlByReviewId(reviewId).forEach(
imageUrl -> fileUtils.removeFile(imageUrl, REVIEW_DIR_NAME)
);
// delete 된 이미지를 db 에서 제거
reviewImageRepository.deleteByIdIn(request.getDeletedImages());
// 새로 추가된 이미지를 db 에 저장
createReviewImages(images, review);
updateReviewImages(request, images, review);

return ReviewIdResponse.builder()
.reviewId(review.getId())
.build();
}

private void updateReviewContent(ReviewUpdateRequest request, Review review) {
review.updateRating((int)(request.getRating() * 10));
review.updateContent(request.getContent());
}

private void updateReviewKeywords(ReviewUpdateRequest request, Review review) {
reviewKeywordRepository.deleteByReviewId(review.getId());
saveReviewKeywords(request.getKeywordIds(), review);
}

private void updateReviewImages(ReviewUpdateRequest request, List<MultipartFile> images, Review review) {
// 삭제된 이미지를 S3에서 제거한 후 DB에서 제거
if (request.getDeletedImages() != null && !request.getDeletedImages().isEmpty()) {
// 먼저 삭제할 이미지들의 URL을 DB에서 조회
List<String> imageUrlsToDelete = reviewImageRepository.findImageUrlByIdIn(request.getDeletedImages());

// S3에서 이미지 파일 삭제
imageUrlsToDelete.forEach(imageUrl -> {
try {
fileUtils.removeFile(imageUrl, REVIEW_DIR_NAME);
} catch (Exception e) {
log.error("Failed to delete image from S3: " + imageUrl, e);
}
});

// S3에서 성공적으로 삭제된 후, DB에서 해당 이미지 정보 삭제
reviewImageRepository.deleteByIdIn(request.getDeletedImages());
}

// 새로 추가된 이미지를 DB에 저장
createReviewImages(images, review);
}

@Transactional
public ReviewIdResponse deleteReview(Long userId, Long reviewId) {

Expand Down

0 comments on commit 776677e

Please sign in to comment.