Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Feat: 특정 강의 리뷰 필터링 구현 #25

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<Review> hasRating(Integer rating) {
return (root, query, criteriaBuilder) -> criteriaBuilder.equal(root.get("rating"), rating);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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<Review> 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<Review> reviews = reviewRepository.findByLecture(lecture, pageable);
List<Review> 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<Review> reviews = reviewRepository.findAll(spec, pageable);

List<ReviewResponseDTO.ReviewDTO> 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<ReviewResponseDTO.ReviewDTO> 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()
Expand Down
17 changes: 13 additions & 4 deletions src/main/java/miniproject/web02/web/controller/ApiController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -84,13 +85,21 @@ public ApiResponse<LectureResponseDTO.LectureDTO> getLectureInfo (@PathVariable(
@Parameter(name = "sort", description = "정렬 필드 (추천순-recommend, 최신순-createdAt)")
})
public ApiResponse<ReviewResponseDTO.ReviewListDTO> 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개 조회")
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Loading