Skip to content

Commit

Permalink
feat: sort by dynamic query
Browse files Browse the repository at this point in the history
  • Loading branch information
minjun3021 committed Oct 18, 2023
1 parent 8bf0225 commit 7b58f3a
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ import jakarta.servlet.http.HttpServletRequest
import jakarta.validation.Valid
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.domain.Sort
import org.springframework.data.web.PageableDefault
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
Expand All @@ -22,6 +19,7 @@ import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import java.time.OffsetDateTime

@RestController
@RequestMapping("/events")
Expand Down Expand Up @@ -84,10 +82,11 @@ class EventController @Autowired constructor(
@GetMapping
fun getEvents(
request: HttpServletRequest,
@RequestParam(required = false) name: String?,
@PageableDefault(size = 10, sort = ["id"], direction = Sort.Direction.DESC) pageable: Pageable
@RequestParam sort: String?,
@RequestParam id: Int?,
@RequestParam time: OffsetDateTime?
): ResponseEntity<Page<Event>> {
val page = eventService.getEvents(name, pageable)
val page = eventService.getEvents(sort, id, time)

val headers = HttpHeaders()
headers.set("Content-Location", request.requestURI)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.group4.ticketingservice.repository

import com.group4.ticketingservice.entity.Event
import com.group4.ticketingservice.entity.QEvent.event
import com.group4.ticketingservice.utils.exception.CustomException
import com.group4.ticketingservice.utils.exception.ErrorCodes
import com.querydsl.core.types.dsl.BooleanExpression
import com.querydsl.jpa.impl.JPAQueryFactory
import org.springframework.data.jpa.repository.support.QuerydslRepositorySupport
import org.springframework.stereotype.Repository
import java.time.Duration
import java.time.OffsetDateTime

@Repository
class EventRepositorySupport(
private val queryFactory: JPAQueryFactory
) : QuerydslRepositorySupport(
Event::class.java
) {

private fun ltEventId(id: Int?): BooleanExpression? {
return if (id == null) null else event.id.lt(id)
}

fun getEvent(sort: String?, id: Int?, dateTime: OffsetDateTime?): List<Event> {
val whereSpecifier = if (id == null) {
when (sort) {
"deadline" -> event.reservationEndTime.after(OffsetDateTime.now())
"startDate" -> event.startDate.before(OffsetDateTime.now() + Duration.ofDays(60))
"createdAt" -> null
null -> null
else -> throw CustomException(ErrorCodes.MESSAGE_NOT_READABLE)
}
} else {
when (sort) {
"deadline" -> event.reservationEndTime.gt(dateTime).or(event.reservationEndTime.eq(dateTime).and(ltEventId(id)))
"startDate" -> event.startDate.gt(dateTime).or(event.startDate.eq(dateTime).and(ltEventId(id)))
"createdAt" -> event.createdAt.lt(dateTime).or(event.createdAt.eq(dateTime).and(ltEventId(id)))
null -> ltEventId(id)
else -> throw CustomException(ErrorCodes.MESSAGE_NOT_READABLE)
}
}

val orderSpecifier = when (sort) {
"deadline" -> event.reservationEndTime.asc()
"startDate" -> event.startDate.asc()
"createdAt" -> event.createdAt.desc()
null -> event.id.desc()
else -> throw CustomException(ErrorCodes.MESSAGE_NOT_READABLE)
}

return queryFactory.selectFrom(event)
.where(whereSpecifier)
.orderBy(orderSpecifier, event.id.desc())
.limit(10)
.fetch()
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package com.group4.ticketingservice.service

import com.group4.ticketingservice.dto.EventSpecifications
import com.group4.ticketingservice.entity.Event
import com.group4.ticketingservice.repository.EventRepository
import com.group4.ticketingservice.repository.EventRepositorySupport
import org.springframework.data.domain.Page
import org.springframework.data.domain.PageImpl
import org.springframework.data.domain.Pageable
import org.springframework.stereotype.Service
import java.time.OffsetDateTime

@Service
class EventService(
private val eventRepository: EventRepository
private val eventRepository: EventRepository,
private val eventRepositorySupport: EventRepositorySupport

) {
fun createEvent(
name: String,
Expand All @@ -36,8 +37,10 @@ class EventService(
return eventRepository.findById(id).orElse(null)
}

fun getEvents(name: String?, pageable: Pageable): Page<Event> {
val specification = EventSpecifications.withName(name)
return PageImpl(eventRepository.findAllBy(specification, pageable))
fun getEvents(sort: String?, id: Int?, time: OffsetDateTime?): Page<Event> {
// val specification = EventSpecifications.withName(name)
// val lastAccessId=13
// val lastAccessTime= OffsetDateTime.of(2024,9,6,0,0,0,0, ZoneOffset.UTC)
return PageImpl(eventRepositorySupport.getEvent(sort, id, time))
}
}

0 comments on commit 7b58f3a

Please sign in to comment.