diff --git a/src/main/kotlin/com/wbrawner/budgetserver/budget/BudgetController.kt b/src/main/kotlin/com/wbrawner/budgetserver/budget/BudgetController.kt index 532bc13..c4bd20d 100644 --- a/src/main/kotlin/com/wbrawner/budgetserver/budget/BudgetController.kt +++ b/src/main/kotlin/com/wbrawner/budgetserver/budget/BudgetController.kt @@ -8,6 +8,8 @@ import io.swagger.annotations.ApiOperation import io.swagger.annotations.Authorization import org.hibernate.Hibernate import org.springframework.beans.factory.annotation.Autowired +import org.springframework.data.domain.PageRequest +import org.springframework.data.domain.Sort import org.springframework.http.MediaType import org.springframework.http.ResponseEntity import org.springframework.web.bind.annotation.* @@ -24,11 +26,14 @@ class BudgetController @Autowired constructor( @Transactional @GetMapping("", produces = [MediaType.APPLICATION_JSON_VALUE]) @ApiOperation(value = "getBudgets", nickname = "getBudgets", tags = ["Budgets"]) - fun getBudgets(): ResponseEntity> = ResponseEntity.ok( - budgetRepository.findAllByUsersContainsOrOwner(getCurrentUser()!!).map { - Hibernate.initialize(it.users) - BudgetResponse(it) - } + fun getBudgets(page: Int?, count: Int?): ResponseEntity> = ResponseEntity.ok( + budgetRepository.findAllByUsersContainsOrOwner( + user = getCurrentUser()!!, + pageable = PageRequest.of(page ?: 0, count ?: 1000, Sort.by("name"))) + .map { + Hibernate.initialize(it.users) + BudgetResponse(it) + } ) @Transactional diff --git a/src/main/kotlin/com/wbrawner/budgetserver/budget/BudgetRepository.kt b/src/main/kotlin/com/wbrawner/budgetserver/budget/BudgetRepository.kt index 783c6fc..1b367c0 100644 --- a/src/main/kotlin/com/wbrawner/budgetserver/budget/BudgetRepository.kt +++ b/src/main/kotlin/com/wbrawner/budgetserver/budget/BudgetRepository.kt @@ -3,12 +3,13 @@ package com.wbrawner.budgetserver.budget import com.wbrawner.budgetserver.category.Category import com.wbrawner.budgetserver.transaction.Transaction import com.wbrawner.budgetserver.user.User +import org.springframework.data.domain.Pageable import org.springframework.data.repository.PagingAndSortingRepository import java.util.* interface BudgetRepository: PagingAndSortingRepository { fun findAllByIdIn(ids: List): List - fun findAllByUsersContainsOrOwner(user: User, owner: User = user): List + fun findAllByUsersContainsOrOwner(user: User, owner: User = user, pageable: Pageable? = null): List fun findByUsersContainsAndId(user: User, id: Long): Optional fun findByUsersContainsAndTransactionsContains(user: User, transaction: Transaction): Optional fun findByUsersContainsAndCategoriesContains(user: User, category: Category): Optional