add spaces

This commit is contained in:
xds
2025-02-17 17:58:07 +03:00
parent a5b334f6c2
commit d680345a9f
23 changed files with 1097 additions and 285 deletions

View File

@@ -0,0 +1,265 @@
package space.luminic.budgerapp.controllers
import org.bson.Document
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
import org.springframework.web.client.HttpClientErrorException
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
import space.luminic.budgerapp.controllers.dtos.BudgetCreationDTO
import space.luminic.budgerapp.models.*
import space.luminic.budgerapp.services.CategoryService
import space.luminic.budgerapp.services.FinancialService
import space.luminic.budgerapp.services.RecurrentService
import space.luminic.budgerapp.services.SpaceService
import java.time.LocalDate
@RestController
@RequestMapping("/spaces")
class SpaceController(
private val spaceService: SpaceService,
private val financialService: FinancialService,
private val categoryService: CategoryService,
private val recurrentService: RecurrentService
) {
@GetMapping
fun getSpaces(): Mono<List<Space>> {
return spaceService.getSpaces()
}
@PostMapping
fun createSpace(@RequestBody space: Space): Mono<Space> {
return spaceService.createSpace(space)
}
@GetMapping("{spaceId}")
fun getSpace(@PathVariable spaceId: String): Mono<Space> {
return spaceService.getSpace(spaceId)
}
@DeleteMapping("/{spaceId}")
fun deleteSpace(@PathVariable spaceId: String): Mono<Void> {
return spaceService.deleteSpace(spaceId)
}
@PostMapping("/{spaceId}/invite")
fun inviteSpace(@PathVariable spaceId: String): Mono<SpaceInvite> {
return spaceService.createInviteSpace(spaceId)
}
@PostMapping("/invite/{code}")
fun acceptInvite(@PathVariable code: String): Mono<Space> {
return spaceService.acceptInvite(code)
}
@DeleteMapping("/{spaceId}/leave")
fun leaveSpace(@PathVariable spaceId: String): Mono<Void> {
return spaceService.leaveSpace(spaceId)
}
@DeleteMapping("/{spaceId}/members/kick/{username}")
fun kickMembers(@PathVariable spaceId: String, @PathVariable username: String): Mono<Void> {
return spaceService.kickMember(spaceId, username)
}
//
//Budgets API
//
@GetMapping("{spaceId}/budgets")
fun getBudgets(@PathVariable spaceId: String): Mono<List<Budget>> {
return financialService.getBudgets(spaceId)
}
@PostMapping("/{spaceId}/budgets")
fun createBudget(
@PathVariable spaceId: String,
@RequestBody budgetCreationDTO: BudgetCreationDTO,
): Mono<Budget> {
return financialService.createBudget(spaceId, budgetCreationDTO.budget, budgetCreationDTO.createRecurrent)
}
// Transactions API
@GetMapping("/{spaceId}/transactions")
fun getTransactions(
@PathVariable spaceId: String,
@RequestParam(value = "transaction_type") transactionType: String? = null,
@RequestParam(value = "category_type") categoryType: String? = null,
@RequestParam(value = "user_id") userId: String? = null,
@RequestParam(value = "is_child") isChild: Boolean? = null,
@RequestParam(value = "limit") limit: Int = 10,
@RequestParam(value = "offset") offset: Int = 0
): ResponseEntity<Any> {
try {
return ResponseEntity.ok(
financialService.getTransactions(
spaceId = spaceId,
transactionType = transactionType,
categoryType = categoryType,
userId = userId,
isChild = isChild,
limit = limit,
offset = offset
)
)
} catch (e: Exception) {
e.printStackTrace()
return ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR)
}
}
@GetMapping("/{spaceId}/transactions/{id}")
fun getTransaction(
@PathVariable spaceId: String,
@PathVariable id: String
): ResponseEntity<Any> {
try {
return ResponseEntity.ok(financialService.getTransactionById(id))
} catch (e: Exception) {
e.printStackTrace()
return ResponseEntity(e.message, HttpStatus.INTERNAL_SERVER_ERROR)
}
}
@PostMapping("/{spaceId}/transactions")
fun createTransaction(@PathVariable spaceId: String, @RequestBody transaction: Transaction): ResponseEntity<Any> {
try {
return ResponseEntity.ok(financialService.createTransaction(spaceId, transaction))
} catch (e: Exception) {
e.printStackTrace()
return ResponseEntity(e.message, HttpStatus.INTERNAL_SERVER_ERROR)
}
}
@PutMapping("/{spaceId}/transactions/{id}")
fun editTransaction(
@PathVariable spaceId: String, @PathVariable id: String, @RequestBody transaction: Transaction
): ResponseEntity<Any> {
try {
return ResponseEntity.ok(financialService.editTransaction(transaction))
} catch (e: Exception) {
e.printStackTrace()
return ResponseEntity(e.message, HttpStatus.INTERNAL_SERVER_ERROR)
}
}
@DeleteMapping("/{spaceId}/transactions/{id}")
fun deleteTransaction(@PathVariable spaceId: String, @PathVariable id: String): Mono<Void> {
return financialService.deleteTransaction(id)
}
//
// Categories API
//
@GetMapping("/{spaceId}/categories")
fun getCategories(
@PathVariable spaceId: String,
@RequestParam("type") type: String? = null,
@RequestParam("sort") sortBy: String = "name",
@RequestParam("direction") direction: String = "ASC"
): Mono<List<Category>> {
return spaceService.isValidRequest(spaceId).flatMap {
categoryService.getCategories(spaceId, type, sortBy, direction)
}
}
@GetMapping("/{spaceId}/categories/types")
fun getCategoriesTypes(): ResponseEntity<Any> {
return try {
ResponseEntity.ok(categoryService.getCategoryTypes())
} catch (e: Exception) {
ResponseEntity(HttpClientErrorException(HttpStatus.INTERNAL_SERVER_ERROR), HttpStatus.INTERNAL_SERVER_ERROR)
}
}
@PostMapping("/{spaceId}/categories")
fun createCategory(
@PathVariable spaceId: String, @RequestBody category: Category
): Mono<Category> {
return spaceService.isValidRequest(spaceId).flatMap {
categoryService.createCategory(it,category)
}
}
@PutMapping("/{spaceId}/{categoryId}")
fun editCategory(@PathVariable categoryId: String, @RequestBody category: Category): Mono<Category> {
return categoryService.editCategory(category)
}
@GetMapping("/by-month")
fun getCategoriesSumsByMonths(): Mono<List<Document>> {
return financialService.getCategorySumsPipeline(LocalDate.of(2024, 8, 1), LocalDate.of(2025, 1, 12))
}
@GetMapping("/{spaceId}/analytics/by-month")
fun getCategoriesSumsByMonthsV2(@PathVariable spaceId: String): Mono<List<Document>> {
return financialService.getCategorySummaries(spaceId, LocalDate.now().minusMonths(6))
}
//
// Recurrents API
//
@GetMapping("/{spaceId}/recurrents")
fun getRecurrents(@PathVariable spaceId: String): Mono<List<Recurrent>> {
return spaceService.isValidRequest(spaceId).flatMap {
recurrentService.getRecurrents(it)
}
}
@GetMapping("/{spaceId}/recurrents/{id}")
fun getRecurrent(@PathVariable spaceId: String, @PathVariable id: String): Mono<Recurrent> {
return spaceService.isValidRequest(spaceId).flatMap {
recurrentService.getRecurrentById(it, id)
}
}
@PostMapping("/{spaceId}/recurrent")
fun createRecurrent(@PathVariable spaceId: String, @RequestBody recurrent: Recurrent): Mono<Recurrent> {
return spaceService.isValidRequest(spaceId).flatMap {
recurrentService.createRecurrent(it, recurrent)
}
}
@PutMapping("/{spaceId}/recurrent/{id}")
fun editRecurrent(
@PathVariable spaceId: String,
@PathVariable id: String,
@RequestBody recurrent: Recurrent
): Mono<Recurrent> {
return spaceService.isValidRequest(spaceId).flatMap {
recurrentService.editRecurrent(recurrent)
}
}
@DeleteMapping("/{spaceId}/recurrent/{id}")
fun deleteRecurrent(@PathVariable spaceId: String, @PathVariable id: String): Mono<Void> {
return spaceService.isValidRequest(spaceId).flatMap {
recurrentService.deleteRecurrent(id)
}
}
// @GetMapping("/regen")
// fun regenSpaces(): Mono<List<Space>> {
// return spaceService.regenSpaces()
// }
}