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

[BSVR-241] 블록 리뷰에 유저가 좋아요, 스크랩을 눌렀는 지 안 눌렀는지 확인 필드 추가 #161

Merged
merged 4 commits into from
Aug 25, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class ReadReviewController {
@GetMapping("/stadiums/{stadiumId}/blocks/{blockCode}/reviews")
@Operation(summary = "특정 야구장의 특정 블록에 대한 리뷰 목록을 조회한다.")
public BlockReviewListResponse findReviewsByBlockId(
@Parameter(hidden = true) Long memberId,
@PathVariable("stadiumId")
@NotNull
@Positive
Expand All @@ -52,6 +53,7 @@ public BlockReviewListResponse findReviewsByBlockId(

BlockReviewListResult result =
readReviewUsecase.findReviewsByStadiumIdAndBlockCode(
memberId,
stadiumId,
blockCode,
request.rowNumber(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ public record BaseReviewResponse(
List<KeywordResponse> keywords,
int likesCount,
int scrapsCount,
ReviewType reviewType) {
ReviewType reviewType,
boolean isLiked,
boolean isScrapped) {

public static BaseReviewResponse from(CreateReviewResult result) {
Review review = result.review();
Expand Down Expand Up @@ -59,7 +61,9 @@ public static BaseReviewResponse from(CreateReviewResult result) {
.toList(),
review.getLikesCount(),
review.getScrapsCount(),
review.getReviewType());
review.getReviewType(),
review.isLiked(),
review.isScrapped());
}

public static BaseReviewResponse from(Review review) {
Expand Down Expand Up @@ -89,7 +93,9 @@ public static BaseReviewResponse from(Review review) {
.collect(Collectors.toList()),
review.getLikesCount(),
review.getScrapsCount(),
review.getReviewType());
review.getReviewType(),
review.isLiked(),
review.isScrapped());
}

public record StadiumResponse(Long id, String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public enum SortCriteria {

public static final int DEFAULT_LIKE_COUNT = 0;
public static final int DEFAULT_SCRAPS_COUNT = 0;
public boolean isLiked;
public boolean isScrapped;

@Builder
public Review(
Expand Down Expand Up @@ -142,6 +144,11 @@ public void setDeletedAt(LocalDateTime now) {
this.deletedAt = now;
}

public void setLikedAndScrapped(boolean liked, boolean scraped) {
this.isLiked = liked;
this.isScrapped = scraped;
}

public Review withLimitedImages(int limit) {
List<ReviewImage> limitedImages =
this.images.stream().limit(limit).collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package org.depromeet.spot.infrastructure.jpa.review.repository.like;

import java.util.List;

import org.depromeet.spot.infrastructure.jpa.review.entity.like.ReviewLikeEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface ReviewLikeJpaRepository extends JpaRepository<ReviewLikeEntity, Long> {

Expand All @@ -12,4 +16,9 @@ public interface ReviewLikeJpaRepository extends JpaRepository<ReviewLikeEntity,

@Modifying
void deleteByMemberIdAndReviewId(long memberId, long reviewId);

@Query(
"SELECT r.reviewId FROM ReviewLikeEntity r WHERE r.memberId = :memberId AND r.reviewId IN :reviewIds")
List<Long> findReviewIdsByMemberIdAndReviewIdIn(
@Param("memberId") Long memberId, @Param("reviewIds") List<Long> reviewIds);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package org.depromeet.spot.infrastructure.jpa.review.repository.like;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.depromeet.spot.domain.review.like.ReviewLike;
import org.depromeet.spot.infrastructure.jpa.review.entity.like.ReviewLikeEntity;
import org.depromeet.spot.usecase.port.out.review.ReviewLikeRepository;
Expand Down Expand Up @@ -33,4 +37,12 @@ public void save(ReviewLike like) {
ReviewLikeEntity entity = ReviewLikeEntity.from(like);
reviewLikeJpaRepository.save(entity);
}

@Override
public Map<Long, Boolean> existsByMemberIdAndReviewIds(Long memberId, List<Long> reviewIds) {
List<Long> likedReviewIds =
reviewLikeJpaRepository.findReviewIdsByMemberIdAndReviewIdIn(memberId, reviewIds);
return reviewIds.stream()
.collect(Collectors.toMap(reviewId -> reviewId, likedReviewIds::contains));
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package org.depromeet.spot.infrastructure.jpa.review.repository.scrap;

import java.util.List;

import org.depromeet.spot.infrastructure.jpa.review.entity.scrap.ReviewScrapEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface ReviewScrapJpaRepository extends JpaRepository<ReviewScrapEntity, Integer> {
long countByReviewId(long reviewId);
Expand All @@ -11,4 +15,9 @@ public interface ReviewScrapJpaRepository extends JpaRepository<ReviewScrapEntit

@Modifying
void deleteByMemberIdAndReviewId(long memberId, long reviewId);

@Query(
"SELECT r.reviewId FROM ReviewScrapEntity r WHERE r.memberId = :memberId AND r.reviewId IN :reviewIds")
List<Long> findReviewIdsByMemberIdAndReviewIdIn(
@Param("memberId") Long memberId, @Param("reviewIds") List<Long> reviewIds);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.depromeet.spot.infrastructure.jpa.review.repository.scrap;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.depromeet.spot.domain.review.Review;
import org.depromeet.spot.domain.review.Review.SortCriteria;
Expand Down Expand Up @@ -66,4 +68,12 @@ public void save(ReviewScrap scrap) {
ReviewScrapEntity entity = ReviewScrapEntity.from(scrap);
reviewScrapJpaRepository.save(entity);
}

@Override
public Map<Long, Boolean> existsByMemberIdAndReviewIds(Long memberId, List<Long> reviewIds) {
List<Long> scrappedReviewIds =
reviewScrapJpaRepository.findReviewIdsByMemberIdAndReviewIdIn(memberId, reviewIds);
return reviewIds.stream()
.collect(Collectors.toMap(reviewId -> reviewId, scrappedReviewIds::contains));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
public interface ReadReviewUsecase {

BlockReviewListResult findReviewsByStadiumIdAndBlockCode(
Long memberId,
Long stadiumId,
String blockCode,
Integer rowNumber,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.depromeet.spot.usecase.port.out.review;

import java.util.List;
import java.util.Map;

import org.depromeet.spot.domain.review.like.ReviewLike;

public interface ReviewLikeRepository {
Expand All @@ -11,4 +14,6 @@ public interface ReviewLikeRepository {
void deleteBy(long memberId, long reviewId);

void save(ReviewLike like);

Map<Long, Boolean> existsByMemberIdAndReviewIds(Long memberId, List<Long> reviewIds);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.depromeet.spot.usecase.port.out.review;

import java.util.List;
import java.util.Map;

import org.depromeet.spot.domain.review.Review;
import org.depromeet.spot.domain.review.Review.SortCriteria;
Expand Down Expand Up @@ -32,4 +33,6 @@ Long getTotalCount(
void deleteBy(long memberId, long reviewId);

void save(ReviewScrap like);

Map<Long, Boolean> existsByMemberIdAndReviewIds(Long memberId, List<Long> reviewIds);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
import org.depromeet.spot.usecase.port.out.review.BlockTopKeywordRepository;
import org.depromeet.spot.usecase.port.out.review.KeywordRepository;
import org.depromeet.spot.usecase.port.out.review.ReviewImageRepository;
import org.depromeet.spot.usecase.port.out.review.ReviewLikeRepository;
import org.depromeet.spot.usecase.port.out.review.ReviewRepository;
import org.depromeet.spot.usecase.port.out.review.ReviewScrapRepository;
import org.depromeet.spot.usecase.port.out.team.BaseballTeamRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -34,12 +36,15 @@ public class ReadReviewService implements ReadReviewUsecase {
private final KeywordRepository keywordRepository;
private final MemberRepository memberRepository;
private final BaseballTeamRepository baseballTeamRepository;
private final ReviewLikeRepository reviewLikeRepository;
private final ReviewScrapRepository reviewScrapRepository;

private static final int TOP_KEYWORDS_LIMIT = 5;
private static final int TOP_IMAGES_LIMIT = 5;

@Override
public BlockReviewListResult findReviewsByStadiumIdAndBlockCode(
Long memberId,
Long stadiumId,
String blockCode,
Integer rowNumber,
Expand Down Expand Up @@ -87,6 +92,9 @@ public BlockReviewListResult findReviewsByStadiumIdAndBlockCode(

List<Review> reviewsWithKeywords = mapKeywordsToReviews(reviews);

// 유저의 리뷰 좋아요, 스크랩 여부
setLikedAndScrappedStatus(reviewsWithKeywords, memberId);

long totalElements =
reviewRepository.countByStadiumIdAndBlockCode(
stadiumId, blockCode, rowNumber, seatNumber, year, month);
Expand Down Expand Up @@ -289,4 +297,20 @@ public Review mapKeywordsToReview(Review review) {

return mappedReview;
}

private void setLikedAndScrappedStatus(List<Review> reviews, Long memberId) {
List<Long> reviewIds = reviews.stream().map(Review::getId).collect(Collectors.toList());

Map<Long, Boolean> likedMap =
reviewLikeRepository.existsByMemberIdAndReviewIds(memberId, reviewIds);
Map<Long, Boolean> scrappedMap =
reviewScrapRepository.existsByMemberIdAndReviewIds(memberId, reviewIds);

reviews.forEach(
review -> {
boolean isLiked = likedMap.getOrDefault(review.getId(), false);
boolean isScrapped = scrappedMap.getOrDefault(review.getId(), false);
review.setLikedAndScrapped(isLiked, isScrapped);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.depromeet.spot.domain.review.Review;
Expand Down Expand Up @@ -90,6 +91,11 @@ public void save(ReviewScrap scrap) {
scraps.add(scrap);
}

@Override
public Map<Long, Boolean> existsByMemberIdAndReviewIds(Long memberId, List<Long> reviewIds) {
return Map.of();
}

public void addReview(Review review) {
reviews.add(review);
}
Expand Down
Loading