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