Fix transaction sum queries
This commit is contained in:
parent
f5c6857d71
commit
89ba569873
4 changed files with 20 additions and 3 deletions
|
@ -48,7 +48,7 @@ class AccountController @Autowired constructor(
|
||||||
accountRepository.findByUsersContainsAndId(getCurrentUser()!!, id)
|
accountRepository.findByUsersContainsAndId(getCurrentUser()!!, id)
|
||||||
.orElse(null)
|
.orElse(null)
|
||||||
?.let {
|
?.let {
|
||||||
ResponseEntity.ok(AccountBalanceResponse(it.id!!, transactionRepository.sumBalanceByAccount(it)))
|
ResponseEntity.ok(AccountBalanceResponse(it.id!!, transactionRepository.sumBalanceByAccountId(it.id)))
|
||||||
} ?: ResponseEntity.notFound().build()
|
} ?: ResponseEntity.notFound().build()
|
||||||
|
|
||||||
@PostMapping("/new", consumes = [MediaType.APPLICATION_JSON_VALUE], produces = [MediaType.APPLICATION_JSON_VALUE])
|
@PostMapping("/new", consumes = [MediaType.APPLICATION_JSON_VALUE], produces = [MediaType.APPLICATION_JSON_VALUE])
|
||||||
|
|
|
@ -36,6 +36,8 @@ data class CategoryResponse(
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
data class CategoryBalanceResponse(val id: Long, val balance: Long)
|
||||||
|
|
||||||
data class NewCategoryRequest(
|
data class NewCategoryRequest(
|
||||||
val title: String,
|
val title: String,
|
||||||
val description: String?,
|
val description: String?,
|
||||||
|
|
|
@ -44,6 +44,15 @@ class CategoryController @Autowired constructor(
|
||||||
return ResponseEntity.ok(CategoryResponse(category))
|
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<CategoryBalanceResponse> {
|
||||||
|
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
|
@Transactional
|
||||||
@PostMapping("/new", consumes = [MediaType.APPLICATION_JSON_VALUE], produces = [MediaType.APPLICATION_JSON_VALUE])
|
@PostMapping("/new", consumes = [MediaType.APPLICATION_JSON_VALUE], produces = [MediaType.APPLICATION_JSON_VALUE])
|
||||||
@ApiOperation(value = "newCategory", nickname = "newCategory", tags = ["Categories"])
|
@ApiOperation(value = "newCategory", nickname = "newCategory", tags = ["Categories"])
|
||||||
|
|
|
@ -13,7 +13,13 @@ interface TransactionRepository: PagingAndSortingRepository<Transaction, Long> {
|
||||||
fun findAllByAccountAndCategory(account: Account, category: Category): List<Transaction>
|
fun findAllByAccountAndCategory(account: Account, category: Category): List<Transaction>
|
||||||
@Query(
|
@Query(
|
||||||
nativeQuery = true,
|
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
|
||||||
}
|
}
|
Loading…
Reference in a new issue