Skip to content

Commit 72fa259

Browse files
committed
refactor: Add writer info in comment response
1 parent 86e995f commit 72fa259

File tree

10 files changed

+83
-26
lines changed

10 files changed

+83
-26
lines changed

quiz-service/quiz-application/app-api/src/main/kotlin/com/grepp/quizy/quiz/api/comment/CommentApi.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ class CommentApi(
3232
request.toParentCommentId(),
3333
request.toContent(),
3434
)
35-
.id
3635
)
3736

3837
@GetMapping

quiz-service/quiz-application/app-api/src/main/kotlin/com/grepp/quizy/quiz/api/comment/dto/CommentResponse.kt

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package com.grepp.quizy.quiz.api.comment.dto
22

33
import com.fasterxml.jackson.annotation.JsonInclude
44
import com.grepp.quizy.quiz.domain.comment.Comment
5+
import com.grepp.quizy.quiz.domain.comment.Writer
6+
import com.grepp.quizy.quiz.domain.user.QuizUser
57

68
@JsonInclude(JsonInclude.Include.NON_EMPTY)
79
data class CommentResponse(
810
val id: Long,
911
val quizId: Long,
10-
val writerId: Long,
12+
val writer: WriterResponse,
1113
val parentCommentId: Long,
1214
val content: String,
1315
val childComments: List<CommentResponse>,
@@ -19,7 +21,7 @@ data class CommentResponse(
1921
return CommentResponse(
2022
id = comment.id.value,
2123
quizId = comment.quizId.value,
22-
writerId = comment.writerId.value,
24+
writer = WriterResponse.from(comment.writer),
2325
parentCommentId = comment.parentCommentId.value,
2426
content = comment.content.value,
2527
childComments =
@@ -30,3 +32,19 @@ data class CommentResponse(
3032
}
3133
}
3234
}
35+
36+
data class WriterResponse(
37+
val id: Long,
38+
val name: String,
39+
val profileImageUrl: String
40+
) {
41+
companion object {
42+
fun from(writer: Writer): WriterResponse {
43+
return WriterResponse(
44+
id = writer.id.value,
45+
name = writer.name,
46+
profileImageUrl = writer.profileImageUrl
47+
)
48+
}
49+
}
50+
}

quiz-service/quiz-domain/src/main/kotlin/com/grepp/quizy/quiz/domain/comment/Comment.kt

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ package com.grepp.quizy.quiz.domain.comment
33
import com.grepp.quizy.common.dto.DateTime
44
import com.grepp.quizy.quiz.domain.comment.exception.CommentException
55
import com.grepp.quizy.quiz.domain.quiz.QuizId
6+
import com.grepp.quizy.quiz.domain.user.QuizUser
67
import com.grepp.quizy.quiz.domain.user.UserId
78

89
class Comment(
910
val quizId: QuizId,
10-
val writerId: UserId,
11+
val writer: Writer,
1112
val parentCommentId: CommentId,
1213
private var _content: CommentContent,
1314
val id: CommentId = CommentId(0),
@@ -37,8 +38,24 @@ class Comment(
3738
}
3839

3940
fun validateOwner(userId: UserId) {
40-
if (writerId != userId) {
41+
if (writer.id != userId) {
4142
throw CommentException.NoPermission
4243
}
4344
}
4445
}
46+
47+
data class Writer(
48+
val id: UserId,
49+
val name: String = "",
50+
val profileImageUrl: String = ""
51+
) {
52+
companion object {
53+
fun from(writer: QuizUser): Writer {
54+
return Writer(
55+
id = writer.id,
56+
name = writer.name,
57+
profileImageUrl = writer.imgPath
58+
)
59+
}
60+
}
61+
}

quiz-service/quiz-domain/src/main/kotlin/com/grepp/quizy/quiz/domain/comment/CommentAppender.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.grepp.quizy.quiz.domain.comment
33
import com.grepp.quizy.quiz.domain.quiz.QuizId
44
import com.grepp.quizy.quiz.domain.quiz.QuizReader
55
import com.grepp.quizy.quiz.domain.quiz.QuizUpdater
6+
import com.grepp.quizy.quiz.domain.user.QuizUser
67
import com.grepp.quizy.quiz.domain.user.UserId
78
import org.springframework.stereotype.Component
89

@@ -21,7 +22,7 @@ class CommentAppender(
2122
val comment =
2223
Comment(
2324
quizId = quizId,
24-
writerId = writerId,
25+
writer = Writer(writerId),
2526
parentCommentId = parentCommentId,
2627
_content = content,
2728
)

quiz-service/quiz-domain/src/main/kotlin/com/grepp/quizy/quiz/domain/comment/CommentCreateUseCase.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ interface CommentCreateUseCase {
99
writerId: UserId,
1010
parentCommentId: CommentId,
1111
content: CommentContent,
12-
): Comment
12+
): CommentId
1313
}

quiz-service/quiz-domain/src/main/kotlin/com/grepp/quizy/quiz/domain/comment/CommentService.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ class CommentService(
2323
writerId: UserId,
2424
parentCommentId: CommentId,
2525
content: CommentContent,
26-
): Comment {
26+
): CommentId {
2727
return commentAppender.append(
2828
quizId,
2929
writerId,
3030
parentCommentId,
3131
content,
32-
)
32+
).id
3333
}
3434

