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 : 리뷰 검색 기능 추가 #35

Merged
merged 1 commit into from
Nov 22, 2023
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
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
Loading