Skip to content

Commit

Permalink
Merge branch 'QB-64' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
phjppo0918 committed Jan 6, 2024
2 parents 4c9a27f + 8db90d8 commit c087375
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import site.qbox.qboxserver.domain.lecture.command.entity.QLecture.lecture
import site.qbox.qboxserver.domain.lecture.query.dto.QLectureRes
import site.qbox.qboxserver.domain.member.command.entity.QMember.member
import site.qbox.qboxserver.domain.member.query.MemberQuery
import site.qbox.qboxserver.domain.question.command.entity.QQuestion
import site.qbox.qboxserver.domain.question.command.entity.QQuestion.question
import site.qbox.qboxserver.domain.question.query.dto.*
import site.qbox.qboxserver.global.annotation.QueryService
import site.qbox.qboxserver.global.querydsl.util.SortQueryGenerator

@QueryService
class QuestionDao(
private val queryFactory: JPAQueryFactory,
private val sortQueryGenerator: SortQueryGenerator,
) {
fun findAllBy(condition: QuestionCondition, pageable: Pageable): List<QuestionSummary> {
return queryFactory
Expand All @@ -22,7 +25,7 @@ class QuestionDao(
.join(member).on(question.writerId.eq(MemberQuery.id))
.offset(pageable.offset)
.limit(pageable.pageSize.toLong())
.orderBy(question.createdAt.desc())
.orderBy(*sortQueryGenerator.generate(pageable.sort))
.fetch()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package site.qbox.qboxserver.global.querydsl.util

import com.querydsl.core.types.Order
import com.querydsl.core.types.OrderSpecifier
import com.querydsl.core.types.dsl.*
import org.springframework.data.domain.Sort
import org.springframework.stereotype.Component

@Component
class SortQueryGenerator {
fun generate(sort: Sort): Array<OrderSpecifier<*>> {
val specs = sort.map {
val direction = when {
it.isAscending -> Order.ASC
else -> Order.DESC
}
val path = Expressions.comparablePath(Comparable::class.java, it.property)
OrderSpecifier(direction, path)
}.toList()

return specs.toTypedArray()
}
}
4 changes: 4 additions & 0 deletions src/main/resources/templates/api-docs.html
Original file line number Diff line number Diff line change
Expand Up @@ -2095,6 +2095,10 @@ <h3 id="_http_request_17"><a class="link" href="#_http_request_17">HTTP request<
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>size</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">페이지 당 보여줄 컨텐츠 수(default: 10)</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>sort</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">정렬 기준, 복수개 넣어도 됨.(ex - title,createdAt), 내림차순 정렬 시 맨 마지막에 desc 넣기(ex- title,desc) (optional)</p></td>
</tr>
</tbody>
</table>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import io.kotest.matchers.shouldBe
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.data.domain.PageRequest
import org.springframework.data.domain.Sort
import site.qbox.qboxserver.domain.lecture.command.LectureRepo
import site.qbox.qboxserver.domain.lecture.command.entity.Lecture
import site.qbox.qboxserver.domain.member.command.MemberRepo
Expand Down Expand Up @@ -58,6 +59,18 @@ class QuestionDaoTest : DescribeSpec() {
val result = questionDao.findAllBy(condition, PageRequest.of(0, 100))
result.size shouldBe 3
}

it("정렬 기준을 설정해서 조회한다") {
val condition = QuestionCondition(lectureCode = "456", lectureDepart = 1)
val result = questionDao.findAllBy(condition, PageRequest.of(0, 100, Sort.Direction.DESC, "title"))
result[0].title shouldBe "제목4"
result[1].title shouldBe "제목3"

val resultAsc = questionDao.findAllBy(condition, PageRequest.of(0, 100, Sort.Direction.ASC, "title"))
resultAsc[0].title shouldBe "제목3"
resultAsc[1].title shouldBe "제목4"
}

it("Id를 통한 조회를 수행한다") {
val any = questionRepo.findAll()[0]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class QuestionQueryCtrlTest : WebClientDocsTest() {
params["writerNickname"] = condition.writerNickname
params["size"] = "100"
params["page"] = "0"
params["sort"] = "title"

val action = performGet("/questions", params)

Expand All @@ -62,6 +63,7 @@ class QuestionQueryCtrlTest : WebClientDocsTest() {
parameterWithName("lectureDepart").description("강의 해당 학과 ID"),
parameterWithName("page").description("page 번호"),
parameterWithName("size").description("페이지 당 보여줄 컨텐츠 수(default: 10)"),
parameterWithName("sort").description("정렬 기준, 복수개 넣어도 됨.(ex - title,createdAt), 내림차순 정렬 시 맨 마지막에 desc 넣기(ex- title,desc) (optional)"),
),
responseFields(
fieldWithPath("[].id").type(JsonFieldType.NUMBER).description("ID"),
Expand Down

0 comments on commit c087375

Please sign in to comment.