Skip to content

Commit

Permalink
Merge pull request #109 from LeeSM0518/DVK-123-edit-review-list-view
Browse files Browse the repository at this point in the history
[DVK-123] fix: 리뷰 목록 조회 수정
  • Loading branch information
LeeSM0518 authored Nov 28, 2024
2 parents e098ba8 + 08c4ac0 commit 9d156b6
Show file tree
Hide file tree
Showing 13 changed files with 117 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ data class EbookDetailView(
@Schema(description = "판매자 정보")
val seller: EbookSellerView,
@Schema(description = "리뷰 정보")
val review: ReviewView,
val review: EbookReviewView,
@Schema(description = "페이지 개수")
val pageCount: Int,
@Schema(description = "PDF 식별자")
Expand All @@ -45,7 +45,7 @@ data class EbookDetailView(
descriptionImageList = this.descriptionImageList,
wishlistId = this.wishlistId,
title = this.title,
review = ReviewView(
review = EbookReviewView(
rating = this.reviewRating,
count = this.reviewCount,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.devooks.backend.ebook.v1.dto

import io.swagger.v3.oas.annotations.media.Schema

data class ReviewView(
data class EbookReviewView(
@Schema(description = "평점")
val rating: Double,
@Schema(description = "개수", implementation = Int::class)
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/com/devooks/backend/ebook/v1/dto/EbookView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ data class EbookView(
@Schema(description = "판매자 정보")
val seller: EbookSellerView,
@Schema(description = "리뷰 정보")
val review: ReviewView,
val review: EbookReviewView,
@Schema(description = "생성 날짜")
val createdDate: Instant,
@Schema(description = "수정 날짜")
Expand All @@ -39,7 +39,7 @@ data class EbookView(
nickname = this.sellerNickname,
profileImagePath = this.sellerProfileImagePath ?: "",
),
review = ReviewView(
review = EbookReviewView(
rating = this.reviewRating,
count = this.reviewCount,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import com.devooks.backend.auth.v1.service.TokenService
import com.devooks.backend.common.dto.PageResponse
import com.devooks.backend.common.dto.PageResponse.Companion.toResponse
import com.devooks.backend.ebook.v1.service.EbookService
import com.devooks.backend.member.v1.domain.Member
import com.devooks.backend.member.v1.service.MemberService
import com.devooks.backend.review.v1.controller.docs.ReviewControllerDocs
import com.devooks.backend.review.v1.domain.Review
import com.devooks.backend.review.v1.dto.CreateReviewCommand
Expand All @@ -18,6 +20,7 @@ import com.devooks.backend.review.v1.dto.ModifyReviewCommand
import com.devooks.backend.review.v1.dto.ModifyReviewRequest
import com.devooks.backend.review.v1.dto.ModifyReviewResponse
import com.devooks.backend.review.v1.dto.ModifyReviewResponse.Companion.toModifyReviewResponse
import com.devooks.backend.review.v1.dto.ReviewRow
import com.devooks.backend.review.v1.dto.ReviewView
import com.devooks.backend.review.v1.dto.ReviewView.Companion.toReviewView
import com.devooks.backend.review.v1.service.ReviewEventService
Expand Down Expand Up @@ -47,6 +50,7 @@ class ReviewController(
private val transactionService: TransactionService,
private val ebookService: EbookService,
private val reviewEventService: ReviewEventService,
private val memberService: MemberService,
) : ReviewControllerDocs {

@Transactional
Expand All @@ -63,8 +67,9 @@ class ReviewController(
ebookService.validate(command)
transactionService.validate(command)
val review: Review = reviewService.create(command)
val member: Member = memberService.findById(review.writerMemberId)
reviewEventService.publish(review)
return review.toCreateReviewResponse()
return review.toCreateReviewResponse(member)
}

@GetMapping
Expand All @@ -77,7 +82,7 @@ class ReviewController(
count: Int,
): PageResponse<ReviewView> {
val command = GetReviewsCommand(ebookId, page, count)
val reviewList: Page<Review> = reviewService.get(command)
val reviewList: Page<ReviewRow> = reviewService.get(command)
return reviewList.map { it.toReviewView() }.toResponse()
}

Expand All @@ -95,7 +100,8 @@ class ReviewController(
val requesterId = tokenService.getMemberId(Authorization(authorization))
val command: ModifyReviewCommand = request.toCommand(reviewId, requesterId)
val review: Review = reviewService.modify(command)
return review.toModifyReviewResponse()
val member: Member = memberService.findById(review.writerMemberId)
return review.toModifyReviewResponse(member)
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.devooks.backend.review.v1.dto

import com.devooks.backend.member.v1.domain.Member
import com.devooks.backend.review.v1.domain.Review
import com.devooks.backend.review.v1.dto.ReviewView.Companion.toReviewView

data class CreateReviewResponse(
val review: ReviewView,
) {
companion object {
fun Review.toCreateReviewResponse() = CreateReviewResponse(this.toReviewView())
fun Review.toCreateReviewResponse(member: Member) =
CreateReviewResponse(this.toReviewView(member))
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.devooks.backend.review.v1.dto

import com.devooks.backend.member.v1.domain.Member
import com.devooks.backend.review.v1.domain.Review
import com.devooks.backend.review.v1.dto.ReviewView.Companion.toReviewView

class ModifyReviewResponse(
val review: ReviewView
val review: ReviewView,
) {
companion object {
fun Review.toModifyReviewResponse() = ModifyReviewResponse(this.toReviewView())
fun Review.toModifyReviewResponse(member: Member) =
ModifyReviewResponse(this.toReviewView(member))
}
}
17 changes: 17 additions & 0 deletions src/main/kotlin/com/devooks/backend/review/v1/dto/ReviewRow.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.devooks.backend.review.v1.dto

import java.time.Instant
import java.util.*

data class ReviewRow(
val reviewId: UUID,
val content: String,
val rating: Int,
val ebookId: UUID,
val memberId: UUID,
val nickname: String,
val profileImagePath: String?,
val writtenDate: Instant,
val modifiedDate: Instant,
val commentCount: Int,
)
21 changes: 17 additions & 4 deletions src/main/kotlin/com/devooks/backend/review/v1/dto/ReviewView.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.devooks.backend.review.v1.dto

import com.devooks.backend.member.v1.domain.Member
import com.devooks.backend.review.v1.domain.Review
import com.devooks.backend.review.v1.dto.WriterView.Companion.toWriterView
import io.swagger.v3.oas.annotations.media.Schema
import java.time.Instant
import java.util.*
Expand All @@ -14,21 +16,32 @@ data class ReviewView(
val content: String,
@Schema(description = "전자책 식별자")
val ebookId: UUID,
@Schema(description = "작성자 회원 식별자")
val writerMemberId: UUID,
@Schema(description = "작성자")
val writer: WriterView,
@Schema(description = "작성 날짜")
val writtenDate: Instant,
@Schema(description = "수정 날짜")
val modifiedDate: Instant,
) {
companion object {
fun Review.toReviewView(): ReviewView =
fun ReviewRow.toReviewView(): ReviewView =
ReviewView(
id = this.reviewId,
rating = this.rating,
content = this.content,
ebookId = this.ebookId,
writer = this.toWriterView(),
writtenDate = this.writtenDate,
modifiedDate = this.modifiedDate,
)

fun Review.toReviewView(writer: Member): ReviewView =
ReviewView(
id = this.id,
rating = this.rating,
content = this.content,
ebookId = this.ebookId,
writerMemberId = this.writerMemberId,
writer = writer.toWriterView(),
writtenDate = this.writtenDate,
modifiedDate = this.modifiedDate,
)
Expand Down
26 changes: 26 additions & 0 deletions src/main/kotlin/com/devooks/backend/review/v1/dto/WriterView.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.devooks.backend.review.v1.dto

import com.devooks.backend.member.v1.domain.Member
import java.util.*

data class WriterView(
val memberId: UUID,
val nickname: String,
val profileImagePath: String,
) {
companion object {
fun ReviewRow.toWriterView() =
WriterView(
memberId = this.memberId,
nickname = this.nickname,
profileImagePath = this.profileImagePath ?: "",
)

fun Member.toWriterView() =
WriterView(
memberId = this.id,
nickname = this.nickname,
profileImagePath = this.profileImagePath,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package com.devooks.backend.review.v1.repository

import com.devooks.backend.common.config.database.JooqR2dbcRepository
import com.devooks.backend.jooq.tables.Review.Companion.REVIEW
import com.devooks.backend.jooq.tables.references.EBOOK
import com.devooks.backend.review.v1.domain.Review
import com.devooks.backend.jooq.tables.references.MEMBER
import com.devooks.backend.jooq.tables.references.REVIEW_COMMENT
import com.devooks.backend.review.v1.dto.GetReviewsCommand
import com.devooks.backend.review.v1.dto.ReviewRow
import java.util.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import org.jooq.impl.DSL
Expand All @@ -13,25 +15,42 @@ import org.springframework.stereotype.Repository
@Repository
class ReviewQueryRepository : JooqR2dbcRepository() {

suspend fun findBy(command: GetReviewsCommand): Flow<Review> =
suspend fun findBy(command: GetReviewsCommand): Flow<ReviewRow> =
query {
val reviewCountSubQuery =
select(
REVIEW_COMMENT.REVIEW_ID,
DSL.count().`as`("comment_count")
).from(
REVIEW_COMMENT
).groupBy(
REVIEW_COMMENT.REVIEW_ID,
).asTable("review_comment")

select(
REVIEW.REVIEW_ID.`as`("id"),
REVIEW.RATING,
REVIEW.REVIEW_ID,
REVIEW.CONTENT,
REVIEW.RATING,
REVIEW.EBOOK_ID,
REVIEW.WRITER_MEMBER_ID,
MEMBER.MEMBER_ID,
MEMBER.NICKNAME,
MEMBER.PROFILE_IMAGE_PATH,
REVIEW.WRITTEN_DATE,
REVIEW.MODIFIED_DATE
REVIEW.MODIFIED_DATE,
DSL.coalesce(reviewCountSubQuery.field("comment_count"), 0)
.`as`("comment_count")
).from(
REVIEW
.join(EBOOK).on(EBOOK.EBOOK_ID.eq(REVIEW.EBOOK_ID))
.join(MEMBER).on(REVIEW.WRITER_MEMBER_ID.eq(MEMBER.MEMBER_ID))
.leftJoin(reviewCountSubQuery).on(
REVIEW.REVIEW_ID.eq(reviewCountSubQuery.field("review_id", UUID::class.java))
)
).where(
REVIEW.EBOOK_ID.eq(command.ebookId)
).orderBy(
REVIEW.WRITTEN_DATE.desc(),
).offset(command.offset).limit(command.limit)
}.map { it.into(Review::class.java) }
}.map { it.into(ReviewRow::class.java) }

suspend fun countBy(command: GetReviewsCommand): Flow<Long> =
query {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.devooks.backend.review.v1.dto.CreateReviewCommentCommand
import com.devooks.backend.review.v1.dto.DeleteReviewCommand
import com.devooks.backend.review.v1.dto.GetReviewsCommand
import com.devooks.backend.review.v1.dto.ModifyReviewCommand
import com.devooks.backend.review.v1.dto.ReviewRow
import com.devooks.backend.review.v1.entity.ReviewEntity
import com.devooks.backend.review.v1.error.ReviewError
import com.devooks.backend.review.v1.repository.ReviewQueryRepository
Expand Down Expand Up @@ -33,7 +34,7 @@ class ReviewService(
return reviewRepository.save(entity).toDomain()
}

suspend fun get(command: GetReviewsCommand): Page<Review> {
suspend fun get(command: GetReviewsCommand): Page<ReviewRow> {
val reviews = reviewQueryRepository.findBy(command)
val count = reviewQueryRepository.countBy(command)
return PageImpl(reviews.toList(), command.pageable, count.first())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,13 @@ internal class ReviewCommentControllerTest @Autowired constructor(
assertThat(reviewComment.content).isEqualTo(request.content)

delay(100)
val notification = notificationRepository.findAll().toList().find { it.receiverId == review.writerMemberId }!!
val notification = notificationRepository.findAll().toList()
.find { it.receiverId == review.writer.memberId }!!
assertThat(notification.type).isEqualTo(NotificationType.REVIEW_COMMENT)
assertThat(notification.receiverId).isEqualTo(review.writerMemberId)
assertThat(notification.receiverId).isEqualTo(review.writer.memberId)
assertThat(notification.note["ebookId"]).isEqualTo(review.ebookId.toString())
assertThat(notification.note["reviewId"]).isEqualTo(review.id.toString())
assertThat(notification.note["receiverId"]).isEqualTo(review.writerMemberId.toString())
assertThat(notification.note["receiverId"]).isEqualTo(review.writer.memberId.toString())
assertThat(notification.note["commenterName"]).isEqualTo(expectedMember2.nickname)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ internal class ReviewControllerTest @Autowired constructor(
assertThat(review.rating).isEqualTo(createReviewRequest.rating)
assertThat(review.content).isEqualTo(createReviewRequest.content)
assertThat(review.ebookId).isEqualTo(createReviewRequest.ebookId)
assertThat(review.writerMemberId).isEqualTo(expectedMember2.id)
assertThat(review.writer.memberId).isEqualTo(expectedMember2.id)

delay(100)
val notification = notificationRepository.findAll().toList()[0]
Expand Down Expand Up @@ -174,7 +174,7 @@ internal class ReviewControllerTest @Autowired constructor(
assertThat(review.ebookId).isEqualTo(createReviewResponse.ebookId)
assertThat(review.content).isEqualTo(createReviewResponse.content)
assertThat(review.rating).isEqualTo(createReviewResponse.rating)
assertThat(review.writerMemberId).isEqualTo(createReviewResponse.writerMemberId)
assertThat(review.writer).isEqualTo(createReviewResponse.writer)
assertThat(review.writtenDate.toEpochMilli()).isEqualTo(createReviewResponse.writtenDate.toEpochMilli())
assertThat(review.modifiedDate.toEpochMilli()).isEqualTo(createReviewResponse.modifiedDate.toEpochMilli())
}
Expand Down

0 comments on commit 9d156b6

Please sign in to comment.