-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f365d1b
commit 9882023
Showing
9 changed files
with
224 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/main/java/com/odiga/fiesta/review/dto/response/ReviewLikeResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.odiga.fiesta.review.dto.response; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
@Builder | ||
public class ReviewLikeResponse { | ||
|
||
private Long reviewId; | ||
private Long likeCount; | ||
private Boolean isLiked; | ||
} |
11 changes: 3 additions & 8 deletions
11
src/main/java/com/odiga/fiesta/review/repository/ReviewLikeRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,14 @@ | ||
package com.odiga.fiesta.review.repository; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
|
||
import com.odiga.fiesta.review.domain.ReviewLike; | ||
|
||
public interface ReviewLikeRepository extends JpaRepository<ReviewLike, Long> { | ||
|
||
List<ReviewLike> findByReviewId(Long reviewId); | ||
Long countByReviewId(Long reviewId); | ||
|
||
@Query("SELECT rl.reviewId, COUNT(rl.id) as likeCount " + | ||
"FROM ReviewLike rl " + | ||
"GROUP BY rl.reviewId " + | ||
"ORDER BY likeCount DESC") | ||
List<Object[]> findReviewsWithLikeCount(); | ||
Optional<ReviewLike> findByUserIdAndReviewId(Long userId, Long reviewId); | ||
} |
66 changes: 66 additions & 0 deletions
66
src/main/java/com/odiga/fiesta/review/service/ReviewLikeService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.odiga.fiesta.review.service; | ||
|
||
import static com.odiga.fiesta.common.error.ErrorCode.*; | ||
import static java.util.Objects.*; | ||
|
||
import java.util.Optional; | ||
|
||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import com.odiga.fiesta.common.error.exception.CustomException; | ||
import com.odiga.fiesta.review.domain.ReviewLike; | ||
import com.odiga.fiesta.review.dto.response.ReviewLikeResponse; | ||
import com.odiga.fiesta.review.repository.ReviewLikeRepository; | ||
import com.odiga.fiesta.review.repository.ReviewRepository; | ||
import com.odiga.fiesta.user.repository.UserRepository; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional(readOnly = true) | ||
public class ReviewLikeService { | ||
|
||
private final ReviewRepository reviewRepository; | ||
private final ReviewLikeRepository reviewLikeRepository; | ||
private final UserRepository userRepository; | ||
|
||
@Transactional | ||
public ReviewLikeResponse updateReviewLike(Long userId, Long reviewId) { | ||
validateUser(userId); | ||
validateReview(reviewId); | ||
|
||
Optional<ReviewLike> optionalReviewLike = reviewLikeRepository.findByUserIdAndReviewId(userId, reviewId); | ||
boolean isLiked = optionalReviewLike.isPresent(); | ||
|
||
optionalReviewLike.ifPresentOrElse(reviewLikeRepository::delete, | ||
() -> reviewLikeRepository.save(ReviewLike.builder() | ||
.userId(userId) | ||
.reviewId(reviewId) | ||
.build()) | ||
); | ||
|
||
Long likeCount = reviewLikeRepository.countByReviewId(reviewId); | ||
|
||
return ReviewLikeResponse.builder() | ||
.reviewId(reviewId) | ||
.isLiked(!isLiked) | ||
.likeCount(likeCount) | ||
.build(); | ||
} | ||
|
||
private void validateReview(Long reviewId) { | ||
if (!reviewRepository.existsById(reviewId)) { | ||
throw new CustomException(REVIEW_NOT_FOUND); | ||
} | ||
} | ||
|
||
private void validateUser(Long userId) { | ||
if (isNull(userId)) { | ||
throw new CustomException(UNAUTHENTICATED_USER); | ||
} | ||
|
||
userRepository.findById(userId).orElseThrow(() -> new CustomException(USER_NOT_FOUND)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
src/test/java/com/odiga/fiesta/review/service/ReviewLikeServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package com.odiga.fiesta.review.service; | ||
|
||
import static com.odiga.fiesta.common.error.ErrorCode.*; | ||
import static org.assertj.core.api.Assertions.*; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
import com.odiga.fiesta.IntegrationTestSupport; | ||
import com.odiga.fiesta.review.domain.Review; | ||
import com.odiga.fiesta.review.domain.ReviewLike; | ||
import com.odiga.fiesta.review.dto.response.ReviewLikeResponse; | ||
import com.odiga.fiesta.review.repository.ReviewLikeRepository; | ||
import com.odiga.fiesta.review.repository.ReviewRepository; | ||
import com.odiga.fiesta.user.domain.User; | ||
import com.odiga.fiesta.user.repository.UserRepository; | ||
|
||
class ReviewLikeServiceTest extends IntegrationTestSupport { | ||
|
||
@Autowired | ||
private ReviewLikeService reviewLikeService; | ||
|
||
@Autowired | ||
private ReviewLikeRepository reviewLikeRepository; | ||
|
||
@Autowired | ||
private ReviewRepository reviewRepository; | ||
|
||
@Autowired | ||
private UserRepository userRepository; | ||
|
||
@DisplayName("리뷰 좋아요 등록/해제 - 좋아요 등록") | ||
@Test | ||
void updateReviewLike_AddLike() { | ||
// given | ||
Review review = reviewRepository.save(createReview()); | ||
User user = userRepository.save(createUser()); | ||
|
||
// when | ||
ReviewLikeResponse reviewLikeResponse = reviewLikeService.updateReviewLike(user.getId(), review.getId()); | ||
|
||
// then | ||
assertThat(reviewLikeResponse.getIsLiked()).isTrue(); | ||
assertEquals(1, reviewLikeResponse.getLikeCount()); | ||
} | ||
|
||
@DisplayName("리뷰 좋아요 등록/해제 - 좋아요 해제") | ||
@Test | ||
void updateReviewLike_RemoveLike() { | ||
// given | ||
Review review = reviewRepository.save(createReview()); | ||
User user = userRepository.save(createUser()); | ||
ReviewLike reviewLike = createReviewLike(user, review); | ||
reviewLikeRepository.save(reviewLike); | ||
|
||
// when | ||
ReviewLikeResponse reviewLikeResponse = reviewLikeService.updateReviewLike(user.getId(), review.getId()); | ||
|
||
// then | ||
assertThat(reviewLikeResponse.getIsLiked()).isFalse(); | ||
assertEquals(0, reviewLikeResponse.getLikeCount()); | ||
} | ||
|
||
@DisplayName("리뷰 좋아요 등록/해제 - 존재하지 않는 유저의 경우 에러 발생") | ||
@Test | ||
void updateReviewLike_UserNotFound() { | ||
// given | ||
Review review = reviewRepository.save(createReview()); | ||
|
||
User deletedUser = userRepository.save(createUser()); | ||
userRepository.delete(deletedUser); | ||
|
||
// when // then | ||
assertThatThrownBy(() -> reviewLikeService.updateReviewLike(deletedUser.getId(), review.getId())) | ||
.hasMessage(USER_NOT_FOUND.getMessage()); | ||
} | ||
|
||
@DisplayName("리뷰 좋아요 등록/해제 - 존재하지 않는 리뷰에 좋아요 등록 시 에러 발생") | ||
@Test | ||
void updateReviewLike_ReviewNotFound() { | ||
// given | ||
User user = userRepository.save(createUser()); | ||
|
||
// when // then | ||
assertThatThrownBy(() -> reviewLikeService.updateReviewLike(user.getId(), -1L)) | ||
.hasMessage(REVIEW_NOT_FOUND.getMessage()); | ||
} | ||
|
||
private static ReviewLike createReviewLike(User user, Review review) { | ||
return ReviewLike.builder() | ||
.userId(user.getId()) | ||
.reviewId(review.getId()) | ||
.build(); | ||
} | ||
|
||
private static Review createReview() { | ||
return Review.builder() | ||
.userId(1L) | ||
.festivalId(1L) | ||
.rating(5) | ||
.content("리뷰 내용") | ||
.build(); | ||
} | ||
|
||
private User createUser() { | ||
return User.builder() | ||
.userTypeId(1L) | ||
.nickname("테스트 유저") | ||
.statusMessage("상태 메시지") | ||
.profileImage("프로필 이미지 링크") | ||
.build(); | ||
} | ||
|
||
} |