From 972d1cefac7d76f9a6c1c4e4fdcce16a13c5acc8 Mon Sep 17 00:00:00 2001 From: jiyoung Date: Mon, 2 Dec 2024 20:50:54 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Feat:=20=ED=8A=B9=EC=A0=95=20?= =?UTF-8?q?=EA=B0=95=EC=9D=98=20=EB=A6=AC=EB=B7=B0=20=ED=95=84=ED=84=B0?= =?UTF-8?q?=EB=A7=81=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../LectureReviewSpecification.java | 12 +++++ .../reviewService/ReviewServiceImpl.java | 45 ++++++++++--------- .../web02/web/controller/ApiController.java | 17 +++++-- .../dto/reviewDTO/ReviewFilterRequestDTO.java | 15 +++++++ 4 files changed, 63 insertions(+), 26 deletions(-) create mode 100644 src/main/java/miniproject/web02/repository/LectureReviewSpecification.java create mode 100644 src/main/java/miniproject/web02/web/dto/reviewDTO/ReviewFilterRequestDTO.java diff --git a/src/main/java/miniproject/web02/repository/LectureReviewSpecification.java b/src/main/java/miniproject/web02/repository/LectureReviewSpecification.java new file mode 100644 index 0000000..74516c6 --- /dev/null +++ b/src/main/java/miniproject/web02/repository/LectureReviewSpecification.java @@ -0,0 +1,12 @@ +package miniproject.web02.repository; + +import miniproject.web02.domain.Review; +import org.springframework.data.jpa.domain.Specification; + +public class LectureReviewSpecification { + + public static Specification hasRating(Integer rating) { + return (root, query, criteriaBuilder) -> criteriaBuilder.equal(root.get("rating"), rating); + } + +} diff --git a/src/main/java/miniproject/web02/service/reviewService/ReviewServiceImpl.java b/src/main/java/miniproject/web02/service/reviewService/ReviewServiceImpl.java index f0a41d9..0d1ba78 100644 --- a/src/main/java/miniproject/web02/service/reviewService/ReviewServiceImpl.java +++ b/src/main/java/miniproject/web02/service/reviewService/ReviewServiceImpl.java @@ -7,6 +7,7 @@ import miniproject.web02.domain.Review; import miniproject.web02.domain.ReviewImage; import miniproject.web02.repository.LectureRepository; +import miniproject.web02.repository.LectureReviewSpecification; import miniproject.web02.repository.ReviewImageRepository; import miniproject.web02.repository.ReviewRepository; import miniproject.web02.service.S3FileStorageService; @@ -16,6 +17,7 @@ import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; +import org.springframework.data.jpa.domain.Specification; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; @@ -39,34 +41,33 @@ public ReviewResponseDTO.ReviewListDTO getLectureReviews(long lectureId, Integer Lecture lecture = lectureRepository.findById(lectureId) .orElseThrow(() -> new TempHandler(ErrorStatus.LECTURE_NOT_FOUND)); - Pageable pageable; + Specification spec = Specification.where( + (root, query, criteriaBuilder) -> criteriaBuilder.equal(root.get("lecture"), lecture)); + + if (rating != null) { + spec = spec.and(LectureReviewSpecification.hasRating(rating)); + } + + Sort sort; if ("recommend".equals(sortField)) { - pageable = PageRequest.of(page, 5, - Sort.by(Sort.Order.desc("recommend"), Sort.Order.desc("createdAt"))); + sort = Sort.by(Sort.Order.desc("likes")).and(Sort.by(Sort.Order.desc("createdAt"))); } else { - pageable = PageRequest.of(page, 5, Sort.by(Sort.Direction.DESC, sortField)); + sort = Sort.by(Sort.Order.desc("createdAt")); } - Page reviews = reviewRepository.findByLecture(lecture, pageable); - List reviewList = new ArrayList<>(reviews.getContent()); + Pageable pageable = PageRequest.of(page, 5, sort); - if (rating != null) { - reviewList = reviewList.stream() - .filter(review -> review.getRating().equals(rating)) - .collect(Collectors.toList()); - } + Page reviews = reviewRepository.findAll(spec, pageable); - List reviewDTOList = reviewList.stream() - .map(review -> { - return ReviewResponseDTO.ReviewDTO.builder() - .reviewId(review.getReviewId()) - .rating(review.getRating()) - .studyTime(review.getStudyTime()) - .likes(review.getLikes()) - .content(review.getContent()) - .createdAt(review.getCreatedAt()) - .build(); - }) + List reviewDTOList = reviews.getContent().stream() + .map(review -> ReviewResponseDTO.ReviewDTO.builder() + .reviewId(review.getReviewId()) + .rating(review.getRating()) + .studyTime(review.getStudyTime()) + .likes(review.getLikes()) + .content(review.getContent()) + .createdAt(review.getCreatedAt()) + .build()) .collect(Collectors.toList()); return ReviewResponseDTO.ReviewListDTO.builder() diff --git a/src/main/java/miniproject/web02/web/controller/ApiController.java b/src/main/java/miniproject/web02/web/controller/ApiController.java index 04512d0..f1d84a5 100644 --- a/src/main/java/miniproject/web02/web/controller/ApiController.java +++ b/src/main/java/miniproject/web02/web/controller/ApiController.java @@ -21,6 +21,7 @@ import miniproject.web02.web.dto.lectureDTO.LectureRequestDTO; import miniproject.web02.web.dto.ReviewSearchDTO.ReviewSearchResponseDTO; import miniproject.web02.web.dto.lectureDTO.LectureResponseDTO; +import miniproject.web02.web.dto.reviewDTO.ReviewFilterRequestDTO; import miniproject.web02.web.dto.reviewDTO.ReviewResponseDTO; import miniproject.web02.web.dto.totalRatingDTO.totalRatingResponseDTO; import org.springframework.http.MediaType; @@ -84,13 +85,21 @@ public ApiResponse getLectureInfo (@PathVariable( @Parameter(name = "sort", description = "정렬 필드 (추천순-recommend, 최신순-createdAt)") }) public ApiResponse getReviewList ( - @RequestParam String lectureId, + @RequestParam Long lectureId, @RequestParam(name = "page", defaultValue = "0") Integer page, @RequestParam(name = "rating", required = false) Integer rating, @RequestParam(name = "sort", defaultValue = "createdAt") String sortField) { - ReviewResponseDTO.ReviewListDTO result = reviewService.getLectureReviews( - Long.parseLong(lectureId), rating, sortField, page); - return ApiResponse.of(SuccessStatus.SUCCESS_FETCH_REVIEW_LIST, result); + ReviewFilterRequestDTO filterRequest = new ReviewFilterRequestDTO(rating, sortField); + + // 리뷰 서비스 호출 + ReviewResponseDTO.ReviewListDTO response = reviewService.getLectureReviews( + lectureId, + filterRequest.getRating() != null ? filterRequest.getRating().intValue() : null, + filterRequest.getSort(), + page + ); + + return ApiResponse.of(SuccessStatus.SUCCESS_FETCH_REVIEW_LIST, response); } @Operation(summary = "특정 강의의 리뷰 이미지 조회 API", description = "강의 상세 페이지의 최신 리뷰 이미지 3개 조회") diff --git a/src/main/java/miniproject/web02/web/dto/reviewDTO/ReviewFilterRequestDTO.java b/src/main/java/miniproject/web02/web/dto/reviewDTO/ReviewFilterRequestDTO.java new file mode 100644 index 0000000..0b169f5 --- /dev/null +++ b/src/main/java/miniproject/web02/web/dto/reviewDTO/ReviewFilterRequestDTO.java @@ -0,0 +1,15 @@ +package miniproject.web02.web.dto.reviewDTO; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.math.BigDecimal; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class ReviewFilterRequestDTO { + private Integer rating; + private String sort; +}