diff --git a/src/main/kotlin/com/group4/ticketingservice/controller/EventController.kt b/src/main/kotlin/com/group4/ticketingservice/controller/EventController.kt index 08d914a9..1904d7a3 100644 --- a/src/main/kotlin/com/group4/ticketingservice/controller/EventController.kt +++ b/src/main/kotlin/com/group4/ticketingservice/controller/EventController.kt @@ -10,6 +10,7 @@ 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 @@ -84,7 +85,7 @@ class EventController @Autowired constructor( fun getEvents( request: HttpServletRequest, @RequestParam(required = false) name: String?, - @PageableDefault(size = 10, sort = ["startDate", "id"]) pageable: Pageable + @PageableDefault(size = 10, sort = ["id"], direction = Sort.Direction.DESC) pageable: Pageable ): ResponseEntity> { val page = eventService.getEvents(name, pageable) diff --git a/src/main/kotlin/com/group4/ticketingservice/dto/ResponseDTO.kt b/src/main/kotlin/com/group4/ticketingservice/dto/ResponseDTO.kt index b08e500a..780b6814 100644 --- a/src/main/kotlin/com/group4/ticketingservice/dto/ResponseDTO.kt +++ b/src/main/kotlin/com/group4/ticketingservice/dto/ResponseDTO.kt @@ -6,8 +6,7 @@ data class SuccessResponseDTO( val timestamp: OffsetDateTime = OffsetDateTime.now(), val message: String = "success", var data: Any? = null, - val path: String? = null, - val totalElements: Long? = null + val path: String? = null ) data class ErrorResponseDTO( diff --git a/src/main/kotlin/com/group4/ticketingservice/repository/EventRepository.kt b/src/main/kotlin/com/group4/ticketingservice/repository/EventRepository.kt index eb6e7544..69e5ebf2 100644 --- a/src/main/kotlin/com/group4/ticketingservice/repository/EventRepository.kt +++ b/src/main/kotlin/com/group4/ticketingservice/repository/EventRepository.kt @@ -2,6 +2,8 @@ package com.group4.ticketingservice.repository import com.group4.ticketingservice.entity.Event import jakarta.persistence.LockModeType +import org.springframework.data.domain.Pageable +import org.springframework.data.jpa.domain.Specification import org.springframework.data.jpa.repository.JpaRepository import org.springframework.data.jpa.repository.JpaSpecificationExecutor import org.springframework.data.jpa.repository.Lock @@ -17,4 +19,7 @@ interface EventRepository : JpaRepository, JpaSpecificationExecutor< @Lock(LockModeType.OPTIMISTIC) @Query("select e from Event e where e.id = :id") fun findByIdWithOptimisicLock(id: Int): Event? + + @Query("select e from Event e") + fun findAllBy(name: Specification, pageable: Pageable): List } diff --git a/src/main/kotlin/com/group4/ticketingservice/service/EventService.kt b/src/main/kotlin/com/group4/ticketingservice/service/EventService.kt index 923c642a..0a8caf04 100644 --- a/src/main/kotlin/com/group4/ticketingservice/service/EventService.kt +++ b/src/main/kotlin/com/group4/ticketingservice/service/EventService.kt @@ -4,6 +4,7 @@ import com.group4.ticketingservice.dto.EventSpecifications import com.group4.ticketingservice.entity.Event import com.group4.ticketingservice.repository.EventRepository 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 @@ -37,6 +38,6 @@ class EventService( fun getEvents(name: String?, pageable: Pageable): Page { val specification = EventSpecifications.withName(name) - return eventRepository.findAll(specification, pageable) + return PageImpl(eventRepository.findAllBy(specification, pageable)) } } diff --git a/src/main/kotlin/com/group4/ticketingservice/utils/ResponseAdvice.kt b/src/main/kotlin/com/group4/ticketingservice/utils/ResponseAdvice.kt index 192c1c7f..27b9a894 100644 --- a/src/main/kotlin/com/group4/ticketingservice/utils/ResponseAdvice.kt +++ b/src/main/kotlin/com/group4/ticketingservice/utils/ResponseAdvice.kt @@ -125,8 +125,7 @@ class ResponseAdvice( return SuccessResponseDTO( data = data, - path = response.headers.getFirst("Content-Location"), - totalElements = page.totalElements + path = response.headers.getFirst("Content-Location") ) as T? // return page as T? } diff --git a/src/test/kotlin/com/group4/ticketingservice/bookmark/BookmarkControllerTest.kt b/src/test/kotlin/com/group4/ticketingservice/bookmark/BookmarkControllerTest.kt index e03e0756..d007b452 100644 --- a/src/test/kotlin/com/group4/ticketingservice/bookmark/BookmarkControllerTest.kt +++ b/src/test/kotlin/com/group4/ticketingservice/bookmark/BookmarkControllerTest.kt @@ -142,8 +142,8 @@ class BookmarkControllerTest( ) ) ) - val totalElements: Long = 100 - val page: Page = PageImpl(content, pageable, totalElements) + + val page: Page = PageImpl(content) @Test @WithAuthUser(email = testUserName, id = testUserId) @@ -239,7 +239,6 @@ class BookmarkControllerTest( // then result .andExpect(status().isOk) - .andExpect(jsonPath("$.totalElements").value(totalElements)) .andExpect(jsonPath("$.data.[0].id").value(11)) .andExpect(jsonPath("$.data.[1].id").value(12)) } diff --git a/src/test/kotlin/com/group4/ticketingservice/event/EventControllerTest.kt b/src/test/kotlin/com/group4/ticketingservice/event/EventControllerTest.kt index e1fa8fa5..c5933162 100644 --- a/src/test/kotlin/com/group4/ticketingservice/event/EventControllerTest.kt +++ b/src/test/kotlin/com/group4/ticketingservice/event/EventControllerTest.kt @@ -108,8 +108,8 @@ class EventControllerTest( maxAttendees = 10 ) ) - val totalElements: Long = 100 - val page: Page = PageImpl(content, pageable, totalElements) + + val page: Page = PageImpl(content) val emptyPage: Page = PageImpl(listOf(), pageable, listOf().size.toLong()) @Test @@ -191,7 +191,6 @@ class EventControllerTest( // Then result .andExpect(status().isOk) - .andExpect(jsonPath("$.totalElements").value(totalElements)) .andExpect(jsonPath("$.data.[0].id").value(2)) .andExpect(jsonPath("$.data.[1].id").value(1)) } diff --git a/src/test/kotlin/com/group4/ticketingservice/event/EventServiceTest.kt b/src/test/kotlin/com/group4/ticketingservice/event/EventServiceTest.kt index 7f1ca1e7..9f0279de 100644 --- a/src/test/kotlin/com/group4/ticketingservice/event/EventServiceTest.kt +++ b/src/test/kotlin/com/group4/ticketingservice/event/EventServiceTest.kt @@ -84,8 +84,8 @@ class EventServiceTest { maxAttendees = 10 ) ) - val totalElements: Long = 100 - val page: Page = PageImpl(content, pageable, totalElements) + + val page: Page = PageImpl(content) val emptyPage: Page = PageImpl(listOf(), pageable, listOf().size.toLong()) val name = "코딩" @@ -115,21 +115,21 @@ class EventServiceTest { @Test fun `EventService_getEvents invoke EventRepository_findAll`() { - every { eventRepository.findAll(any(), pageable) } returns page + every { eventRepository.findAllBy(any(), pageable) } returns content eventService.getEvents(name, pageable) - verify(exactly = 1) { eventRepository.findAll(any(), pageable) } + verify(exactly = 1) { eventRepository.findAllBy(any(), pageable) } } @Test fun `EventService_getEvents return page with pagination and sorting`() { // Given - every { eventRepository.findAll(any(), pageable) } returns page + every { eventRepository.findAllBy(any(), pageable) } returns content // When val result: Page = eventService.getEvents(null, pageable) // Then - assertThat(result.totalElements).isEqualTo(totalElements) + assertThat(result.numberOfElements).isEqualTo(content.size) assertThat(result.content[0].id).isEqualTo(content[0].id) assertThat(result.content[1].id).isEqualTo(content[1].id)