From b865d5f760e21f9fb9107d3da472f3514d476e4d Mon Sep 17 00:00:00 2001 From: sebbbin Date: Wed, 22 Nov 2023 14:32:04 +0900 Subject: [PATCH] =?UTF-8?q?feat=20:=20=EB=A6=AC=EB=B7=B0=20=EA=B2=80?= =?UTF-8?q?=EC=83=89=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gallae/controller/ReviewController.java | 9 ++++++--- .../repository/review/ReviewRepositoryCustom.java | 2 +- .../repository/review/ReviewRepositoryImpl.java | 14 +++++++++----- .../gallae/service/review/ReviewService.java | 4 ++-- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/main/java/kusitms/gallae/controller/ReviewController.java b/src/main/java/kusitms/gallae/controller/ReviewController.java index b617556..61bdeed 100644 --- a/src/main/java/kusitms/gallae/controller/ReviewController.java +++ b/src/main/java/kusitms/gallae/controller/ReviewController.java @@ -51,6 +51,9 @@ public ResponseEntity> getReviewsByCategory( @RequestParam(value = "category", required = false) String category, + @Parameter(description = "타이틀 검색") + @RequestParam(value = "title", required = false) String title, + @Parameter(description = "페이지 번호") @Positive(message = "must be greater than 0") @RequestParam(value = "page", defaultValue = "0") @@ -61,9 +64,9 @@ public ResponseEntity> getReviewsByCategory( @Max(value = 100, message = "must be less than or equal to 100") @RequestParam(value = "size", defaultValue = "20") Integer pagingSize){ - - PageRequest pageRequest = PageRequest.of(pageNumber,pagingSize); - return ResponseEntity.ok(new BaseResponse<>(this.reviewService.getReviewsByCategory(category,pageRequest))); + PageRequest pageRequest = PageRequest.of(pageNumber, pagingSize); + ReviewPageRes reviewPageRes = reviewService.getReviewsByCategoryAndTitle(category, title, pageRequest); + return ResponseEntity.ok(new BaseResponse<>(reviewPageRes)); } @PostMapping("/saveReview") diff --git a/src/main/java/kusitms/gallae/repository/review/ReviewRepositoryCustom.java b/src/main/java/kusitms/gallae/repository/review/ReviewRepositoryCustom.java index 019b7a4..655beb6 100644 --- a/src/main/java/kusitms/gallae/repository/review/ReviewRepositoryCustom.java +++ b/src/main/java/kusitms/gallae/repository/review/ReviewRepositoryCustom.java @@ -5,5 +5,5 @@ import org.springframework.data.domain.Pageable; public interface ReviewRepositoryCustom { - Page findReviewDynamicCategory(String category, Pageable pageable); + Page findReviewDynamicCategory(String category, String title, Pageable pageable); } diff --git a/src/main/java/kusitms/gallae/repository/review/ReviewRepositoryImpl.java b/src/main/java/kusitms/gallae/repository/review/ReviewRepositoryImpl.java index e53e71b..e871191 100644 --- a/src/main/java/kusitms/gallae/repository/review/ReviewRepositoryImpl.java +++ b/src/main/java/kusitms/gallae/repository/review/ReviewRepositoryImpl.java @@ -37,10 +37,11 @@ public ReviewRepositoryImpl(EntityManager entityManager) { } @Override - public Page findReviewDynamicCategory(String category, Pageable pageable) { - List reviews = this.jpaQueryFactory + public Page findReviewDynamicCategory(String category, String title, Pageable pageable) { + BooleanBuilder condition = createReviewCondition(category, title); + List reviews = jpaQueryFactory .selectFrom(review) - .where(createReviewCondition(category)) + .where(condition) .orderBy(new OrderSpecifier<>(Order.DESC,review.createdAt)) .offset(pageable.getOffset()) .limit(pageable.getPageSize()) @@ -49,17 +50,20 @@ public Page findReviewDynamicCategory(String category, Pageable pageable Long totalSize = this.jpaQueryFactory .select(Wildcard.count) .from(review) - .where(createReviewCondition(category)) + .where(condition) .fetchOne(); return new PageImpl<>(reviews, pageable, Objects.requireNonNull(totalSize)); } - private BooleanBuilder createReviewCondition(String category) { + private BooleanBuilder createReviewCondition(String category, String title) { BooleanBuilder booleanBuilder = new BooleanBuilder(); if(category != null) { booleanBuilder.and(review.category.eq(category)); } + if (title != null && !title.trim().isEmpty()) { + booleanBuilder.and(review.title.contains(title)); + } return booleanBuilder; } diff --git a/src/main/java/kusitms/gallae/service/review/ReviewService.java b/src/main/java/kusitms/gallae/service/review/ReviewService.java index e7bf622..0d3954d 100644 --- a/src/main/java/kusitms/gallae/service/review/ReviewService.java +++ b/src/main/java/kusitms/gallae/service/review/ReviewService.java @@ -50,8 +50,8 @@ public class ReviewService { private S3Service s3Service; - public ReviewPageRes getReviewsByCategory(String category, Pageable pageable) { - Page reviews = reviewRepositoryCustom.findReviewDynamicCategory(category,pageable); + public ReviewPageRes getReviewsByCategoryAndTitle(String category,String title, Pageable pageable) { + Page reviews = reviewRepositoryCustom.findReviewDynamicCategory(category, title, pageable); List reviewDtos = reviews.getContent().stream() .map(review -> { ReviewDtoRes reviewDtoRes =new ReviewDtoRes();