From 89ba5698738ea3ec444818f30314c7943f160e1c Mon Sep 17 00:00:00 2001 From: Billy Brawner Date: Sun, 2 Jun 2019 07:07:32 -0700 Subject: [PATCH] Fix transaction sum queries --- .../wbrawner/budgetserver/account/AccountController.kt | 2 +- .../com/wbrawner/budgetserver/category/Category.kt | 2 ++ .../budgetserver/category/CategoryController.kt | 9 +++++++++ .../budgetserver/transaction/TransactionRepository.kt | 10 ++++++++-- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/com/wbrawner/budgetserver/account/AccountController.kt b/src/main/kotlin/com/wbrawner/budgetserver/account/AccountController.kt index 98ce58d..3f48f5c 100644 --- a/src/main/kotlin/com/wbrawner/budgetserver/account/AccountController.kt +++ b/src/main/kotlin/com/wbrawner/budgetserver/account/AccountController.kt @@ -48,7 +48,7 @@ class AccountController @Autowired constructor( accountRepository.findByUsersContainsAndId(getCurrentUser()!!, id) .orElse(null) ?.let { - ResponseEntity.ok(AccountBalanceResponse(it.id!!, transactionRepository.sumBalanceByAccount(it))) + ResponseEntity.ok(AccountBalanceResponse(it.id!!, transactionRepository.sumBalanceByAccountId(it.id))) } ?: ResponseEntity.notFound().build() @PostMapping("/new", consumes = [MediaType.APPLICATION_JSON_VALUE], produces = [MediaType.APPLICATION_JSON_VALUE]) diff --git a/src/main/kotlin/com/wbrawner/budgetserver/category/Category.kt b/src/main/kotlin/com/wbrawner/budgetserver/category/Category.kt index 2cad0c5..9c1e31f 100644 --- a/src/main/kotlin/com/wbrawner/budgetserver/category/Category.kt +++ b/src/main/kotlin/com/wbrawner/budgetserver/category/Category.kt @@ -36,6 +36,8 @@ data class CategoryResponse( ) } +data class CategoryBalanceResponse(val id: Long, val balance: Long) + data class NewCategoryRequest( val title: String, val description: String?, diff --git a/src/main/kotlin/com/wbrawner/budgetserver/category/CategoryController.kt b/src/main/kotlin/com/wbrawner/budgetserver/category/CategoryController.kt index 234db9e..0d14e2f 100644 --- a/src/main/kotlin/com/wbrawner/budgetserver/category/CategoryController.kt +++ b/src/main/kotlin/com/wbrawner/budgetserver/category/CategoryController.kt @@ -44,6 +44,15 @@ class CategoryController @Autowired constructor( return ResponseEntity.ok(CategoryResponse(category)) } + @GetMapping("/{id}/balance", produces = [MediaType.APPLICATION_JSON_VALUE]) + @ApiOperation(value = "getCategoryBalance", nickname = "getCategoryBalance", tags = ["Categories"]) + fun getCategoryBalance(@PathVariable id: Long): ResponseEntity { + val category = categoryRepository.findById(id).orElse(null) ?: return ResponseEntity.notFound().build() + accountRepository.findByUsersContainsAndCategoriesContains(getCurrentUser()!!, category).orElse(null) + ?: return ResponseEntity.notFound().build() + return ResponseEntity.ok(CategoryBalanceResponse(category.id!!, transactionRepository.sumBalanceByCategoryId(category.id))) + } + @Transactional @PostMapping("/new", consumes = [MediaType.APPLICATION_JSON_VALUE], produces = [MediaType.APPLICATION_JSON_VALUE]) @ApiOperation(value = "newCategory", nickname = "newCategory", tags = ["Categories"]) diff --git a/src/main/kotlin/com/wbrawner/budgetserver/transaction/TransactionRepository.kt b/src/main/kotlin/com/wbrawner/budgetserver/transaction/TransactionRepository.kt index b808cae..a34bbf1 100644 --- a/src/main/kotlin/com/wbrawner/budgetserver/transaction/TransactionRepository.kt +++ b/src/main/kotlin/com/wbrawner/budgetserver/transaction/TransactionRepository.kt @@ -13,7 +13,13 @@ interface TransactionRepository: PagingAndSortingRepository { fun findAllByAccountAndCategory(account: Account, category: Category): List @Query( nativeQuery = true, - value = "SELECT (COALESCE((SELECT SUM(amount) from transaction WHERE expense = 0), 0)) - (COALESCE((SELECT SUM(amount) from transaction WHERE expense = 1), 0));" + value = "SELECT (COALESCE((SELECT SUM(amount) from transaction WHERE account_id = :accountId AND expense = 0), 0)) - (COALESCE((SELECT SUM(amount) from transaction WHERE account_id = :accountId AND expense = 1), 0));" ) - fun sumBalanceByAccount(account: Account): Long + fun sumBalanceByAccountId(accountId: Long): Long + + @Query( + nativeQuery = true, + value = "SELECT (COALESCE((SELECT SUM(amount) from transaction WHERE category_id = :categoryId AND expense = 0), 0)) - (COALESCE((SELECT SUM(amount) from transaction WHERE category_id = :categoryId AND expense = 1), 0));" + ) + fun sumBalanceByCategoryId(categoryId: Long): Long } \ No newline at end of file