Add expense/income filtering to categories

Signed-off-by: William Brawner <me@wbrawner.com>
This commit is contained in:
William Brawner 2019-12-22 17:19:44 -06:00
parent d80ade5ef6
commit 412c4b5edf
2 changed files with 8 additions and 1 deletions

View file

@ -31,6 +31,7 @@ class CategoryController @Autowired constructor(
@GetMapping("", produces = [MediaType.APPLICATION_JSON_VALUE])
@ApiOperation(value = "getCategories", nickname = "getCategories", tags = ["Categories"])
fun getCategories(budgetId: Long? = null,
isExpense: Boolean? = null,
count: Int?,
page: Int?,
sortBy: String?,
@ -46,7 +47,12 @@ class CategoryController @Autowired constructor(
count?: 1000,
Sort(sortOrder?: Sort.Direction.ASC, sortBy?: "title")
)
return ResponseEntity.ok(categoryRepository.findAllByBudgetIn(budgets, pageRequest).map { CategoryResponse(it) })
val categories = if (isExpense == null) {
categoryRepository.findAllByBudgetIn(budgets, pageRequest)
} else {
categoryRepository.findAllByBudgetInAndExpense(budgets, isExpense, pageRequest)
}
return ResponseEntity.ok(categories.map { CategoryResponse(it) })
}
@GetMapping("/{id}", produces = [MediaType.APPLICATION_JSON_VALUE])

View file

@ -8,6 +8,7 @@ import java.util.*
interface CategoryRepository: PagingAndSortingRepository<Category, Long> {
fun findAllByBudget(budget: Budget, pageable: Pageable): List<Category>
fun findAllByBudgetIn(budgets: List<Budget>, pageable: Pageable? = null): List<Category>
fun findAllByBudgetInAndExpense(budgets: List<Budget>, isExpense: Boolean, pageable: Pageable? = null): List<Category>
fun findByBudgetAndId(budget: Budget, id: Long): Optional<Category>
fun findAllByBudgetInAndIdIn(budgets: List<Budget>, ids: List<Long>, pageable: Pageable? = null): List<Category>
}