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.FinancialService import java.time.LocalDate @RestController @RequestMapping("/budgets") class BudgetController( val financialService: FinancialService ) { private val logger = LoggerFactory.getLogger(BudgetController::class.java) @GetMapping fun getBudgets(): Mono> { return financialService.getBudgets() } @GetMapping("/{id}") fun getBudget(@PathVariable id: String): Mono { return financialService.getBudget(id) } @GetMapping("/by-dates") fun getBudgetByDate(@RequestParam date: LocalDate): ResponseEntity { return ResponseEntity.ok(financialService.getBudgetByDate(date)) } @GetMapping("/{id}/categories") fun getBudgetCategories(@PathVariable id: String): ResponseEntity { return ResponseEntity.ok(financialService.getBudgetCategories(id)) } @GetMapping("/{id}/transactions") fun getBudgetTransactions(@PathVariable id: String):Mono>> { return financialService.getBudgetTransactionsByType(id) } @PostMapping("/") fun createBudget(@RequestBody budgetCreationDTO: BudgetCreationDTO): Mono { return financialService.createBudget( budgetCreationDTO.budget, budgetCreationDTO.createRecurrent ) } @DeleteMapping("/{id}") fun deleteBudget(@PathVariable id: String): Mono { return financialService.deleteBudget(id) } @PostMapping("/{budgetId}/categories/{catId}/limit") fun setCategoryLimit( @PathVariable budgetId: String, @PathVariable catId: String, @RequestBody limit: LimitValue, ): ResponseEntity { return try { ResponseEntity.ok(financialService.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> { return financialService.getWarns(id, hidden) } @PostMapping("/{id}/warns/{warnId}/hide") fun setWarnHide(@PathVariable id: String, @PathVariable warnId: String): Mono { return financialService.hideWarn( warnId) } @GetMapping("/regencats") fun regenCats(): Mono{ return financialService.regenCats() } data class LimitValue( var limit: Double ) }