Skip to content

Commit

Permalink
[KAN-108] 리뷰 테스트 코드 (테스트 커버리지 100%) - 일부 로직 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
sinkyoungdeok committed May 30, 2024
1 parent 3edb7ed commit 5dae94c
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 27 deletions.
19 changes: 0 additions & 19 deletions src/main/kotlin/com/restaurant/be/review/domain/entity/Review.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,7 +25,10 @@ class GetReviewService(

return GetReviewsResponse(
reviewsWithLikes.map {
it.review.toDto(it.isLikedByUser)
ReviewResponseDto.toDto(
it.review,
it.isLikedByUser
)
}
)
}
Expand All @@ -40,7 +44,8 @@ class GetReviewService(
reviewWithLikes.review.incrementViewCount()
}

val responseDto = reviewWithLikes.review.toDto(
val responseDto = ReviewResponseDto.toDto(
reviewWithLikes.review,
reviewWithLikes.isLikedByUser
)

Expand All @@ -55,7 +60,10 @@ class GetReviewService(

return GetMyReviewsResponse(
reviewsWithLikes.map {
it.review.toDto(it.isLikedByUser)
ReviewResponseDto.toDto(
it.review,
it.isLikedByUser
)
}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
}
}

0 comments on commit 5dae94c

Please sign in to comment.