Skip to content

Commit

Permalink
v2.0.1 (#51)
Browse files Browse the repository at this point in the history
v2.0.1
  • Loading branch information
70825 authored Apr 23, 2024
2 parents caf43c2 + e410c97 commit 239b076
Show file tree
Hide file tree
Showing 9 changed files with 401 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,21 @@
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.util.UriTemplate;

@Component
public class AuthHandlerInterceptor implements HandlerInterceptor {

private static final String GET = "GET";
private static final UriTemplate SORTING_REVIEW_URI_TEMPLATE = new UriTemplate("/api/products/{productId}/reviews");

@Override
public boolean preHandle(final HttpServletRequest request, final HttpServletResponse response,
final Object handler) {
if (GET.equals(request.getMethod()) && SORTING_REVIEW_URI_TEMPLATE.matches(request.getRequestURI())) {
return true;
}

final HttpSession session = request.getSession(false);

if (Objects.isNull(session)) {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/funeat/member/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public Member(final String nickname, final String profileImage, final String pla
this.platformId = platformId;
}

public static Member createGuest() {
return new Member("Guest", "", "-1");
}

public Long getId() {
return id;
}
Expand Down Expand Up @@ -81,4 +85,8 @@ public void modifyNickname(final String nickname) {
}
this.nickname = nickname;
}

public boolean isGuest() {
return platformId.equals("-1");
}
}
15 changes: 7 additions & 8 deletions src/main/java/com/funeat/review/application/ReviewService.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class ReviewService {
private static final int START_INDEX = 0;
private static final int ONE = 1;
private static final String EMPTY_URL = "";
private static final int RANKING_SIZE = 3;
private static final int RANKING_SIZE = 2;
private static final long RANKING_MINIMUM_FAVORITE_COUNT = 1L;
private static final int REVIEW_PAGE_SIZE = 10;

Expand Down Expand Up @@ -156,12 +156,12 @@ public void updateProductImage(final Long productId) {

public SortingReviewsResponse sortingReviews(final Long productId, final Long memberId,
final SortingReviewRequest request) {
final Member findMember = memberRepository.findById(memberId)
.orElseThrow(() -> new MemberNotFoundException(MEMBER_NOT_FOUND, memberId));
final Member guestOrFindMember = memberRepository.findById(memberId)
.orElse(Member.createGuest());
final Product findProduct = productRepository.findById(productId)
.orElseThrow(() -> new ProductNotFoundException(PRODUCT_NOT_FOUND, productId));

final List<SortingReviewDto> sortingReviews = getSortingReviews(findMember, findProduct, request);
final List<SortingReviewDto> sortingReviews = getSortingReviews(guestOrFindMember, findProduct, request);
final int resultSize = getResultSize(sortingReviews);

final List<SortingReviewDto> resizeSortingReviews = sortingReviews.subList(START_INDEX, resultSize);
Expand All @@ -170,14 +170,13 @@ public SortingReviewsResponse sortingReviews(final Long productId, final Long me
return SortingReviewsResponse.toResponse(resizeSortingReviews, hasNext);
}

private List<SortingReviewDto> getSortingReviews(final Member member, final Product product,
final SortingReviewRequest request) {
private List<SortingReviewDto> getSortingReviews(final Member guestOrFindMember, final Product product,
final SortingReviewRequest request) {
final Long lastReviewId = request.getLastReviewId();
final String sortOption = request.getSort();

final Specification<Review> specification = getSortingSpecification(product, sortOption, lastReviewId);
final List<SortingReviewDtoWithoutTag> sortingReviewDtoWithoutTags = reviewRepository.getSortingReview(member,
specification, sortOption);
final List<SortingReviewDtoWithoutTag> sortingReviewDtoWithoutTags = reviewRepository.getSortingReview(guestOrFindMember, specification, sortOption);

return addTagsToSortingReviews(sortingReviewDtoWithoutTags);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class ReviewRepositoryImpl implements ReviewCustomRepository {
private EntityManager em;

@Override
public List<SortingReviewDtoWithoutTag> getSortingReview(final Member loginMember,
public List<SortingReviewDtoWithoutTag> getSortingReview(final Member guestOrLoginMember,
final Specification<Review> specification,
final String sortOption) {
final CriteriaBuilder cb = em.getCriteriaBuilder();
Expand All @@ -43,8 +43,11 @@ public List<SortingReviewDtoWithoutTag> getSortingReview(final Member loginMembe

// left join
final Join<Review, ReviewFavorite> leftJoinReviewFavorite = root.join("reviewFavorites", JoinType.LEFT);
final Predicate condition = cb.equal(leftJoinReviewFavorite.get("member"), loginMember);
leftJoinReviewFavorite.on(condition);
leftJoinReviewFavorite.on(cb.disjunction()); // ๋น„๋กœ๊ทธ์ธ ์ข‹์•„์š” ๊ธฐ๋ณธ๊ฐ’์€ false
if (!guestOrLoginMember.isGuest()) { // ๋กœ๊ทธ์ธ ์ข‹์•„์š”๋Š” ์‚ฌ์šฉ์ž์˜ ์ข‹์•„์š” ์œ ๋ฌด์— ๋”ฐ๋ผ ๊ฒฐ์ •
final Predicate condition = cb.equal(leftJoinReviewFavorite.get("member"), guestOrLoginMember);
leftJoinReviewFavorite.on(condition);
}

// select - from - where - order by
cq.select(getConstruct(root, cb, joinMember, leftJoinReviewFavorite))
Expand Down
Loading

0 comments on commit 239b076

Please sign in to comment.