Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/113 remove total count #124

Merged
merged 1 commit into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<Page<Event>> {
val page = eventService.getEvents(name, pageable)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,4 +19,7 @@ interface EventRepository : JpaRepository<Event, Int>, 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<Event>, pageable: Pageable): List<Event>
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -37,6 +38,6 @@ class EventService(

fun getEvents(name: String?, pageable: Pageable): Page<Event> {
val specification = EventSpecifications.withName(name)
return eventRepository.findAll(specification, pageable)
return PageImpl(eventRepository.findAllBy(specification, pageable))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ class ResponseAdvice<T>(

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?
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ class BookmarkControllerTest(
)
)
)
val totalElements: Long = 100
val page: Page<Bookmark> = PageImpl(content, pageable, totalElements)

val page: Page<Bookmark> = PageImpl(content)

@Test
@WithAuthUser(email = testUserName, id = testUserId)
Expand Down Expand Up @@ -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))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ class EventControllerTest(
maxAttendees = 10
)
)
val totalElements: Long = 100
val page: Page<Event> = PageImpl(content, pageable, totalElements)

val page: Page<Event> = PageImpl(content)
val emptyPage: Page<Event> = PageImpl(listOf(), pageable, listOf<Event>().size.toLong())

@Test
Expand Down Expand Up @@ -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))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ class EventServiceTest {
maxAttendees = 10
)
)
val totalElements: Long = 100
val page: Page<Event> = PageImpl(content, pageable, totalElements)

val page: Page<Event> = PageImpl(content)
val emptyPage: Page<Event> = PageImpl(listOf(), pageable, listOf<Event>().size.toLong())

val name = "코딩"
Expand Down Expand Up @@ -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<Event> = 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)
Expand Down
Loading