suspend coroutines

This commit is contained in:
xds
2025-02-28 01:17:52 +03:00
parent 35090b946d
commit db0ada5ee8
13 changed files with 1099 additions and 1184 deletions

View File

@@ -1,12 +1,11 @@
package space.luminic.budgerapp.controllers
import kotlinx.coroutines.reactor.awaitSingle
import kotlinx.coroutines.reactor.awaitSingleOrNull
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.BudgetController.LimitValue
import space.luminic.budgerapp.controllers.dtos.BudgetCreationDTO
import space.luminic.budgerapp.models.*
@@ -33,13 +32,13 @@ class SpaceController(
)
@GetMapping
fun getSpaces(): Mono<List<Space>> {
suspend fun getSpaces(): List<Space> {
return spaceService.getSpaces()
}
@PostMapping
fun createSpace(@RequestBody space: SpaceCreateDTO): Mono<Space> {
suspend fun createSpace(@RequestBody space: SpaceCreateDTO): Space {
return spaceService.createSpace(
Space(name = space.name, description = space.description),
space.createCategories
@@ -48,87 +47,85 @@ class SpaceController(
@GetMapping("{spaceId}")
fun getSpace(@PathVariable spaceId: String): Mono<Space> {
suspend fun getSpace(@PathVariable spaceId: String): Space {
return spaceService.getSpace(spaceId)
}
@DeleteMapping("/{spaceId}")
fun deleteSpace(@PathVariable spaceId: String): Mono<Void> {
return spaceService.isValidRequest(spaceId).flatMap {
spaceService.deleteSpace(it)
}
suspend fun deleteSpace(@PathVariable spaceId: String) {
return spaceService.deleteSpace(spaceService.isValidRequest(spaceId))
}
@PostMapping("/{spaceId}/invite")
fun inviteSpace(@PathVariable spaceId: String): Mono<SpaceInvite> {
suspend fun inviteSpace(@PathVariable spaceId: String): SpaceInvite {
spaceService.isValidRequest(spaceId)
return spaceService.createInviteSpace(spaceId)
}
@PostMapping("/invite/{code}")
fun acceptInvite(@PathVariable code: String): Mono<Space> {
suspend fun acceptInvite(@PathVariable code: String): Space {
return spaceService.acceptInvite(code)
}
@DeleteMapping("/{spaceId}/leave")
fun leaveSpace(@PathVariable spaceId: String): Mono<Void> {
suspend fun leaveSpace(@PathVariable spaceId: String) {
spaceService.isValidRequest(spaceId)
return spaceService.leaveSpace(spaceId)
}
@DeleteMapping("/{spaceId}/members/kick/{username}")
fun kickMembers(@PathVariable spaceId: String, @PathVariable username: String): Mono<Void> {
suspend fun kickMembers(@PathVariable spaceId: String, @PathVariable username: String) {
spaceService.isValidRequest(spaceId)
return spaceService.kickMember(spaceId, username)
}
//
//Budgets API
//
//Budgets API
//
@GetMapping("/{spaceId}/budgets")
fun getBudgets(@PathVariable spaceId: String): Mono<List<Budget>> {
return spaceService.isValidRequest(spaceId).flatMap {
financialService.getBudgets(spaceId)
}
suspend fun getBudgets(@PathVariable spaceId: String): List<Budget> {
spaceService.isValidRequest(spaceId)
return financialService.getBudgets(spaceId).awaitSingleOrNull().orEmpty()
}
@GetMapping("/{spaceId}/budgets/{id}")
fun getBudget(@PathVariable spaceId: String, @PathVariable id: String): Mono<BudgetDTO> {
return spaceService.isValidRequest(spaceId).flatMap {
financialService.getBudget(spaceId, id)
}
suspend fun getBudget(@PathVariable spaceId: String, @PathVariable id: String): BudgetDTO? {
spaceService.isValidRequest(spaceId)
return financialService.getBudget(spaceId, id)
}
@PostMapping("/{spaceId}/budgets")
fun createBudget(
suspend fun createBudget(
@PathVariable spaceId: String,
@RequestBody budgetCreationDTO: BudgetCreationDTO,
): Mono<Budget> {
return spaceService.isValidRequest(spaceId).flatMap {
financialService.createBudget(it, budgetCreationDTO.budget, budgetCreationDTO.createRecurrent)
}
): Budget? {
return financialService.createBudget(
spaceService.isValidRequest(spaceId),
budgetCreationDTO.budget,
budgetCreationDTO.createRecurrent
)
}
@DeleteMapping("/{spaceId}/budgets/{id}")
fun deleteBudget(@PathVariable spaceId: String, @PathVariable id: String): Mono<Void> {
return spaceService.isValidRequest(spaceId).flatMap {
financialService.deleteBudget(spaceId, id)
}
suspend fun deleteBudget(@PathVariable spaceId: String, @PathVariable id: String) {
spaceService.isValidRequest(spaceId)
financialService.deleteBudget(spaceId, id)
}
@PostMapping("/{spaceId}/budgets/{budgetId}/categories/{catId}/limit")
fun setCategoryLimit(
suspend fun setCategoryLimit(
@PathVariable spaceId: String,
@PathVariable budgetId: String,
@PathVariable catId: String,
@RequestBody limit: LimitValue,
): Mono<BudgetCategory> {
return spaceService.isValidRequest(spaceId).flatMap {
financialService.setCategoryLimit(it.id!!, budgetId, catId, limit.limit)
}
): BudgetCategory {
spaceService.isValidRequest(spaceId)
return financialService.setCategoryLimit(spaceId, budgetId, catId, limit.limit)
}
//
@@ -164,172 +161,145 @@ class SpaceController(
@GetMapping("/{spaceId}/transactions/{id}")
fun getTransaction(
suspend 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)
}
): Transaction {
return financialService.getTransactionById(id)
}
@PostMapping("/{spaceId}/transactions")
fun createTransaction(@PathVariable spaceId: String, @RequestBody transaction: Transaction): Mono<Transaction> {
return spaceService.isValidRequest(spaceId).flatMap {
financialService.createTransaction(it, transaction)
}
suspend fun createTransaction(@PathVariable spaceId: String, @RequestBody transaction: Transaction): Transaction {
val space = spaceService.isValidRequest(spaceId)
return financialService.createTransaction(space, transaction)
}
@PutMapping("/{spaceId}/transactions/{id}")
fun editTransaction(
suspend fun editTransaction(
@PathVariable spaceId: String, @PathVariable id: String, @RequestBody transaction: Transaction
): Mono<Transaction> {
return spaceService.isValidRequest(spaceId).flatMap {
transaction.space = it
financialService.editTransaction(transaction)
}
): Transaction {
val space = spaceService.isValidRequest(spaceId)
transaction.space = space
return financialService.editTransaction(transaction)
}
@DeleteMapping("/{spaceId}/transactions/{id}")
fun deleteTransaction(@PathVariable spaceId: String, @PathVariable id: String): Mono<Void> {
return spaceService.isValidRequest(spaceId).flatMap {
financialService.getTransactionById(id).flatMap { financialService.deleteTransaction(it) }
}
suspend fun deleteTransaction(@PathVariable spaceId: String, @PathVariable id: String) {
spaceService.isValidRequest(spaceId)
val transaction = financialService.getTransactionById(id)
financialService.deleteTransaction(transaction)
}
//
// Categories API
//
// Categories API
//
@GetMapping("/{spaceId}/categories")
fun getCategories(
suspend 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)
}
): List<Category> {
spaceService.isValidRequest(spaceId)
return categoryService.getCategories(spaceId, type, sortBy, direction).awaitSingleOrNull().orEmpty()
}
@GetMapping("/{spaceId}/categories/types")
fun getCategoriesTypes(@PathVariable spaceId: String): ResponseEntity<Any> {
return try {
ResponseEntity.ok(categoryService.getCategoryTypes())
} catch (e: Exception) {
ResponseEntity(HttpClientErrorException(HttpStatus.INTERNAL_SERVER_ERROR), HttpStatus.INTERNAL_SERVER_ERROR)
}
fun getCategoriesTypes(@PathVariable spaceId: String): List<CategoryType> {
return categoryService.getCategoryTypes()
}
@PostMapping("/{spaceId}/categories")
fun createCategory(
suspend fun createCategory(
@PathVariable spaceId: String, @RequestBody category: Category
): Mono<Category> {
return spaceService.isValidRequest(spaceId).flatMap {
financialService.createCategory(it, category)
}
): Category {
val space = spaceService.isValidRequest(spaceId)
return financialService.createCategory(space, category).awaitSingle()
}
@PutMapping("/{spaceId}/categories/{categoryId}")
fun editCategory(
suspend fun editCategory(
@PathVariable categoryId: String,
@RequestBody category: Category,
@PathVariable spaceId: String
): Mono<Category> {
return spaceService.isValidRequest(spaceId).flatMap {
categoryService.editCategory(it, category)
}
): Category {
val space = spaceService.isValidRequest(spaceId)
return categoryService.editCategory(space, category)
}
@DeleteMapping("/{spaceId}/categories/{categoryId}")
fun deleteCategory(@PathVariable categoryId: String, @PathVariable spaceId: String): Mono<String> {
return spaceService.isValidRequest(spaceId).flatMap {
categoryService.deleteCategory(it, categoryId)
}
suspend fun deleteCategory(@PathVariable categoryId: String, @PathVariable spaceId: String) {
val space = spaceService.isValidRequest(spaceId)
categoryService.deleteCategory(space, categoryId)
}
@GetMapping("/{spaceId}/categories/tags")
fun getTags(@PathVariable spaceId: String): Mono<List<Tag>> {
return spaceService.isValidRequest(spaceId).flatMap {
spaceService.getTags(it)
}
suspend fun getTags(@PathVariable spaceId: String): List<Tag> {
val space = spaceService.isValidRequest(spaceId)
return spaceService.getTags(space)
}
@PostMapping("/{spaceId}/categories/tags")
fun createTags(@PathVariable spaceId: String, @RequestBody tag: Tag): Mono<Tag> {
return spaceService.isValidRequest(spaceId).flatMap {
spaceService.createTag(it, tag)
}
suspend fun createTags(@PathVariable spaceId: String, @RequestBody tag: Tag): Tag {
val space = spaceService.isValidRequest(spaceId)
return spaceService.createTag(space, tag)
}
@DeleteMapping("/{spaceId}/categories/tags/{tagId}")
fun deleteTags(@PathVariable spaceId: String, @PathVariable tagId: String): Mono<Void> {
return spaceService.isValidRequest(spaceId).flatMap {
spaceService.deleteTag(it, tagId)
}
suspend fun deleteTags(@PathVariable spaceId: String, @PathVariable tagId: String) {
val space = spaceService.isValidRequest(spaceId)
return spaceService.deleteTag(space, tagId)
}
@GetMapping("/{spaceId}/analytics/by-month")
fun getCategoriesSumsByMonthsV2(@PathVariable spaceId: String): Mono<List<Document>> {
suspend fun getCategoriesSumsByMonthsV2(@PathVariable spaceId: String): List<Document> {
return financialService.getCategorySummaries(spaceId, LocalDate.now().minusMonths(6))
}
//
// Recurrents API
//
//
// Recurrents API
//
@GetMapping("/{spaceId}/recurrents")
fun getRecurrents(@PathVariable spaceId: String): Mono<List<Recurrent>> {
return spaceService.isValidRequest(spaceId).flatMap {
recurrentService.getRecurrents(it.id!!)
}
suspend fun getRecurrents(@PathVariable spaceId: String): List<Recurrent> {
spaceService.isValidRequest(spaceId)
return recurrentService.getRecurrents(spaceId).awaitSingleOrNull().orEmpty()
}
@GetMapping("/{spaceId}/recurrents/{id}")
fun getRecurrent(@PathVariable spaceId: String, @PathVariable id: String): Mono<Recurrent> {
return spaceService.isValidRequest(spaceId).flatMap {
recurrentService.getRecurrentById(it, id)
}
suspend fun getRecurrent(@PathVariable spaceId: String, @PathVariable id: String): Recurrent {
val space = spaceService.isValidRequest(spaceId)
return recurrentService.getRecurrentById(space, id).awaitSingle()
}
@PostMapping("/{spaceId}/recurrent")
fun createRecurrent(@PathVariable spaceId: String, @RequestBody recurrent: Recurrent): Mono<Recurrent> {
return spaceService.isValidRequest(spaceId).flatMap {
recurrentService.createRecurrent(it, recurrent)
}
suspend fun createRecurrent(@PathVariable spaceId: String, @RequestBody recurrent: Recurrent): Recurrent {
val space = spaceService.isValidRequest(spaceId)
return recurrentService.createRecurrent(space, recurrent).awaitSingle()
}
@PutMapping("/{spaceId}/recurrent/{id}")
fun editRecurrent(
suspend fun editRecurrent(
@PathVariable spaceId: String,
@PathVariable id: String,
@RequestBody recurrent: Recurrent
): Mono<Recurrent> {
return spaceService.isValidRequest(spaceId).flatMap {
recurrentService.editRecurrent(recurrent)
}
): Recurrent {
spaceService.isValidRequest(spaceId)
return recurrentService.editRecurrent(recurrent).awaitSingle()
}
@DeleteMapping("/{spaceId}/recurrent/{id}")
fun deleteRecurrent(@PathVariable spaceId: String, @PathVariable id: String): Mono<Void> {
return spaceService.isValidRequest(spaceId).flatMap {
recurrentService.deleteRecurrent(id)
}
suspend fun deleteRecurrent(@PathVariable spaceId: String, @PathVariable id: String) {
spaceService.isValidRequest(spaceId)
recurrentService.deleteRecurrent(id).awaitSingle()
}
// @GetMapping("/regen")