Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Custom Jpa Method QueryDSL 로 변경 #232

Merged
merged 1 commit into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.swm_standard.phote.repository
package com.swm_standard.phote.repository.examrepository

import com.swm_standard.phote.entity.Workbook

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String>?, keywords: List<String>?): List<Question>
fun searchQuestionsList(
memberId: UUID,
tags: List<String>?,
keywords: List<String>?,
): List<Question>

fun searchQuestionsToAddList(
memberId: UUID,
workbookId: UUID,
tags: List<String>?,
keywords: List<String>?
keywords: List<String>?,
): List<SearchQuestionsToAddResponse>
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
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
import org.springframework.stereotype.Repository
import java.util.*

@Repository
interface QuestionRepository : JpaRepository<Question, UUID>, QuestionCustomRepository
interface QuestionRepository :
JpaRepository<Question, UUID>,
QuestionCustomRepository
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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

Expand All @@ -67,14 +58,11 @@ class QuestionSetCustomRepositoryImpl(

private fun toQuestionSetIds(workbookId: UUID): List<UUID> {
val questionSets: MutableList<QuestionSet> =
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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Workbook> =
jpaQueryFactory
.selectFrom(workbook)
.join(workbook.member, member)
.fetchJoin()
.where(workbook.member.id.eq(memberId))
.fetch()
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/com/swm_standard/phote/service/ExamService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Loading