Skip to content

Commit

Permalink
리뷰 조회 버그 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
awesomeo184 committed Dec 31, 2023
1 parent f0a9680 commit 40c5cf2
Show file tree
Hide file tree
Showing 12 changed files with 150 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private OfferSummary getOfferSummary(Offer offer) {

boolean reviewAvailable = !messages.isEmpty();
ReviewInfoResponse review = reviewRepository.findByPostAndReviewer(offer.getPost(), offer.getOfferer())
.map(ReviewInfoResponse::from)
.map(it -> ReviewInfoResponse.buildReviewInfoResponse(it, offer.getOfferer()))
.orElse(null);

return OfferSummary.from(offer, reviewAvailable, review);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.offer.offer.domain.Offer;
import com.offer.post.application.response.EnumResponse;
import com.offer.post.domain.Post;
import com.offer.post.domain.TradeStatus;
import com.offer.review.application.response.ReviewInfoResponse;
import java.time.LocalDateTime;
import lombok.Builder;
Expand All @@ -17,6 +16,8 @@ public class OfferSummary {

private Long offerId;
private Long postId;
private String sellerNickname;
private String title;
private int offerPrice;
private String thumbnailImageUrl;
private EnumResponse tradeStatus;
Expand All @@ -27,11 +28,13 @@ public class OfferSummary {
private ReviewInfoResponse review;

@Builder
public OfferSummary(Long offerId, Long postId, int offerPrice, String thumbnailImageUrl,
public OfferSummary(Long offerId, Long postId, String sellerNickname, String title, int offerPrice, String thumbnailImageUrl,
EnumResponse tradeStatus, LocalDateTime createdAt, boolean reviewAvailable,
boolean hasReview, ReviewInfoResponse review) {
this.offerId = offerId;
this.postId = postId;
this.sellerNickname = sellerNickname;
this.title = title;
this.offerPrice = offerPrice;
this.thumbnailImageUrl = thumbnailImageUrl;
this.tradeStatus = tradeStatus;
Expand All @@ -46,6 +49,8 @@ public static OfferSummary from(Offer offer) {
return OfferSummary.builder()
.offerId(offer.getId())
.postId(post.getId())
.title(post.getTitle())
.sellerNickname(post.getSeller().getNickname())
.offerPrice(offer.getPrice())
.thumbnailImageUrl(post.getThumbnailImageUrl())
.tradeStatus(new EnumResponse(post.getTradeStatus().name(), post.getTradeStatus().getDescription()))
Expand All @@ -58,6 +63,8 @@ public static OfferSummary from(Offer offer, boolean reviewAvailable, ReviewInfo
return OfferSummary.builder()
.offerId(offer.getId())
.postId(post.getId())
.title(post.getTitle())
.sellerNickname(post.getSeller().getNickname())
.offerPrice(offer.getPrice())
.thumbnailImageUrl(post.getThumbnailImageUrl())
.tradeStatus(new EnumResponse(post.getTradeStatus().name(), post.getTradeStatus().getDescription()))
Expand Down
16 changes: 13 additions & 3 deletions src/main/java/com/offer/post/domain/PostQueryRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.offer.review.application.response.ReviewInfoResponse;
import com.offer.review.domain.ReviewRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;
import java.util.Collections;
Expand All @@ -35,7 +36,6 @@ public PostSummaries searchPost(PostReadParams params, Long loginMemberId) {
memberRepository.getById(sellerId) : null;

Set<Long> likePostIds = getLikePostId(loginMemberId);
boolean priceDesc = "PRICE_DESC".equals(params.getSort());

BooleanBuilder booleanBuilder = new BooleanBuilder();
if (params.getLastId() != null) {
Expand All @@ -49,7 +49,7 @@ public PostSummaries searchPost(PostReadParams params, Long loginMemberId) {
sellerEq(seller),
tradeStatus(TradeStatus.from(params.getTradeStatus()))
)
.orderBy(priceDesc ? post.price.desc() : post.id.desc())
.orderBy(order(params.getSort()))
.limit(params.getLimit() + 1)
.fetch();

Expand Down Expand Up @@ -79,13 +79,23 @@ public PostSummaries searchPost(PostReadParams params, Long loginMemberId) {
.build();
}

private OrderSpecifier<?> order(String sort) {
if (sort == null || sort.equals("CREATED_DATE_DESC")) {
return post.id.desc();
}
if (sort.equals("PRICE_DESC")) {
return post.price.desc();
}
return post.price.asc();
}

private ReviewInfoResponse getReviewInfoResponse(Post post, Member seller) {
if (seller == null) {
return null;
}

ReviewInfoResponse review = reviewRepository.findByPostAndReviewer(post, seller)
.map(ReviewInfoResponse::from)
.map(it -> ReviewInfoResponse.buildReviewInfoResponse(it, seller))
.orElse(null);
return review;
}
Expand Down
49 changes: 36 additions & 13 deletions src/main/java/com/offer/review/application/ReviewService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@
import com.offer.post.domain.PostRepository;
import com.offer.review.application.request.ReviewCreateRequest;
import com.offer.review.application.response.ReviewInfoResponse;
import com.offer.review.application.response.ReviewInfoResponse.ReviewTargetMemberResponse;
import com.offer.review.application.response.ReviewInfoResponses;
import com.offer.review.domain.Review;
import com.offer.review.domain.ReviewRepository;
import com.offer.review.domain.Role;
import com.offer.utils.SliceUtils;
import java.util.ArrayList;
import lombok.RequiredArgsConstructor;
import org.hibernate.query.spi.Limit;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -55,18 +59,37 @@ public CommonCreationResponse createReview(ReviewCreateRequest request, Long rev
}

@Transactional(readOnly = true)
public List<ReviewInfoResponse> getReviews(int page, Long memberId, Role role) {
PageRequest pageRequest = PageRequest.of(SliceUtils.getSliceNumber(page), Properties.DEFAULT_SLICE_SIZE);

Slice<Review> reviews = switch (role) {
case BUYER -> reviewRepository.findSliceByRevieweeIdAndIsRevieweeBuyer(pageRequest, memberId, true);
case SELLER -> reviewRepository.findSliceByRevieweeIdAndIsRevieweeBuyer(pageRequest, memberId, false);
case ALL -> reviewRepository.findSliceByRevieweeId(pageRequest, memberId);
};

return reviews.stream()
.filter(Objects::nonNull)
.map(ReviewInfoResponse::from)
.collect(Collectors.toList());
public ReviewInfoResponses getReviews(Long memberId, Role role, Long lastId, int limit) {

Member member = memberRepository.getById(memberId);

List<Review> result = new ArrayList<>();

if (role == Role.ALL) {
result = reviewRepository.findTop10ByReviewerOrRevieweeAndIdGreaterThanEqual(member, member, lastId);
}
if (role == Role.SELLER) {
result = reviewRepository.findTop10ByReviewerAndRevieweeIsBuyerAndIdGreaterThanEqual(member, true, lastId);
}
if (role == Role.BUYER) {
result = reviewRepository.findTop10ByRevieweeAndRevieweeIsBuyerAndIdGreaterThanEqual(member, false, lastId);
}

if (result.size() > limit) {
result.remove(limit);
return ReviewInfoResponses.builder()
.reviews(result.stream()
.map(review -> ReviewInfoResponse.buildReviewInfoResponse(review, member))
.collect(Collectors.toList()))
.hasNext(true)
.build();
}

return ReviewInfoResponses.builder()
.reviews(result.stream()
.map(review -> ReviewInfoResponse.buildReviewInfoResponse(review, member))
.collect(Collectors.toList()))
.hasNext(false)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ public ReviewCreateRequest(Long targetMemberId, Long postId, int score, String c
this.content = content;
}

public Review toEntity(Member reviewee, Member reviewer, Post post, boolean isRevieweeBuyer) {
public Review toEntity(Member reviewee, Member reviewer, Post post, boolean revieweeIsBuyer) {
return Review.builder()
.reviewee(reviewee)
.reviewer(reviewer)
.post(post)
.score(score)
.content(content)
.isRevieweeBuyer(isRevieweeBuyer)
.revieweeIsBuyer(revieweeIsBuyer)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.offer.review.application.response;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.offer.member.Member;
import com.offer.review.domain.Review;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -15,7 +15,7 @@
public class ReviewInfoResponse {
private final Long id;

private final ReviewerBriefResponse reviewer;
private final ReviewTargetMemberResponse reviewTargetMember;

private final int score;

Expand All @@ -28,7 +28,7 @@ public class ReviewInfoResponse {

@Getter
@RequiredArgsConstructor
public static class ReviewerBriefResponse {
public static class ReviewTargetMemberResponse {
private final Long id;

private final String profileImageUrl;
Expand All @@ -44,13 +44,22 @@ public static class PostBriefResponse {
private final String title;
}

public static ReviewInfoResponse from(Review review) {
public static ReviewInfoResponse buildReviewInfoResponse(Review review, Member member) {
// 내가 reviewer인 경우 reviewee, 내가 reviewee인 경우 reviewer
Member reviewer = review.getReviewer();
if (member.getId().equals(reviewer.getId())) {
return ReviewInfoResponse.from(review, review.getReviewee());
}
return ReviewInfoResponse.from(review, review.getReviewer());
}

private static ReviewInfoResponse from(Review review, Member targetMember) {
return ReviewInfoResponse.builder()
.id(review.getId())
.reviewer(new ReviewerBriefResponse(
review.getReviewer().getId(),
review.getReviewer().getProfileImageUrl(),
review.getReviewer().getNickname())
.reviewTargetMember(new ReviewTargetMemberResponse(
targetMember.getId(),
targetMember.getProfileImageUrl(),
targetMember.getNickname())
)
.score(review.getScore())
.post(new PostBriefResponse(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.offer.review.application.response;

import java.util.List;
import lombok.Builder;
import lombok.Getter;

@Getter
public class ReviewInfoResponses {

private List<ReviewInfoResponse> reviews;
private boolean hasNext;

@Builder
public ReviewInfoResponses(List<ReviewInfoResponse> reviews, boolean hasNext) {
this.reviews = reviews;
this.hasNext = hasNext;
}
}
6 changes: 3 additions & 3 deletions src/main/java/com/offer/review/domain/Review.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class Review {

private String content;

private boolean isRevieweeBuyer;
private boolean revieweeIsBuyer;

@CreatedDate
private LocalDateTime createdAt;
Expand All @@ -49,12 +49,12 @@ public class Review {

@Builder
public Review(Member reviewee, Member reviewer, Post post, int score,
String content, boolean isRevieweeBuyer) {
String content, boolean revieweeIsBuyer) {
this.reviewee = reviewee;
this.reviewer = reviewer;
this.post = post;
this.score = score;
this.content = content;
this.isRevieweeBuyer = isRevieweeBuyer;
this.revieweeIsBuyer = revieweeIsBuyer;
}
}
13 changes: 8 additions & 5 deletions src/main/java/com/offer/review/domain/ReviewRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import com.offer.member.Member;
import com.offer.post.domain.Post;
import com.querydsl.core.types.Projections;
import java.util.List;
import java.util.Optional;
import org.hibernate.query.spi.Limit;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Slice;
import org.springframework.data.jpa.repository.JpaRepository;
Expand All @@ -14,13 +15,15 @@ public interface ReviewRepository extends JpaRepository<Review, Long> {

boolean existsByReviewerAndPost(Member reviewer, Post post);

Slice<Review> findSliceByRevieweeIdAndIsRevieweeBuyer(PageRequest pageRequest, Long revieweeId, boolean IsRevieweeBuyer);

Slice<Review> findSliceByRevieweeId(PageRequest pageRequest, Long revieweeId);

int countByRevieweeIdOrReviewerId(Long revieweeId, Long reviewerId);

Optional<Review> findByPost(Post post);

Optional<Review> findByPostAndReviewer(Post post, Member reviewer);

List<Review> findTop10ByReviewerOrRevieweeAndIdGreaterThanEqual(Member reviewer, Member reviewee, Long id);

List<Review> findTop10ByReviewerAndRevieweeIsBuyerAndIdGreaterThanEqual(Member reviewer, boolean revieweeIsBuyer, Long id);

List<Review> findTop10ByRevieweeAndRevieweeIsBuyerAndIdGreaterThanEqual(Member reviewee, boolean revieweeIsBuyer, Long id);
}
20 changes: 6 additions & 14 deletions src/main/java/com/offer/review/domain/Role.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
package com.offer.review.domain;

import com.offer.common.response.ResponseMessage;
import com.offer.common.response.exception.BusinessException;
import java.util.Arrays;

public enum Role {
BUYER, SELLER, ALL

;

public static Role of(String roleStr) {
if (roleStr == null) return ALL;

roleStr = roleStr.trim();

if (roleStr.equalsIgnoreCase(BUYER.name())) {
return BUYER;
} else if (roleStr.equalsIgnoreCase(SELLER.name())) {
return SELLER;
} else {
throw new BusinessException(ResponseMessage.INVALID_ROLE);
}
public static Role of(String role) {
return Arrays.stream(values())
.filter(it -> it.name().equalsIgnoreCase(role))
.findAny()
.orElse(ALL);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.offer.review.application.ReviewService;
import com.offer.review.application.request.ReviewCreateRequest;
import com.offer.review.application.response.ReviewInfoResponse;
import com.offer.review.application.response.ReviewInfoResponses;
import com.offer.review.domain.Role;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
Expand Down Expand Up @@ -39,13 +40,15 @@ public ResponseEntity<ApiResponse<CommonCreationResponse>> createReview(

@Operation(summary = "리뷰 조회", security = {@SecurityRequirement(name = "jwt")})
@GetMapping("/reviews")
public ResponseEntity<ApiResponse<List<ReviewInfoResponse>>> getReviews(
public ResponseEntity<ApiResponse<ReviewInfoResponses>> getReviews(
@Schema(hidden = true) @AuthenticationPrincipal LoginMember loginMember,
@RequestParam(value = "memberId", required = true) Long memberId,
@RequestParam(value = "role", required = false) String role,
@RequestParam(required = true) int page) {
@RequestParam Long memberId,
@RequestParam(defaultValue = "ALL") String role,
@RequestParam(defaultValue = "1") Long lastId,
@RequestParam(defaultValue = "10") int limit
) {

List<ReviewInfoResponse> response = reviewService.getReviews(page, memberId, Role.of(role));
ReviewInfoResponses response = reviewService.getReviews(memberId, Role.of(role), lastId, limit);

return ResponseEntity.ok(
ApiResponse.of(ResponseMessage.SUCCESS, response)
Expand Down
Loading

0 comments on commit 40c5cf2

Please sign in to comment.