diff --git a/src/main/kotlin/com/restaurant/be/review/domain/entity/Review.kt b/src/main/kotlin/com/restaurant/be/review/domain/entity/Review.kt index 0c538a9..9f0d9fa 100644 --- a/src/main/kotlin/com/restaurant/be/review/domain/entity/Review.kt +++ b/src/main/kotlin/com/restaurant/be/review/domain/entity/Review.kt @@ -4,7 +4,6 @@ import com.restaurant.be.common.entity.BaseEntity import com.restaurant.be.common.exception.InvalidLikeCountException import com.restaurant.be.review.domain.entity.QReview.review import com.restaurant.be.review.presentation.dto.UpdateReviewRequest -import com.restaurant.be.review.presentation.dto.common.ReviewResponseDto import com.restaurant.be.user.domain.entity.User import javax.persistence.CascadeType import javax.persistence.Column @@ -82,22 +81,4 @@ class Review( } this.likeCount-- } - - fun toDto(isLikedByUser: Boolean): ReviewResponseDto { - return ReviewResponseDto( - id = id ?: 0, - userId = user.id ?: 0, - username = user.nickname, - profileImageUrl = user.profileImageUrl, - restaurantId = restaurantId, - rating = rating, - content = content, - imageUrls = images.map { it.imageUrl }, - isLike = isLikedByUser, - likeCount = likeCount, - viewCount = viewCount, - createdAt = createdAt, - modifiedAt = modifiedAt - ) - } } diff --git a/src/main/kotlin/com/restaurant/be/review/domain/service/CreateReviewService.kt b/src/main/kotlin/com/restaurant/be/review/domain/service/CreateReviewService.kt index c6e2242..77a22d7 100644 --- a/src/main/kotlin/com/restaurant/be/review/domain/service/CreateReviewService.kt +++ b/src/main/kotlin/com/restaurant/be/review/domain/service/CreateReviewService.kt @@ -6,6 +6,7 @@ import com.restaurant.be.restaurant.repository.RestaurantRepository import com.restaurant.be.review.domain.entity.ReviewImage import com.restaurant.be.review.presentation.dto.CreateReviewResponse import com.restaurant.be.review.presentation.dto.common.ReviewRequestDto +import com.restaurant.be.review.presentation.dto.common.ReviewResponseDto import com.restaurant.be.review.repository.ReviewRepository import com.restaurant.be.user.repository.UserRepository import org.springframework.stereotype.Service @@ -42,7 +43,10 @@ class CreateReviewService( val reviewWithLikes = reviewRepository.findReview(user, review.id ?: 0)!! - val responseDto = review.toDto(reviewWithLikes.isLikedByUser) + val responseDto = ReviewResponseDto.toDto( + reviewWithLikes.review, + reviewWithLikes.isLikedByUser + ) return CreateReviewResponse(responseDto) } diff --git a/src/main/kotlin/com/restaurant/be/review/domain/service/GetReviewService.kt b/src/main/kotlin/com/restaurant/be/review/domain/service/GetReviewService.kt index 42f95c9..cc6eef1 100644 --- a/src/main/kotlin/com/restaurant/be/review/domain/service/GetReviewService.kt +++ b/src/main/kotlin/com/restaurant/be/review/domain/service/GetReviewService.kt @@ -5,6 +5,7 @@ import com.restaurant.be.common.exception.NotFoundUserEmailException import com.restaurant.be.review.presentation.dto.GetMyReviewsResponse import com.restaurant.be.review.presentation.dto.GetReviewResponse import com.restaurant.be.review.presentation.dto.GetReviewsResponse +import com.restaurant.be.review.presentation.dto.common.ReviewResponseDto import com.restaurant.be.review.repository.ReviewRepository import com.restaurant.be.user.repository.UserRepository import org.springframework.data.domain.Pageable @@ -24,7 +25,10 @@ class GetReviewService( return GetReviewsResponse( reviewsWithLikes.map { - it.review.toDto(it.isLikedByUser) + ReviewResponseDto.toDto( + it.review, + it.isLikedByUser + ) } ) } @@ -40,7 +44,8 @@ class GetReviewService( reviewWithLikes.review.incrementViewCount() } - val responseDto = reviewWithLikes.review.toDto( + val responseDto = ReviewResponseDto.toDto( + reviewWithLikes.review, reviewWithLikes.isLikedByUser ) @@ -55,7 +60,10 @@ class GetReviewService( return GetMyReviewsResponse( reviewsWithLikes.map { - it.review.toDto(it.isLikedByUser) + ReviewResponseDto.toDto( + it.review, + it.isLikedByUser + ) } ) } diff --git a/src/main/kotlin/com/restaurant/be/review/domain/service/LikeReviewService.kt b/src/main/kotlin/com/restaurant/be/review/domain/service/LikeReviewService.kt index d33966a..3a8d47b 100644 --- a/src/main/kotlin/com/restaurant/be/review/domain/service/LikeReviewService.kt +++ b/src/main/kotlin/com/restaurant/be/review/domain/service/LikeReviewService.kt @@ -7,6 +7,7 @@ import com.restaurant.be.common.exception.NotFoundUserEmailException import com.restaurant.be.review.presentation.dto.LikeReviewRequest import com.restaurant.be.review.presentation.dto.LikeReviewResponse import com.restaurant.be.review.presentation.dto.ReviewWithLikesDto +import com.restaurant.be.review.presentation.dto.common.ReviewResponseDto import com.restaurant.be.review.repository.ReviewLikeRepository import com.restaurant.be.review.repository.ReviewRepository import com.restaurant.be.user.domain.entity.User @@ -32,7 +33,10 @@ class LikeReviewService( val reviewWithLikes: ReviewWithLikesDto = reviewRepository.findReview(user, reviewId) ?: throw NotFoundReviewException() - val responseDto = reviewWithLikes.review.toDto(reviewWithLikes.isLikedByUser) + val responseDto = ReviewResponseDto.toDto( + reviewWithLikes.review, + reviewWithLikes.isLikedByUser + ) return LikeReviewResponse(responseDto) } diff --git a/src/main/kotlin/com/restaurant/be/review/domain/service/UpdateReviewService.kt b/src/main/kotlin/com/restaurant/be/review/domain/service/UpdateReviewService.kt index 2c449c4..3192e4c 100644 --- a/src/main/kotlin/com/restaurant/be/review/domain/service/UpdateReviewService.kt +++ b/src/main/kotlin/com/restaurant/be/review/domain/service/UpdateReviewService.kt @@ -6,6 +6,7 @@ import com.restaurant.be.common.exception.UnAuthorizedUpdateException import com.restaurant.be.restaurant.repository.RestaurantRepository import com.restaurant.be.review.presentation.dto.UpdateReviewRequest import com.restaurant.be.review.presentation.dto.UpdateReviewResponse +import com.restaurant.be.review.presentation.dto.common.ReviewResponseDto import com.restaurant.be.review.repository.ReviewLikeRepository import com.restaurant.be.review.repository.ReviewRepository import com.restaurant.be.user.repository.UserRepository @@ -44,8 +45,10 @@ class UpdateReviewService( reviewRepository.save(review) - val responseDto = - review.toDto(reviewLikeRepository.existsByReviewIdAndUserId(reviewId, user.id)) + val responseDto = ReviewResponseDto.toDto( + review, + reviewLikeRepository.existsByReviewIdAndUserId(reviewId, user.id) + ) return UpdateReviewResponse(responseDto) } diff --git a/src/main/kotlin/com/restaurant/be/review/presentation/dto/common/ReviewDto.kt b/src/main/kotlin/com/restaurant/be/review/presentation/dto/common/ReviewDto.kt index 131b000..227399c 100644 --- a/src/main/kotlin/com/restaurant/be/review/presentation/dto/common/ReviewDto.kt +++ b/src/main/kotlin/com/restaurant/be/review/presentation/dto/common/ReviewDto.kt @@ -57,4 +57,24 @@ data class ReviewResponseDto( val createdAt: LocalDateTime, @Schema(description = "리뷰 수정 시간") val modifiedAt: LocalDateTime -) +) { + companion object { + fun toDto(review: Review, isLikedByUser: Boolean): ReviewResponseDto { + return ReviewResponseDto( + id = review.id ?: 0, + userId = review.user.id ?: 0, + username = review.user.nickname, + profileImageUrl = review.user.profileImageUrl, + restaurantId = review.restaurantId, + rating = review.rating, + content = review.content, + imageUrls = review.images.map { it.imageUrl }, + isLike = isLikedByUser, + likeCount = review.likeCount, + viewCount = review.viewCount, + createdAt = review.createdAt, + modifiedAt = review.modifiedAt + ) + } + } +}