Use Sets instead of Lists for collections and correct capitalization in naming

This commit is contained in:
Billy Brawner 2019-10-29 12:15:13 -06:00
parent 4755b30a11
commit 6191ed9fe4
3 changed files with 14 additions and 14 deletions

View file

@ -19,9 +19,9 @@ data class Budget(
@ManyToOne val owner: User
)
data class NewBudgetRequest(val name: String, val description: String?, val userIds: List<Long>)
data class NewBudgetRequest(val name: String, val description: String?, val userIds: Set<Long>)
data class UpdateBudgetRequest(val name: String?, val description: String?, val userIds: List<Long>?)
data class UpdateBudgetRequest(val name: String?, val description: String?, val userIds: Set<Long>?)
data class BudgetResponse(val id: Long, val name: String, val description: String?, val users: List<UserResponse>) {
constructor(budget: Budget) : this(budget.id!!, budget.name, budget.description, budget.users.map { UserResponse(it) })

View file

@ -56,30 +56,30 @@ class BudgetController @Autowired constructor(
fun newBudget(@RequestBody request: NewBudgetRequest): ResponseEntity<BudgetResponse> {
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<BudgetResponse> {
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<Unit> {
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()
}
}

View file

@ -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<Transaction> = emptyList(),
@OneToMany(mappedBy = "category") val transactions: Set<Transaction> = emptySet(),
val expense: Boolean? = true
) : Comparable<Category> {
override fun compareTo(other: Category): Int = title.compareTo(other.title)