102 lines
3.2 KiB
Kotlin
102 lines
3.2 KiB
Kotlin
package space.luminic.budgerapp.controllers
|
|
|
|
import org.slf4j.LoggerFactory
|
|
import org.springframework.http.ResponseEntity
|
|
import org.springframework.web.bind.annotation.DeleteMapping
|
|
import org.springframework.web.bind.annotation.GetMapping
|
|
import org.springframework.web.bind.annotation.PathVariable
|
|
import org.springframework.web.bind.annotation.PostMapping
|
|
import org.springframework.web.bind.annotation.RequestBody
|
|
import org.springframework.web.bind.annotation.RequestMapping
|
|
import org.springframework.web.bind.annotation.RequestParam
|
|
import org.springframework.web.bind.annotation.RestController
|
|
import reactor.core.publisher.Mono
|
|
import space.luminic.budgerapp.controllers.dtos.BudgetCreationDTO
|
|
import space.luminic.budgerapp.models.Budget
|
|
import space.luminic.budgerapp.models.BudgetDTO
|
|
import space.luminic.budgerapp.models.Transaction
|
|
import space.luminic.budgerapp.models.Warn
|
|
import space.luminic.budgerapp.services.BudgetService
|
|
import java.time.LocalDate
|
|
|
|
|
|
@RestController
|
|
@RequestMapping("/budgets")
|
|
class BudgetController(
|
|
val budgetService: BudgetService
|
|
) {
|
|
|
|
private val logger = LoggerFactory.getLogger(BudgetController::class.java)
|
|
|
|
@GetMapping
|
|
fun getBudgets(): Mono<MutableList<Budget>> {
|
|
return budgetService.getBudgets()
|
|
}
|
|
|
|
@GetMapping("/{id}")
|
|
fun getBudget(@PathVariable id: String): Mono<BudgetDTO> {
|
|
|
|
return budgetService.getBudget(id)
|
|
}
|
|
|
|
|
|
@GetMapping("/by-dates")
|
|
fun getBudgetByDate(@RequestParam date: LocalDate): ResponseEntity<Any> {
|
|
return ResponseEntity.ok(budgetService.getBudgetByDate(date))
|
|
}
|
|
|
|
|
|
@GetMapping("/{id}/categories")
|
|
fun getBudgetCategories(@PathVariable id: String): ResponseEntity<Any> {
|
|
return ResponseEntity.ok(budgetService.getBudgetCategories(id))
|
|
}
|
|
|
|
@GetMapping("/{id}/transactions")
|
|
fun getBudgetTransactions(@PathVariable id: String):Mono<Map<String,List<Transaction>>> {
|
|
return budgetService.getBudgetTransactionsByType(id)
|
|
}
|
|
|
|
@PostMapping("/")
|
|
fun createBudget(@RequestBody budgetCreationDTO: BudgetCreationDTO): Mono<Budget> {
|
|
return budgetService.createBudget(
|
|
budgetCreationDTO.budget,
|
|
budgetCreationDTO.createRecurrent
|
|
)
|
|
}
|
|
|
|
@DeleteMapping("/{id}")
|
|
fun deleteBudget(@PathVariable id: String): Mono<Void> {
|
|
return budgetService.deleteBudget(id)
|
|
}
|
|
|
|
@PostMapping("/{budgetId}/categories/{catId}/limit")
|
|
fun setCategoryLimit(
|
|
@PathVariable budgetId: String,
|
|
@PathVariable catId: String,
|
|
@RequestBody limit: LimitValue,
|
|
): ResponseEntity<Any> {
|
|
return try {
|
|
ResponseEntity.ok(budgetService.setCategoryLimit(budgetId, catId, limit.limit))
|
|
} catch (e: Exception) {
|
|
ResponseEntity.badRequest().body(e.message)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@GetMapping("/{id}/warns")
|
|
fun budgetWarns(@PathVariable id: String, @RequestParam hidden: Boolean? = null): Mono<List<Warn>> {
|
|
return budgetService.getWarns(id, hidden)
|
|
}
|
|
|
|
@PostMapping("/{id}/warns/{warnId}/hide")
|
|
fun setWarnHide(@PathVariable id: String, @PathVariable warnId: String): Mono<Warn> {
|
|
return budgetService.hideWarn( warnId)
|
|
|
|
|
|
}
|
|
|
|
data class LimitValue(
|
|
var limit: Double
|
|
)
|
|
} |