Skip to content

Commit

Permalink
상품 리뷰 등록 시 사용자 중복 조회 제거
Browse files Browse the repository at this point in the history
  • Loading branch information
psh10066 committed Oct 17, 2024
1 parent 2441a74 commit 63f8e60
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.commerce.service.product.application.service

import com.commerce.common.model.member.MemberRepository
import com.commerce.common.model.member.Member
import com.commerce.common.model.review.Review
import com.commerce.common.model.review.ReviewRepository
import com.commerce.common.model.review.ReviewWithMember
Expand All @@ -14,19 +14,15 @@ import java.time.LocalDateTime
@Service
class ReviewService(
private val reviewRepository: ReviewRepository,
private val memberRepository: MemberRepository,
) : ReviewUseCase{
) : ReviewUseCase {

@Transactional(readOnly = true)
override fun getProductReviews(productId: Long): List<ReviewWithMember> {
return reviewRepository.findByProductIdOrderByCreatedAtDesc(productId)
}

@Transactional
override fun addReviewToProduct(addReviewCommand: AddReviewCommand): Long {

val member = memberRepository.findByEmail(addReviewCommand.email)
?: throw IllegalArgumentException("해당 아이디(${addReviewCommand.email})를 가진 유저가 존재하지 않습니다.")
override fun addReviewToProduct(member: Member, addReviewCommand: AddReviewCommand): Long {

val review = Review.byProduct(
productId = addReviewCommand.productId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.commerce.service.product.application.usecase

import com.commerce.common.model.member.Member
import com.commerce.common.model.review.ReviewWithMember
import com.commerce.common.model.review.ReviewWithProduct
import com.commerce.service.product.application.usecase.command.AddReviewCommand

interface ReviewUseCase {
fun getProductReviews(productId: Long): List<ReviewWithMember>
fun addReviewToProduct(addReviewCommand: AddReviewCommand): Long
fun addReviewToProduct(member: Member, addReviewCommand: AddReviewCommand): Long
fun getMemberReviews(memberId: Long): List<ReviewWithProduct>
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import java.math.BigDecimal

data class AddReviewCommand(
val productId: Long,
val email: String,
val content: String,
val score: BigDecimal,
)
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ReviewController(

@PostMapping
fun addReviewToProduct(@AuthenticationPrincipal member: Member, @RequestBody reviewCreateRequest: ReviewCreateRequest): CommonResponse<ReviewCreateResponse> {
val reviewId = reviewUseCase.addReviewToProduct(reviewCreateRequest.toCommand(member.email))
val reviewId = reviewUseCase.addReviewToProduct(member, reviewCreateRequest.toCommand())
return CommonResponse.ok(ReviewCreateResponse.of(reviewId))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ data class ReviewCreateRequest(
val content: String,
val score: BigDecimal,
) {
fun toCommand(email: String): AddReviewCommand {
fun toCommand(): AddReviewCommand {
return AddReviewCommand(
productId = productId,
email = email,
content = content,
score = score,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ReviewServiceTest {
private lateinit var reviewRepository: ReviewRepository
private lateinit var memberRepository: MemberRepository
private val reviewService by lazy {
ReviewService(reviewRepository, memberRepository)
ReviewService(reviewRepository)
}

@BeforeEach
Expand Down Expand Up @@ -87,11 +87,10 @@ class ReviewServiceTest {

var addReviewCommand = AddReviewCommand(
productId = 1L,
email = "[email protected]",
content = "재미있는 책이에요",
score = BigDecimal(5),
)
reviewService.addReviewToProduct(addReviewCommand)
reviewService.addReviewToProduct(newMember, addReviewCommand)

val reviewsByProduct = reviewRepository.findByProductIdOrderByCreatedAtDesc(1L)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,11 @@ class ReviewControllerTest(
@Test
fun addReviewToProduct() {
given(reviewUseCase.addReviewToProduct(
testMember,
AddReviewCommand(
productId = 1L,
content = "재미있어요!",
score = BigDecimal(5),
email = "[email protected]",
)
)).willReturn(1L)

Expand Down

0 comments on commit 63f8e60

Please sign in to comment.