diff --git a/src/main/kotlin/com/wbrawner/budgetserver/budget/Budget.kt b/src/main/kotlin/com/wbrawner/budgetserver/budget/Budget.kt index cbf0ea6..5001d40 100644 --- a/src/main/kotlin/com/wbrawner/budgetserver/budget/Budget.kt +++ b/src/main/kotlin/com/wbrawner/budgetserver/budget/Budget.kt @@ -19,9 +19,9 @@ data class Budget( @ManyToOne val owner: User ) -data class NewBudgetRequest(val name: String, val description: String?, val userIds: List) +data class NewBudgetRequest(val name: String, val description: String?, val userIds: Set) -data class UpdateBudgetRequest(val name: String?, val description: String?, val userIds: List?) +data class UpdateBudgetRequest(val name: String?, val description: String?, val userIds: Set?) data class BudgetResponse(val id: Long, val name: String, val description: String?, val users: List) { constructor(budget: Budget) : this(budget.id!!, budget.name, budget.description, budget.users.map { UserResponse(it) }) diff --git a/src/main/kotlin/com/wbrawner/budgetserver/budget/BudgetController.kt b/src/main/kotlin/com/wbrawner/budgetserver/budget/BudgetController.kt index 47f77fa..532bc13 100644 --- a/src/main/kotlin/com/wbrawner/budgetserver/budget/BudgetController.kt +++ b/src/main/kotlin/com/wbrawner/budgetserver/budget/BudgetController.kt @@ -56,30 +56,30 @@ class BudgetController @Autowired constructor( fun newBudget(@RequestBody request: NewBudgetRequest): ResponseEntity { val users = request.userIds .map { id -> userRepository.findById(id).orElse(null) } - .filter { user -> user != null } + .filterNotNull() .toMutableSet() - .apply { this.add(getCurrentUser()) } - val Budget = budgetRepository.save(Budget(name = request.name, description = request.description, users = users, owner = getCurrentUser()!!)) - return ResponseEntity.ok(BudgetResponse(Budget)) + .apply { this.add(getCurrentUser()!!) } + val budget = budgetRepository.save(Budget(name = request.name, description = request.description, users = users, owner = getCurrentUser()!!)) + return ResponseEntity.ok(BudgetResponse(budget)) } @PutMapping("/{id}", consumes = [MediaType.APPLICATION_JSON_VALUE], produces = [MediaType.APPLICATION_JSON_VALUE]) @ApiOperation(value = "updateBudget", nickname = "updateBudget", tags = ["Budgets"]) fun updateBudget(@PathVariable id: Long, request: UpdateBudgetRequest): ResponseEntity { - var Budget = budgetRepository.findByUsersContainsAndId(getCurrentUser()!!, id).orElse(null) + var budget = budgetRepository.findByUsersContainsAndId(getCurrentUser()!!, id).orElse(null) ?: return ResponseEntity.notFound().build() - if (request.name != null) Budget = Budget.copy(name = request.name) - if (request.description != null) Budget = Budget.copy(description = request.description) - if (request.userIds != null) Budget = Budget.copy(users = userRepository.findAllById(request.userIds).toSet()) - return ResponseEntity.ok(BudgetResponse(budgetRepository.save(Budget))) + if (request.name != null) budget = budget.copy(name = request.name) + if (request.description != null) budget = budget.copy(description = request.description) + if (request.userIds != null) budget = budget.copy(users = userRepository.findAllById(request.userIds).toSet()) + return ResponseEntity.ok(BudgetResponse(budgetRepository.save(budget))) } @DeleteMapping("/{id}", produces = [MediaType.TEXT_PLAIN_VALUE]) @ApiOperation(value = "deleteBudget", nickname = "deleteBudget", tags = ["Budgets"]) fun deleteBudget(@PathVariable id: Long): ResponseEntity { - val Budget = budgetRepository.findByUsersContainsAndId(getCurrentUser()!!, id).orElse(null) + val budget = budgetRepository.findByUsersContainsAndId(getCurrentUser()!!, id).orElse(null) ?: return ResponseEntity.notFound().build() - budgetRepository.delete(Budget) + budgetRepository.delete(budget) return ResponseEntity.ok().build() } } \ No newline at end of file diff --git a/src/main/kotlin/com/wbrawner/budgetserver/category/Category.kt b/src/main/kotlin/com/wbrawner/budgetserver/category/Category.kt index dd642e4..113f2fc 100644 --- a/src/main/kotlin/com/wbrawner/budgetserver/category/Category.kt +++ b/src/main/kotlin/com/wbrawner/budgetserver/category/Category.kt @@ -12,7 +12,7 @@ data class Category( val description: String? = null, val amount: Long = 0, @ManyToOne val budget: Budget, - @OneToMany(mappedBy = "category") val transactions: List = emptyList(), + @OneToMany(mappedBy = "category") val transactions: Set = emptySet(), val expense: Boolean? = true ) : Comparable { override fun compareTo(other: Category): Int = title.compareTo(other.title)