3535
override fun getComments(quizId: QuizId): List<Comment> {

quiz-service/quiz-domain/src/main/kotlin/com/grepp/quizy/quiz/domain/user/QuizUser.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import com.grepp.quizy.quiz.domain.useranswer.events.UserAnsweredEvent
55

66
class QuizUser(
77
val id: UserId,
8-
private var profile: UserProfile,
8+
private var profile: UserProfile = UserProfile("", ""),
99
private val _interests: MutableList<QuizCategory> = mutableListOf(),
1010
private val _achievements: MutableList<QuizUserAchievement> = mutableListOf(),
1111
private var _stats: UserStats = UserStats(id)

quiz-service/quiz-infra/src/main/kotlin/com/grepp/quizy/quiz/infra/comment/entity/CommentEntity.kt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import com.grepp.quizy.jpa.BaseTimeEntity
55
import com.grepp.quizy.quiz.domain.comment.Comment
66
import com.grepp.quizy.quiz.domain.comment.CommentContent
77
import com.grepp.quizy.quiz.domain.comment.CommentId
8+
import com.grepp.quizy.quiz.domain.comment.Writer
89
import com.grepp.quizy.quiz.domain.quiz.QuizId
910
import com.grepp.quizy.quiz.domain.user.UserId
11+
import com.grepp.quizy.quiz.infra.user.entity.QuizUserEntity
1012
import jakarta.persistence.Entity
1113
import jakarta.persistence.GeneratedValue
1214
import jakarta.persistence.GenerationType
@@ -27,11 +29,15 @@ class CommentEntity(
2729
createdAt: LocalDateTime,
2830
updatedAt: LocalDateTime,
2931
) : BaseTimeEntity(createdAt, updatedAt) {
30-
fun toDomain() =
32+
fun toDomain(writer: QuizUserEntity) =
3133
Comment(
3234
id = CommentId(id),
3335
quizId = QuizId(quizId),
34-
writerId = UserId(writerId),
36+
writer = Writer(
37+
id = UserId(writerId),
38+
name = writer.name,
39+
profileImageUrl = writer.imgPath,
40+
),
3541
parentCommentId = CommentId(parentCommentId),
3642
_content = CommentContent(content),
3743
dateTime = DateTime(createdAt, updatedAt),
@@ -42,9 +48,8 @@ class CommentEntity(
4248
CommentEntity(
4349
id = comment.id.value,
4450
quizId = comment.quizId.value,
45-
writerId = comment.writerId.value,
46-
parentCommentId =
47-
comment.parentCommentId.value,
51+
writerId = comment.writer.id.value,
52+
parentCommentId = comment.parentCommentId.value,
4853
content = comment.content.value,
4954
createdAt = comment.dateTime.createdAt,
5055
updatedAt = comment.dateTime.updatedAt,

quiz-service/quiz-infra/src/main/kotlin/com/grepp/quizy/quiz/infra/comment/repository/CommentRepositoryAdapter.kt

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,49 @@ import com.grepp.quizy.quiz.domain.comment.Comment
44
import com.grepp.quizy.quiz.domain.comment.CommentId
55
import com.grepp.quizy.quiz.domain.comment.CommentRepository
66
import com.grepp.quizy.quiz.domain.quiz.QuizId
7+
import com.grepp.quizy.quiz.domain.user.exception.QuizUserException
78
import com.grepp.quizy.quiz.infra.comment.entity.CommentEntity
9+
import com.grepp.quizy.quiz.infra.user.repository.QuizUserJpaRepository
810
import org.springframework.stereotype.Repository
911
import org.springframework.transaction.annotation.Transactional
1012

1113
@Repository
1214
@Transactional
1315
class CommentRepositoryAdapter(
14-
private val commentJpaRepository: CommentJpaRepository
16+
private val commentJpaRepository: CommentJpaRepository,
17+
private val quizUserJpaRepository: QuizUserJpaRepository
1518
) : CommentRepository {
1619

17-
override fun save(comment: Comment): Comment =
18-
commentJpaRepository
19-
.save(CommentEntity.from(comment))
20-
.toDomain()
20+
override fun save(comment: Comment): Comment {
21+
val commentEntity = commentJpaRepository
22+
.save(CommentEntity.from(comment))
23+
return commentEntity.toDomain(
24+
quizUserJpaRepository.findById(commentEntity.writerId)
25+
.orElseThrow { QuizUserException.NotFound }
26+
)
27+
}
28+
2129

2230
override fun findById(id: CommentId): Comment? =
2331
commentJpaRepository
2432
.findById(id.value)
25-
.map { it.toDomain() }
33+
.map {
34+
val writer = quizUserJpaRepository
35+
.findById(it.writerId)
36+
.orElseThrow{ QuizUserException.NotFound }
37+
it.toDomain(writer)
38+
}
2639
.orElse(null)
2740

28-
override fun findAllByQuizId(quizId: QuizId): List<Comment> =
29-
commentJpaRepository
30-
.findAllByQuizId(quizId.value)
31-
.map(CommentEntity::toDomain)
41+
override fun findAllByQuizId(quizId: QuizId): List<Comment> {
42+
val commentEntities = commentJpaRepository.findAllByQuizId(quizId.value)
43+
val writerEntities = quizUserJpaRepository.findAllById(
44+
commentEntities.map { it.writerId }
45+
).associateBy { it.id }
46+
return commentEntities.map {
47+
it.toDomain(writerEntities[it.writerId] ?: throw QuizUserException.NotFound)
48+
}
49+
}
3250

3351
override fun delete(comment: Comment) =
3452
commentJpaRepository.delete(CommentEntity.from(comment))

quiz-service/quiz-infra/src/main/kotlin/com/grepp/quizy/quiz/infra/quizread/messaging/listener/QuizCDCEventHandler.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ class QuizCDCEventHandler(
1616
initActions()
1717
}
1818

19-
2019
final override fun initActions() {
2120
actions.put(DebeziumEvent.DebeziumEventPayloadOperation.CREATE) { _, after ->
2221
after?.let { quizDocumentSynchronizer.createQuiz(it) }

0 commit comments

Comments
 (0)