diff --git a/data/src/main/kotlin/com/kw/data/common/dto/SearchSortingType.kt b/data/src/main/kotlin/com/kw/data/common/dto/SearchSortingType.kt new file mode 100644 index 0000000..cc10c50 --- /dev/null +++ b/data/src/main/kotlin/com/kw/data/common/dto/SearchSortingType.kt @@ -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") + } + } + } +} diff --git a/data/src/main/kotlin/com/kw/data/domain/bundle/dto/request/BundleSearchCondition.kt b/data/src/main/kotlin/com/kw/data/domain/bundle/dto/request/BundleSearchCondition.kt index b408f6d..2082a75 100644 --- a/data/src/main/kotlin/com/kw/data/domain/bundle/dto/request/BundleSearchCondition.kt +++ b/data/src/main/kotlin/com/kw/data/domain/bundle/dto/request/BundleSearchCondition.kt @@ -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?, 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") - } - } - } - } -} +) diff --git a/data/src/main/kotlin/com/kw/data/domain/bundle/repository/BundleCustomRepositoryImpl.kt b/data/src/main/kotlin/com/kw/data/domain/bundle/repository/BundleCustomRepositoryImpl.kt index cea3838..d2bf19d 100644 --- a/data/src/main/kotlin/com/kw/data/domain/bundle/repository/BundleCustomRepositoryImpl.kt +++ b/data/src/main/kotlin/com/kw/data/domain/bundle/repository/BundleCustomRepositoryImpl.kt @@ -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 @@ -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)) } @@ -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() } } ) diff --git a/data/src/main/kotlin/com/kw/data/domain/question/dto/QuestionSearchDto.kt b/data/src/main/kotlin/com/kw/data/domain/question/dto/QuestionSearchDto.kt index 4b140c6..a7641d0 100644 --- a/data/src/main/kotlin/com/kw/data/domain/question/dto/QuestionSearchDto.kt +++ b/data/src/main/kotlin/com/kw/data/domain/question/dto/QuestionSearchDto.kt @@ -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?, val keyword: String?, - val page: Long, - val size: Long + val page: Long = 1, + val size: Long = 10 ) diff --git a/data/src/main/kotlin/com/kw/data/domain/question/repository/QuestionCustomRepository.kt b/data/src/main/kotlin/com/kw/data/domain/question/repository/QuestionCustomRepository.kt index ef7f823..ceef125 100644 --- a/data/src/main/kotlin/com/kw/data/domain/question/repository/QuestionCustomRepository.kt +++ b/data/src/main/kotlin/com/kw/data/domain/question/repository/QuestionCustomRepository.kt @@ -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 diff --git a/data/src/main/kotlin/com/kw/data/domain/question/repository/QuestionCustomRepositoryImpl.kt b/data/src/main/kotlin/com/kw/data/domain/question/repository/QuestionCustomRepositoryImpl.kt index 5485cf2..721cc13 100644 --- a/data/src/main/kotlin/com/kw/data/domain/question/repository/QuestionCustomRepositoryImpl.kt +++ b/data/src/main/kotlin/com/kw/data/domain/question/repository/QuestionCustomRepositoryImpl.kt @@ -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 @@ -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? { @@ -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 } diff --git a/server/api/src/main/kotlin/com/kw/api/domain/question/dto/request/QuestionSearchRequest.kt b/server/api/src/main/kotlin/com/kw/api/domain/question/dto/request/QuestionSearchRequest.kt index a61b6cd..02dc278 100644 --- a/server/api/src/main/kotlin/com/kw/api/domain/question/dto/request/QuestionSearchRequest.kt +++ b/server/api/src/main/kotlin/com/kw/api/domain/question/dto/request/QuestionSearchRequest.kt @@ -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?, val keyword: String?, val page: Long = 1, val size: Long = 10 diff --git a/server/api/src/main/kotlin/com/kw/api/domain/question/service/QuestionService.kt b/server/api/src/main/kotlin/com/kw/api/domain/question/service/QuestionService.kt index 9df66c6..69d4572 100644 --- a/server/api/src/main/kotlin/com/kw/api/domain/question/service/QuestionService.kt +++ b/server/api/src/main/kotlin/com/kw/api/domain/question/service/QuestionService.kt @@ -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 @@ -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) }