Skip to content

Commit

Permalink
hotfix: searchQuestion sortingType, tagIds 요청 쿼리스트링 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
annahxxl committed Mar 8, 2024
1 parent 5711bd4 commit 092bc4a
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 33 deletions.
17 changes: 17 additions & 0 deletions data/src/main/kotlin/com/kw/data/common/dto/SearchSortingType.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.kw.data.common.dto

enum class SearchSortingType {
// RECOMMENDED,
LATEST,
POPULAR;

companion object {
fun from(input: String): SearchSortingType {
try {
return SearchSortingType.valueOf(input.uppercase())
} catch (e: IllegalArgumentException) {
throw IllegalArgumentException("존재하지 않는 정렬 타입입니다: $input")
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
package com.kw.data.domain.bundle.dto.request

import com.kw.data.common.dto.SearchSortingType

data class BundleSearchCondition(
val sortingType: SortingType?,
val sortingType: SearchSortingType?,
val tagIds: List<Long>?,
val keyword: String?
) {
enum class SortingType {
// RECOMMENDED,
LATEST,
POPULAR;

companion object {
fun from(input: String): SortingType {
try {
return valueOf(input.uppercase())
} catch (e: IllegalArgumentException) {
throw IllegalArgumentException("존재하지 않는 정렬 타입입니다: $input")
}
}
}
}
}
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.kw.data.domain.bundle.repository

import com.kw.data.common.dto.SearchSortingType
import com.kw.data.domain.bundle.Bundle
import com.kw.data.domain.bundle.QBundle.Companion.bundle
import com.kw.data.domain.bundle.QBundleTag.Companion.bundleTag
Expand All @@ -21,7 +22,7 @@ class BundleCustomRepositoryImpl(private val queryFactory: JPAQueryFactory) : Bu

if (condition.tagIds != null) {
query
.leftJoin(bundle.bundleTags, bundleTag).fetchJoin()
.leftJoin(bundle.bundleTags, bundleTag)
.where(bundleTag.tag.id.`in`(condition.tagIds))
}

Expand All @@ -41,9 +42,9 @@ class BundleCustomRepositoryImpl(private val queryFactory: JPAQueryFactory) : Bu
bundle.scrapeCount.desc()
} else {
when (condition.sortingType) {
// BundleSearchCondition.SortingType.RECOMMENDED -> TODO() //TODO
BundleSearchCondition.SortingType.LATEST -> bundle.createdAt.desc()
BundleSearchCondition.SortingType.POPULAR -> bundle.scrapeCount.desc()
// SortingType.RECOMMENDED -> TODO() //TODO
SearchSortingType.LATEST -> bundle.createdAt.desc()
SearchSortingType.POPULAR -> bundle.scrapeCount.desc()
}
}
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.kw.infraquerydsl.domain.question.dto
package com.kw.data.domain.question.dto

import com.kw.data.common.dto.SearchSortingType

data class QuestionSearchDto(
val sortingType: SearchSortingType?,
val tagIds: List<Long>?,
val keyword: String?,
val page: Long,
val size: Long
val page: Long = 1,
val size: Long = 10
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.kw.data.domain.question.repository

import com.kw.data.domain.question.Question
import com.kw.infraquerydsl.domain.question.dto.QuestionSearchDto
import com.kw.data.domain.question.dto.QuestionSearchDto

interface QuestionCustomRepository {
fun searchQuestion(questionSearchDto: QuestionSearchDto): List<Question>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.kw.data.domain.question.repository

import com.kw.data.common.dto.SearchSortingType
import com.kw.data.domain.question.QQuestion.Companion.question
import com.kw.data.domain.question.QQuestionTag.Companion.questionTag
import com.kw.data.domain.question.Question
import com.kw.infraquerydsl.domain.question.dto.QuestionSearchDto
import com.kw.data.domain.question.dto.QuestionSearchDto
import com.querydsl.core.types.dsl.BooleanExpression
import com.querydsl.jpa.impl.JPAQueryFactory
import org.springframework.stereotype.Repository
Expand All @@ -15,13 +16,32 @@ class QuestionCustomRepositoryImpl(val jpaQueryFactory: JPAQueryFactory) : Quest
val page = questionSearchDto.page
val size = questionSearchDto.size

return jpaQueryFactory.selectFrom(question)
val query = jpaQueryFactory
.selectFrom(question)
.leftJoin(question.questionTags, questionTag).fetchJoin()
.where(
keyword?.let { containsKeyword(keyword) }
)
.orderBy(
if (questionSearchDto.sortingType == null) {
question.shareCount.desc()
} else {
when (questionSearchDto.sortingType) {
// SortingType.RECOMMENDED -> TODO() //TODO
SearchSortingType.LATEST -> question.createdAt.desc()
SearchSortingType.POPULAR -> question.shareCount.desc()
}
}
)
.offset((page - 1) * size)
.limit(size)
.fetch()

if (questionSearchDto.tagIds != null) {
query
.where(questionTag.tag.id.`in`(questionSearchDto.tagIds))
}

return query.fetch()
}

private fun containsKeyword(keyword: String): BooleanExpression? {
Expand All @@ -33,12 +53,21 @@ class QuestionCustomRepositoryImpl(val jpaQueryFactory: JPAQueryFactory) : Quest
val page = questionSearchDto.page
val size = questionSearchDto.size

val count = jpaQueryFactory.select(question.count())
val query = jpaQueryFactory
.select(question.count())
.from(question)
.where(
keyword?.let { containsKeyword(keyword) }
)
.fetchOne()

if (questionSearchDto.tagIds != null) {
query
.leftJoin(question.questionTags, questionTag)
.where(questionTag.tag.id.`in`(questionSearchDto.tagIds))
}

val count = query.fetchOne()

if (count == 0L) {
return 1L
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.kw.api.domain.question.dto.request

import com.kw.data.common.dto.SearchSortingType

data class QuestionSearchRequest(
val sortingType: SearchSortingType?,
val tagIds: List<Long>?,
val keyword: String?,
val page: Long = 1,
val size: Long = 10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import com.kw.data.domain.member.Member
import com.kw.data.domain.question.Question
import com.kw.data.domain.question.QuestionReport
import com.kw.data.domain.question.QuestionTag
import com.kw.data.domain.question.dto.QuestionSearchDto
import com.kw.data.domain.question.repository.QuestionReportRepository
import com.kw.data.domain.question.repository.QuestionRepository
import com.kw.data.domain.tag.Tag
import com.kw.data.domain.tag.repository.TagRepository
import com.kw.infraquerydsl.domain.question.dto.QuestionSearchDto
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

Expand Down Expand Up @@ -92,9 +92,11 @@ class QuestionService(
@Transactional(readOnly = true)
fun searchQuestion(request: QuestionSearchRequest): QuestionListResponse {
val questionSearchDto = QuestionSearchDto(
sortingType = request.sortingType,
tagIds = request.tagIds,
keyword = request.keyword,
page = request.page,
size = request.size
size = request.size,
)
val questions = questionRepository.searchQuestion(questionSearchDto)
val questionResponses = questions.map { QuestionResponse.from(it) }
Expand Down

0 comments on commit 092bc4a

Please sign in to comment.