Skip to content

Commit be9659e

Browse files
authored
테스트 조회 관련 프로젝션 DTO을 수정한다. (#107)
* fix: modify exam projection * fix: add solved exam read
1 parent f6daed5 commit be9659e

File tree

6 files changed

+61
-14
lines changed

6 files changed

+61
-14
lines changed

domain/src/main/kotlin/com/gotchai/domain/exam/adapter/in/ExamQueryService.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ class ExamQueryService(
2424
override fun getExams(userId: Long): List<GetExamResult> =
2525
examQueryPort
2626
.getExamsWithExamHistoryByUserIdAndIsSolved(userId, null)
27-
.map { GetExamResult.of(it.exam, it.examHistory) }
27+
.map { GetExamResult.of(it.exam, isSolved = it.examHistory?.isSolved ?: false) }
2828

2929
@Transactional(readOnly = true)
3030
override fun getMyExams(userId: Long): List<GetMyExamResult> =
3131
examQueryPort
32-
.getExamsWithExamHistoryByUserIdAndIsSolved(userId, true)
32+
.getSolvedExamsWithExamHistoryByUserIdAndIsSolved(userId, true)
3333
.map { GetMyExamResult.of(it.exam, it.examHistory) }
3434

3535
@Transactional(readOnly = true)

domain/src/main/kotlin/com/gotchai/domain/exam/dto/projection/ExamWithExamHistory.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ import com.gotchai.domain.exam.entity.ExamHistory
55

66
data class ExamWithExamHistory(
77
val exam: Exam,
8-
val examHistory: ExamHistory
8+
val examHistory: ExamHistory?
99
)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.gotchai.domain.exam.dto.projection
2+
3+
import com.gotchai.domain.exam.entity.Exam
4+
import com.gotchai.domain.exam.entity.ExamHistory
5+
6+
data class SolvedExamWithExamHistory(
7+
val exam: Exam,
8+
val examHistory: ExamHistory
9+
)

domain/src/main/kotlin/com/gotchai/domain/exam/dto/result/GetExamResult.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.gotchai.domain.exam.dto.result
22

33
import com.gotchai.domain.exam.entity.Exam
4-
import com.gotchai.domain.exam.entity.ExamHistory
54
import java.time.LocalDateTime
65

76
data class GetExamResult(
@@ -20,7 +19,7 @@ data class GetExamResult(
2019
companion object {
2120
fun of(
2221
exam: Exam,
23-
examHistory: ExamHistory
22+
isSolved: Boolean
2423
): GetExamResult =
2524
with(exam) {
2625
GetExamResult(
@@ -33,7 +32,7 @@ data class GetExamResult(
3332
iconImage = iconImage,
3433
coverImage = coverImage,
3534
theme = theme,
36-
isSolved = examHistory.isSolved,
35+
isSolved = isSolved,
3736
createdAt = createdAt
3837
)
3938
}

domain/src/main/kotlin/com/gotchai/domain/exam/port/out/ExamQueryPort.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.gotchai.domain.exam.port.out
22

33
import com.gotchai.domain.exam.dto.projection.ExamWithExamHistory
4+
import com.gotchai.domain.exam.dto.projection.SolvedExamWithExamHistory
45
import com.gotchai.domain.exam.entity.Exam
56

67
interface ExamQueryPort {
@@ -13,5 +14,10 @@ interface ExamQueryPort {
1314
isSolved: Boolean?
1415
): List<ExamWithExamHistory>
1516

17+
fun getSolvedExamsWithExamHistoryByUserIdAndIsSolved(
18+
userId: Long,
19+
isSolved: Boolean?
20+
): List<SolvedExamWithExamHistory>
21+
1622
fun getExamCount(): Long
1723
}

storage/rdb/src/main/kotlin/com/gotchai/storage/rdb/exam/adapter/out/ExamQueryAdapter.kt

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.gotchai.storage.rdb.exam.adapter.out
22

33
import com.gotchai.domain.exam.dto.projection.ExamWithExamHistory
4+
import com.gotchai.domain.exam.dto.projection.SolvedExamWithExamHistory
45
import com.gotchai.domain.exam.entity.Exam
56
import com.gotchai.domain.exam.port.out.ExamQueryPort
67
import com.gotchai.storage.rdb.exam.entity.ExamEntity
@@ -26,32 +27,64 @@ class ExamQueryAdapter(
2627
@ReadOnlyTransactional
2728
override fun getExams(): List<Exam> = examJpaRepository.findAll().map { it.toExam() }
2829

30+
@ReadOnlyTransactional
31+
override fun getSolvedExamsWithExamHistoryByUserIdAndIsSolved(
32+
userId: Long,
33+
isSolved: Boolean?
34+
): List<SolvedExamWithExamHistory> {
35+
val query =
36+
jpql {
37+
selectNew<Pair<ExamEntity, ExamHistoryEntity>>(
38+
entity(ExamEntity::class),
39+
entity(ExamHistoryEntity::class)
40+
).from(
41+
entity(ExamEntity::class),
42+
innerJoin(ExamHistoryEntity::class)
43+
.on(
44+
path(ExamHistoryEntity::examId)
45+
.eq(path(ExamEntity::id))
46+
.and(path(ExamHistoryEntity::userId).eq(userId))
47+
.apply { if (isSolved != null) and(path(ExamHistoryEntity::isSolved).eq(isSolved)) }
48+
)
49+
)
50+
}
51+
val pairs = entityManager.createQuery(query, jdslRenderContext).resultList
52+
53+
return pairs.map { (examEntity, examHistoryEntity) ->
54+
SolvedExamWithExamHistory(
55+
exam = examEntity.toExam(),
56+
examHistory = examHistoryEntity.toExamHistory()
57+
)
58+
}
59+
}
60+
2961
@ReadOnlyTransactional
3062
override fun getExamsWithExamHistoryByUserIdAndIsSolved(
3163
userId: Long,
3264
isSolved: Boolean?
3365
): List<ExamWithExamHistory> {
3466
val query =
3567
jpql {
36-
selectNew<Pair<ExamEntity, ExamHistoryEntity>>(
68+
selectNew<Pair<ExamEntity, ExamHistoryEntity?>>(
3769
entity(ExamEntity::class),
3870
entity(ExamHistoryEntity::class)
3971
).from(
4072
entity(ExamEntity::class),
41-
leftJoin(ExamHistoryEntity::class).on(
42-
path(ExamHistoryEntity::examId)
43-
.eq(path(ExamEntity::id))
44-
.and(path(ExamHistoryEntity::userId).eq(userId))
45-
.apply { if (isSolved != null) and(path(ExamHistoryEntity::isSolved).eq(isSolved)) }
46-
)
73+
leftJoin(ExamHistoryEntity::class)
74+
.on(
75+
path(ExamHistoryEntity::examId)
76+
.eq(path(ExamEntity::id))
77+
.and(path(ExamHistoryEntity::userId).eq(userId))
78+
.apply { if (isSolved != null) and(path(ExamHistoryEntity::isSolved).eq(isSolved)) }
79+
)
4780
)
4881
}
4982
val pairs = entityManager.createQuery(query, jdslRenderContext).resultList
5083

5184
return pairs.map { (examEntity, examHistoryEntity) ->
5285
ExamWithExamHistory(
5386
exam = examEntity.toExam(),
54-
examHistory = examHistoryEntity.toExamHistory()
87+
examHistory = examHistoryEntity?.toExamHistory()
5588
)
5689
}
5790
}

0 commit comments

Comments
 (0)