Skip to content

Commit

Permalink
feat : #168 도서별 리뷰 목록 조회 api 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
Yujin-nKim committed May 12, 2024
1 parent 5154b54 commit 4b5bef0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,17 @@
public class ReviewController {
private final ReviewService reviewService;

@GetMapping("/reviews/book/{bookId}")
public ResponseEntity<BaseResponse<PageResponse<ReviewResponse>>> getReviewsByBookId(
/**
* 책 ID에 해당하는 리뷰 페이지 조회
* @param bookId 리뷰를 검색할 책의 ID
* @param pageNo 페이지 번호
* @param pageSize 페이지 크기
* @param sortBy 정렬 기준
* @return 주어진 책 ID에 대한 리뷰 페이지 응답
* @author Yujin-nKim(김유진)
*/
@GetMapping("/reviews/books/{bookId}")
public ResponseEntity<BaseResponse<PageResponse<ReviewResponse>>> findReviewsByBookId(
@PathVariable Long bookId,
@RequestParam(value = "pageNo", defaultValue = "0", required = false) int pageNo,
@RequestParam(value = "pageSize", defaultValue = "6", required = false) int pageSize,
Expand All @@ -33,9 +42,7 @@ public ResponseEntity<BaseResponse<PageResponse<ReviewResponse>>> getReviewsByBo

// 도서에 등록된 리뷰가 없는 경우 | status code 204 (No Content)
if (reviewList.getContent().isEmpty()) {
return ResponseEntity.status(HttpStatus.NO_CONTENT).body(
new BaseResponse<PageResponse<ReviewResponse>>().message("도서에 등록된 리뷰가 없습니다.")
);
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
}

return ResponseEntity.status(HttpStatus.OK).body(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package com.t3t.bookstoreapi.review.model.response;

import com.t3t.bookstoreapi.review.model.entity.Review;
import com.t3t.bookstoreapi.review.model.entity.ReviewImage;
import lombok.Builder;
import lombok.Getter;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
* 도서별 리뷰 목록 조회시 사용하는 데이터 전송 객체(DTO)
* @author Yujin-nKim(김유진)
*/
@Getter
@Builder
public class ReviewResponse {
Expand All @@ -15,4 +22,22 @@ public class ReviewResponse {
private LocalDateTime createdAt; // 등록 시간
private LocalDateTime updatedAt; // 수정 시간
private List<String> reviewImgUrlList; // 리뷰 이미지 url 리스트

/**
* Review 객체를 ReviewResponse 객체로 변환
* @param review Review 객체
* @return ReviewResponse 객체
* @author Yujin-nKim(김유진)
*/
public static ReviewResponse of(Review review) {
return ReviewResponse.builder()
.name(review.getMember().getName())
.comment(review.getReviewComment())
.reviewScore(review.getReviewScore())
.createdAt(review.getReviewCreatedAt())
.updatedAt(review.getReviewUpdatedAt())
.reviewImgUrlList(review.getReviewImageList()
.stream().map(ReviewImage::getReviewImageUrl).collect(Collectors.toList()))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.t3t.bookstoreapi.book.exception.BookNotFoundException;
import com.t3t.bookstoreapi.book.repository.BookRepository;
import com.t3t.bookstoreapi.review.model.entity.Review;
import com.t3t.bookstoreapi.review.model.entity.ReviewImage;
import com.t3t.bookstoreapi.model.response.PageResponse;
import com.t3t.bookstoreapi.review.model.response.ReviewResponse;
import com.t3t.bookstoreapi.review.repository.ReviewRepository;
Expand All @@ -22,17 +21,25 @@ public class ReviewService {
private final BookRepository bookRepository;
private final ReviewRepository reviewRepository;

/**
* 책 ID에 해당하는 리뷰 페이지 조회
* @param bookId 리뷰를 검색할 책의 ID
* @param pageable 페이지 정보
* @return 주어진 책 ID에 대한 리뷰 페이지 응답
* @throws BookNotFoundException 주어진 ID에 해당하는 책이 존재하지 않을 경우 발생
* @author Yujin-nKim(김유진)
*/
@Transactional(readOnly = true)
public PageResponse<ReviewResponse> findReviewsByBookId(Long bookId, Pageable pageable) {

if (!bookRepository.existsById(bookId)) {
throw new BookNotFoundException();
}

Page<Review> reviewsPage = reviewRepository.findByBookBookId(bookId, pageable);
Page<Review> reviewsPage = reviewRepository.findReviewsByBookId(bookId, pageable);

List<ReviewResponse> responses = reviewsPage.getContent().stream()
.map(this::buildReviewResponse)
.map(ReviewResponse::of)
.collect(Collectors.toList());

return PageResponse.<ReviewResponse>builder()
Expand All @@ -44,19 +51,4 @@ public PageResponse<ReviewResponse> findReviewsByBookId(Long bookId, Pageable pa
.last(reviewsPage.isLast())
.build();
}

public ReviewResponse buildReviewResponse(Review review) {
List<String> reviewImgUrlList = review.getReviewImageList().stream()
.map(ReviewImage::getReviewImageUrl)
.collect(Collectors.toList());

return ReviewResponse.builder()
.name(review.getMember().getName())
.comment(review.getReviewComment())
.reviewScore(review.getReviewScore())
.createdAt(review.getReviewCreatedAt())
.updatedAt(review.getReviewUpdatedAt())
.reviewImgUrlList(reviewImgUrlList)
.build();
}
}

0 comments on commit 4b5bef0

Please sign in to comment.