Sort budgets by name

This commit is contained in:
Billy Brawner 2019-11-06 15:50:40 -06:00
parent 08ecc1ce26
commit 2215694ef6
2 changed files with 12 additions and 6 deletions

View file

@ -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<List<BudgetResponse>> = ResponseEntity.ok(
budgetRepository.findAllByUsersContainsOrOwner(getCurrentUser()!!).map {
Hibernate.initialize(it.users)
BudgetResponse(it)
}
fun getBudgets(page: Int?, count: Int?): ResponseEntity<List<BudgetResponse>> = ResponseEntity.ok(
budgetRepository.findAllByUsersContainsOrOwner(
user = getCurrentUser()!!,
pageable = PageRequest.of(page ?: 0, count ?: 1000, Sort.by("name")))
.map {
Hibernate.initialize(it.users)
BudgetResponse(it)
}
)
@Transactional

View file

@ -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<Budget, Long> {
fun findAllByIdIn(ids: List<Long>): List<Budget>
fun findAllByUsersContainsOrOwner(user: User, owner: User = user): List<Budget>
fun findAllByUsersContainsOrOwner(user: User, owner: User = user, pageable: Pageable? = null): List<Budget>
fun findByUsersContainsAndId(user: User, id: Long): Optional<Budget>
fun findByUsersContainsAndTransactionsContains(user: User, transaction: Transaction): Optional<Budget>
fun findByUsersContainsAndCategoriesContains(user: User, category: Category): Optional<Budget>