Skip to content

Commit

Permalink
feat : 리뷰 검색 기능 추가 (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebbbin authored Nov 22, 2023
1 parent f5fef67 commit f290bf9
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
9 changes: 6 additions & 3 deletions src/main/java/kusitms/gallae/controller/ReviewController.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public ResponseEntity<BaseResponse<ReviewPageRes>> 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")
Expand All @@ -61,9 +64,9 @@ public ResponseEntity<BaseResponse<ReviewPageRes>> 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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
import org.springframework.data.domain.Pageable;

public interface ReviewRepositoryCustom {
Page<Review> findReviewDynamicCategory(String category, Pageable pageable);
Page<Review> findReviewDynamicCategory(String category, String title, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ public ReviewRepositoryImpl(EntityManager entityManager) {
}

@Override
public Page<Review> findReviewDynamicCategory(String category, Pageable pageable) {
List<Review> reviews = this.jpaQueryFactory
public Page<Review> findReviewDynamicCategory(String category, String title, Pageable pageable) {
BooleanBuilder condition = createReviewCondition(category, title);
List<Review> reviews = jpaQueryFactory
.selectFrom(review)
.where(createReviewCondition(category))
.where(condition)
.orderBy(new OrderSpecifier<>(Order.DESC,review.createdAt))
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
Expand All @@ -49,17 +50,20 @@ public Page<Review> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public class ReviewService {
private S3Service s3Service;


public ReviewPageRes getReviewsByCategory(String category, Pageable pageable) {
Page<Review> reviews = reviewRepositoryCustom.findReviewDynamicCategory(category,pageable);
public ReviewPageRes getReviewsByCategoryAndTitle(String category,String title, Pageable pageable) {
Page<Review> reviews = reviewRepositoryCustom.findReviewDynamicCategory(category, title, pageable);
List<ReviewDtoRes> reviewDtos = reviews.getContent().stream()
.map(review -> {
ReviewDtoRes reviewDtoRes =new ReviewDtoRes();
Expand Down

0 comments on commit f290bf9

Please sign in to comment.