From 2503315d02414e7701ee890bc32a969aa9976e90 Mon Sep 17 00:00:00 2001 From: adorableco Date: Sun, 29 Sep 2024 12:40:22 +0900 Subject: [PATCH] =?UTF-8?q?Refactor:=20Custom=20Jpa=20Method=20QueryDSL=20?= =?UTF-8?q?=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CustomWorkbookRepositoryImpl.kt | 21 ---------- .../ExamCustomRepository.kt | 2 +- .../ExamCustomRepositoryImpl.kt | 2 +- .../{ => examrepository}/ExamRepository.kt | 2 +- .../QuestionCustomRepository.kt | 10 +++-- .../QuestionCustomRepositoryImpl.kt | 2 +- .../QuestionRepository.kt | 6 ++- .../QuestionSetCustomRepository.kt | 2 +- .../QuestionSetCustomRepositoryImpl.kt | 40 +++++++------------ .../QuestionSetRepository.kt | 2 +- .../CustomWorkbookRepository.kt | 2 +- .../CustomWorkbookRepositoryImpl.kt | 19 +++++++++ .../WorkbookRepository.kt | 2 +- .../swm_standard/phote/service/ExamService.kt | 6 +-- .../phote/service/QuestionService.kt | 2 +- .../phote/service/WorkbookService.kt | 6 +-- .../phote/entity/QuestionRepositoryTest.kt | 2 +- 17 files changed, 60 insertions(+), 68 deletions(-) delete mode 100644 src/main/kotlin/com/swm_standard/phote/repository/CustomWorkbookRepositoryImpl.kt rename src/main/kotlin/com/swm_standard/phote/repository/{ => examrepository}/ExamCustomRepository.kt (71%) rename src/main/kotlin/com/swm_standard/phote/repository/{ => examrepository}/ExamCustomRepositoryImpl.kt (92%) rename src/main/kotlin/com/swm_standard/phote/repository/{ => examrepository}/ExamRepository.kt (82%) rename src/main/kotlin/com/swm_standard/phote/repository/{ => questionrepository}/QuestionCustomRepository.kt (58%) rename src/main/kotlin/com/swm_standard/phote/repository/{ => questionrepository}/QuestionCustomRepositoryImpl.kt (98%) rename src/main/kotlin/com/swm_standard/phote/repository/{ => questionrepository}/QuestionRepository.kt (54%) rename src/main/kotlin/com/swm_standard/phote/repository/{ => questionsetrepository}/QuestionSetCustomRepository.kt (83%) rename src/main/kotlin/com/swm_standard/phote/repository/{ => questionsetrepository}/QuestionSetCustomRepositoryImpl.kt (63%) rename src/main/kotlin/com/swm_standard/phote/repository/{ => questionsetrepository}/QuestionSetRepository.kt (87%) rename src/main/kotlin/com/swm_standard/phote/repository/{ => workbookrepository}/CustomWorkbookRepository.kt (73%) create mode 100644 src/main/kotlin/com/swm_standard/phote/repository/workbookrepository/CustomWorkbookRepositoryImpl.kt rename src/main/kotlin/com/swm_standard/phote/repository/{ => workbookrepository}/WorkbookRepository.kt (78%) diff --git a/src/main/kotlin/com/swm_standard/phote/repository/CustomWorkbookRepositoryImpl.kt b/src/main/kotlin/com/swm_standard/phote/repository/CustomWorkbookRepositoryImpl.kt deleted file mode 100644 index 0fe5409..0000000 --- a/src/main/kotlin/com/swm_standard/phote/repository/CustomWorkbookRepositoryImpl.kt +++ /dev/null @@ -1,21 +0,0 @@ -package com.swm_standard.phote.repository - -import com.swm_standard.phote.entity.Workbook -import jakarta.persistence.EntityManager -import jakarta.persistence.PersistenceContext -import java.util.UUID - -class CustomWorkbookRepositoryImpl : CustomWorkbookRepository { - @PersistenceContext - private lateinit var em: EntityManager - - override fun findWorkbooksByMember(memberId: UUID): List = - em - .createQuery( - "select w from Workbook w" + - " join fetch w.member m" + - " where m.id = :memberId", - Workbook::class.java, - ).setParameter("memberId", memberId) - .resultList -} diff --git a/src/main/kotlin/com/swm_standard/phote/repository/ExamCustomRepository.kt b/src/main/kotlin/com/swm_standard/phote/repository/examrepository/ExamCustomRepository.kt similarity index 71% rename from src/main/kotlin/com/swm_standard/phote/repository/ExamCustomRepository.kt rename to src/main/kotlin/com/swm_standard/phote/repository/examrepository/ExamCustomRepository.kt index 3775d07..31ba932 100644 --- a/src/main/kotlin/com/swm_standard/phote/repository/ExamCustomRepository.kt +++ b/src/main/kotlin/com/swm_standard/phote/repository/examrepository/ExamCustomRepository.kt @@ -1,4 +1,4 @@ -package com.swm_standard.phote.repository +package com.swm_standard.phote.repository.examrepository import com.swm_standard.phote.entity.Workbook diff --git a/src/main/kotlin/com/swm_standard/phote/repository/ExamCustomRepositoryImpl.kt b/src/main/kotlin/com/swm_standard/phote/repository/examrepository/ExamCustomRepositoryImpl.kt similarity index 92% rename from src/main/kotlin/com/swm_standard/phote/repository/ExamCustomRepositoryImpl.kt rename to src/main/kotlin/com/swm_standard/phote/repository/examrepository/ExamCustomRepositoryImpl.kt index abe737d..9a7fa6e 100644 --- a/src/main/kotlin/com/swm_standard/phote/repository/ExamCustomRepositoryImpl.kt +++ b/src/main/kotlin/com/swm_standard/phote/repository/examrepository/ExamCustomRepositoryImpl.kt @@ -1,4 +1,4 @@ -package com.swm_standard.phote.repository +package com.swm_standard.phote.repository.examrepository import com.querydsl.jpa.impl.JPAQueryFactory import com.swm_standard.phote.entity.QExam.exam diff --git a/src/main/kotlin/com/swm_standard/phote/repository/ExamRepository.kt b/src/main/kotlin/com/swm_standard/phote/repository/examrepository/ExamRepository.kt similarity index 82% rename from src/main/kotlin/com/swm_standard/phote/repository/ExamRepository.kt rename to src/main/kotlin/com/swm_standard/phote/repository/examrepository/ExamRepository.kt index 3a3b345..9b909ca 100644 --- a/src/main/kotlin/com/swm_standard/phote/repository/ExamRepository.kt +++ b/src/main/kotlin/com/swm_standard/phote/repository/examrepository/ExamRepository.kt @@ -1,4 +1,4 @@ -package com.swm_standard.phote.repository +package com.swm_standard.phote.repository.examrepository import com.swm_standard.phote.entity.Exam import org.springframework.data.jpa.repository.JpaRepository diff --git a/src/main/kotlin/com/swm_standard/phote/repository/QuestionCustomRepository.kt b/src/main/kotlin/com/swm_standard/phote/repository/questionrepository/QuestionCustomRepository.kt similarity index 58% rename from src/main/kotlin/com/swm_standard/phote/repository/QuestionCustomRepository.kt rename to src/main/kotlin/com/swm_standard/phote/repository/questionrepository/QuestionCustomRepository.kt index f5c275f..2686c08 100644 --- a/src/main/kotlin/com/swm_standard/phote/repository/QuestionCustomRepository.kt +++ b/src/main/kotlin/com/swm_standard/phote/repository/questionrepository/QuestionCustomRepository.kt @@ -1,16 +1,20 @@ -package com.swm_standard.phote.repository +package com.swm_standard.phote.repository.questionrepository import com.swm_standard.phote.dto.SearchQuestionsToAddResponse import com.swm_standard.phote.entity.Question import java.util.* interface QuestionCustomRepository { - fun searchQuestionsList(memberId: UUID, tags: List?, keywords: List?): List + fun searchQuestionsList( + memberId: UUID, + tags: List?, + keywords: List?, + ): List fun searchQuestionsToAddList( memberId: UUID, workbookId: UUID, tags: List?, - keywords: List? + keywords: List?, ): List } diff --git a/src/main/kotlin/com/swm_standard/phote/repository/QuestionCustomRepositoryImpl.kt b/src/main/kotlin/com/swm_standard/phote/repository/questionrepository/QuestionCustomRepositoryImpl.kt similarity index 98% rename from src/main/kotlin/com/swm_standard/phote/repository/QuestionCustomRepositoryImpl.kt rename to src/main/kotlin/com/swm_standard/phote/repository/questionrepository/QuestionCustomRepositoryImpl.kt index a61ba13..30413cf 100644 --- a/src/main/kotlin/com/swm_standard/phote/repository/QuestionCustomRepositoryImpl.kt +++ b/src/main/kotlin/com/swm_standard/phote/repository/questionrepository/QuestionCustomRepositoryImpl.kt @@ -1,4 +1,4 @@ -package com.swm_standard.phote.repository +package com.swm_standard.phote.repository.questionrepository import com.querydsl.jpa.impl.JPAQueryFactory import com.swm_standard.phote.dto.SearchQuestionsToAddResponse diff --git a/src/main/kotlin/com/swm_standard/phote/repository/QuestionRepository.kt b/src/main/kotlin/com/swm_standard/phote/repository/questionrepository/QuestionRepository.kt similarity index 54% rename from src/main/kotlin/com/swm_standard/phote/repository/QuestionRepository.kt rename to src/main/kotlin/com/swm_standard/phote/repository/questionrepository/QuestionRepository.kt index 6fd6e20..cb260a8 100644 --- a/src/main/kotlin/com/swm_standard/phote/repository/QuestionRepository.kt +++ b/src/main/kotlin/com/swm_standard/phote/repository/questionrepository/QuestionRepository.kt @@ -1,4 +1,4 @@ -package com.swm_standard.phote.repository +package com.swm_standard.phote.repository.questionrepository import com.swm_standard.phote.entity.Question import org.springframework.data.jpa.repository.JpaRepository @@ -6,4 +6,6 @@ import org.springframework.stereotype.Repository import java.util.* @Repository -interface QuestionRepository : JpaRepository, QuestionCustomRepository +interface QuestionRepository : + JpaRepository, + QuestionCustomRepository diff --git a/src/main/kotlin/com/swm_standard/phote/repository/QuestionSetCustomRepository.kt b/src/main/kotlin/com/swm_standard/phote/repository/questionsetrepository/QuestionSetCustomRepository.kt similarity index 83% rename from src/main/kotlin/com/swm_standard/phote/repository/QuestionSetCustomRepository.kt rename to src/main/kotlin/com/swm_standard/phote/repository/questionsetrepository/QuestionSetCustomRepository.kt index 21e1cce..ecffa96 100644 --- a/src/main/kotlin/com/swm_standard/phote/repository/QuestionSetCustomRepository.kt +++ b/src/main/kotlin/com/swm_standard/phote/repository/questionsetrepository/QuestionSetCustomRepository.kt @@ -1,4 +1,4 @@ -package com.swm_standard.phote.repository +package com.swm_standard.phote.repository.questionsetrepository import com.swm_standard.phote.dto.ReadQuestionsInWorkbookResponse import com.swm_standard.phote.entity.Workbook diff --git a/src/main/kotlin/com/swm_standard/phote/repository/QuestionSetCustomRepositoryImpl.kt b/src/main/kotlin/com/swm_standard/phote/repository/questionsetrepository/QuestionSetCustomRepositoryImpl.kt similarity index 63% rename from src/main/kotlin/com/swm_standard/phote/repository/QuestionSetCustomRepositoryImpl.kt rename to src/main/kotlin/com/swm_standard/phote/repository/questionsetrepository/QuestionSetCustomRepositoryImpl.kt index 297b024..c8fdcfd 100644 --- a/src/main/kotlin/com/swm_standard/phote/repository/QuestionSetCustomRepositoryImpl.kt +++ b/src/main/kotlin/com/swm_standard/phote/repository/questionsetrepository/QuestionSetCustomRepositoryImpl.kt @@ -1,13 +1,11 @@ -package com.swm_standard.phote.repository +package com.swm_standard.phote.repository.questionsetrepository import com.querydsl.jpa.impl.JPAQueryFactory import com.swm_standard.phote.dto.ReadQuestionsInWorkbookResponse +import com.swm_standard.phote.entity.QQuestion.question import com.swm_standard.phote.entity.QQuestionSet.questionSet -import com.swm_standard.phote.entity.Question import com.swm_standard.phote.entity.QuestionSet import com.swm_standard.phote.entity.Workbook -import jakarta.persistence.EntityManager -import jakarta.persistence.PersistenceContext import org.springframework.stereotype.Repository import java.util.UUID import java.util.stream.Collectors @@ -16,9 +14,6 @@ import java.util.stream.Collectors class QuestionSetCustomRepositoryImpl( private val jpaQueryFactory: JPAQueryFactory, ) : QuestionSetCustomRepository { - @PersistenceContext - private lateinit var em: EntityManager - override fun findMaxSequenceByWorkbookId(workbook: Workbook): Int { val maxSequence: Int? = jpaQueryFactory @@ -35,17 +30,13 @@ class QuestionSetCustomRepositoryImpl( val questionSetIds = toQuestionSetIds(workbookId) val questions = - em - .createQuery( - "select q" + - " from Question q" + - " join q.questionSet qs" + - " join fetch q.tags t" + - " where qs.id in :questionSetIds" + - " order by qs.sequence", - Question::class.java, - ).setParameter("questionSetIds", questionSetIds) - .resultList + jpaQueryFactory + .selectFrom(question) + .join(question.questionSet, questionSet) + .fetchJoin() + .where(questionSet.id.`in`(questionSetIds)) + .orderBy(questionSet.id.asc()) + .fetch() var index = 0 @@ -67,14 +58,11 @@ class QuestionSetCustomRepositoryImpl( private fun toQuestionSetIds(workbookId: UUID): List { val questionSets: MutableList = - em - .createQuery( - "select qs from QuestionSet qs" + - " where qs.workbook.id = :workbookId" + - " order by qs.sequence", - QuestionSet::class.java, - ).setParameter("workbookId", workbookId) - .resultList + jpaQueryFactory + .selectFrom(questionSet) + .where(questionSet.id.eq(workbookId)) + .orderBy(questionSet.sequence.asc()) + .fetch() return questionSets .stream() diff --git a/src/main/kotlin/com/swm_standard/phote/repository/QuestionSetRepository.kt b/src/main/kotlin/com/swm_standard/phote/repository/questionsetrepository/QuestionSetRepository.kt similarity index 87% rename from src/main/kotlin/com/swm_standard/phote/repository/QuestionSetRepository.kt rename to src/main/kotlin/com/swm_standard/phote/repository/questionsetrepository/QuestionSetRepository.kt index 8dc1b98..bbcc2bf 100644 --- a/src/main/kotlin/com/swm_standard/phote/repository/QuestionSetRepository.kt +++ b/src/main/kotlin/com/swm_standard/phote/repository/questionsetrepository/QuestionSetRepository.kt @@ -1,4 +1,4 @@ -package com.swm_standard.phote.repository +package com.swm_standard.phote.repository.questionsetrepository import com.swm_standard.phote.entity.QuestionSet import org.springframework.data.jpa.repository.JpaRepository diff --git a/src/main/kotlin/com/swm_standard/phote/repository/CustomWorkbookRepository.kt b/src/main/kotlin/com/swm_standard/phote/repository/workbookrepository/CustomWorkbookRepository.kt similarity index 73% rename from src/main/kotlin/com/swm_standard/phote/repository/CustomWorkbookRepository.kt rename to src/main/kotlin/com/swm_standard/phote/repository/workbookrepository/CustomWorkbookRepository.kt index b9c8dd1..68a7d75 100644 --- a/src/main/kotlin/com/swm_standard/phote/repository/CustomWorkbookRepository.kt +++ b/src/main/kotlin/com/swm_standard/phote/repository/workbookrepository/CustomWorkbookRepository.kt @@ -1,4 +1,4 @@ -package com.swm_standard.phote.repository +package com.swm_standard.phote.repository.workbookrepository import com.swm_standard.phote.entity.Workbook import java.util.UUID diff --git a/src/main/kotlin/com/swm_standard/phote/repository/workbookrepository/CustomWorkbookRepositoryImpl.kt b/src/main/kotlin/com/swm_standard/phote/repository/workbookrepository/CustomWorkbookRepositoryImpl.kt new file mode 100644 index 0000000..52891e8 --- /dev/null +++ b/src/main/kotlin/com/swm_standard/phote/repository/workbookrepository/CustomWorkbookRepositoryImpl.kt @@ -0,0 +1,19 @@ +package com.swm_standard.phote.repository.workbookrepository + +import com.querydsl.jpa.impl.JPAQueryFactory +import com.swm_standard.phote.entity.QMember.member +import com.swm_standard.phote.entity.QWorkbook.workbook +import com.swm_standard.phote.entity.Workbook +import java.util.UUID + +class CustomWorkbookRepositoryImpl( + private val jpaQueryFactory: JPAQueryFactory, +) : CustomWorkbookRepository { + override fun findWorkbooksByMember(memberId: UUID): List = + jpaQueryFactory + .selectFrom(workbook) + .join(workbook.member, member) + .fetchJoin() + .where(workbook.member.id.eq(memberId)) + .fetch() +} diff --git a/src/main/kotlin/com/swm_standard/phote/repository/WorkbookRepository.kt b/src/main/kotlin/com/swm_standard/phote/repository/workbookrepository/WorkbookRepository.kt similarity index 78% rename from src/main/kotlin/com/swm_standard/phote/repository/WorkbookRepository.kt rename to src/main/kotlin/com/swm_standard/phote/repository/workbookrepository/WorkbookRepository.kt index c9f2cd8..0754c9f 100644 --- a/src/main/kotlin/com/swm_standard/phote/repository/WorkbookRepository.kt +++ b/src/main/kotlin/com/swm_standard/phote/repository/workbookrepository/WorkbookRepository.kt @@ -1,4 +1,4 @@ -package com.swm_standard.phote.repository +package com.swm_standard.phote.repository.workbookrepository import com.swm_standard.phote.entity.Workbook import org.springframework.data.jpa.repository.JpaRepository diff --git a/src/main/kotlin/com/swm_standard/phote/service/ExamService.kt b/src/main/kotlin/com/swm_standard/phote/service/ExamService.kt index 2304154..4808250 100644 --- a/src/main/kotlin/com/swm_standard/phote/service/ExamService.kt +++ b/src/main/kotlin/com/swm_standard/phote/service/ExamService.kt @@ -15,10 +15,10 @@ import com.swm_standard.phote.entity.Category import com.swm_standard.phote.entity.Exam import com.swm_standard.phote.entity.Question import com.swm_standard.phote.repository.AnswerRepository -import com.swm_standard.phote.repository.ExamRepository import com.swm_standard.phote.repository.MemberRepository -import com.swm_standard.phote.repository.QuestionRepository -import com.swm_standard.phote.repository.WorkbookRepository +import com.swm_standard.phote.repository.examrepository.ExamRepository +import com.swm_standard.phote.repository.questionrepository.QuestionRepository +import com.swm_standard.phote.repository.workbookrepository.WorkbookRepository import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.async diff --git a/src/main/kotlin/com/swm_standard/phote/service/QuestionService.kt b/src/main/kotlin/com/swm_standard/phote/service/QuestionService.kt index ad6e86c..9faf386 100644 --- a/src/main/kotlin/com/swm_standard/phote/service/QuestionService.kt +++ b/src/main/kotlin/com/swm_standard/phote/service/QuestionService.kt @@ -15,8 +15,8 @@ import com.swm_standard.phote.dto.TransformQuestionResponse import com.swm_standard.phote.entity.Question import com.swm_standard.phote.entity.Tag import com.swm_standard.phote.repository.MemberRepository -import com.swm_standard.phote.repository.QuestionRepository import com.swm_standard.phote.repository.TagRepository +import com.swm_standard.phote.repository.questionrepository.QuestionRepository import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.async diff --git a/src/main/kotlin/com/swm_standard/phote/service/WorkbookService.kt b/src/main/kotlin/com/swm_standard/phote/service/WorkbookService.kt index 419fdff..9b98678 100644 --- a/src/main/kotlin/com/swm_standard/phote/service/WorkbookService.kt +++ b/src/main/kotlin/com/swm_standard/phote/service/WorkbookService.kt @@ -20,9 +20,9 @@ import com.swm_standard.phote.entity.Question import com.swm_standard.phote.entity.QuestionSet import com.swm_standard.phote.entity.Workbook import com.swm_standard.phote.repository.MemberRepository -import com.swm_standard.phote.repository.QuestionRepository -import com.swm_standard.phote.repository.QuestionSetRepository -import com.swm_standard.phote.repository.WorkbookRepository +import com.swm_standard.phote.repository.questionrepository.QuestionRepository +import com.swm_standard.phote.repository.questionsetrepository.QuestionSetRepository +import com.swm_standard.phote.repository.workbookrepository.WorkbookRepository import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional import java.time.LocalDateTime diff --git a/src/test/kotlin/com/swm_standard/phote/entity/QuestionRepositoryTest.kt b/src/test/kotlin/com/swm_standard/phote/entity/QuestionRepositoryTest.kt index 82cf11d..89f4935 100644 --- a/src/test/kotlin/com/swm_standard/phote/entity/QuestionRepositoryTest.kt +++ b/src/test/kotlin/com/swm_standard/phote/entity/QuestionRepositoryTest.kt @@ -1,6 +1,6 @@ package com.swm_standard.phote.entity -import com.swm_standard.phote.repository.QuestionRepository +import com.swm_standard.phote.repository.questionrepository.QuestionRepository import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertThrows import org.mockito.Mock