diff --git a/src/main/kotlin/space/luminic/budgerapp/controllers/BudgetController.kt b/src/main/kotlin/space/luminic/budgerapp/controllers/BudgetController.kt index 1691c35..82a8c7d 100644 --- a/src/main/kotlin/space/luminic/budgerapp/controllers/BudgetController.kt +++ b/src/main/kotlin/space/luminic/budgerapp/controllers/BudgetController.kt @@ -92,12 +92,12 @@ class BudgetController( fun setWarnHide(@PathVariable id: String, @PathVariable warnId: String): Mono { return financialService.hideWarn(warnId) } -// -// @GetMapping("/regencats") -// fun regenCats(): Mono { -// return financialService.regenCats() -// } -// + + @GetMapping("/regencats") + fun regenCats(): Mono { + return financialService.regenCats() + } + // @GetMapping("/regenSpaces") // fun regenSpaces(): Mono { // return financialService.regenBudgets() diff --git a/src/main/kotlin/space/luminic/budgerapp/controllers/SpaceController.kt b/src/main/kotlin/space/luminic/budgerapp/controllers/SpaceController.kt index bc03c90..79bcd38 100644 --- a/src/main/kotlin/space/luminic/budgerapp/controllers/SpaceController.kt +++ b/src/main/kotlin/space/luminic/budgerapp/controllers/SpaceController.kt @@ -25,6 +25,12 @@ class SpaceController( ) { + data class SpaceCreateDTO( + val name: String, + val description: String, + val createCategories: Boolean, + ) + @GetMapping fun getSpaces(): Mono> { return spaceService.getSpaces() @@ -32,8 +38,8 @@ class SpaceController( @PostMapping - fun createSpace(@RequestBody space: Space): Mono { - return spaceService.createSpace(space) + fun createSpace(@RequestBody space: SpaceCreateDTO): Mono { + return spaceService.createSpace(Space(name=space.name, description = space.description), space.createCategories) } @@ -45,7 +51,10 @@ class SpaceController( @DeleteMapping("/{spaceId}") fun deleteSpace(@PathVariable spaceId: String): Mono { - return spaceService.deleteSpace(spaceId) + return spaceService.isValidRequest(spaceId).flatMap { + spaceService.deleteSpace(it) + } + } @PostMapping("/{spaceId}/invite") diff --git a/src/main/kotlin/space/luminic/budgerapp/repos/BudgetRepo.kt b/src/main/kotlin/space/luminic/budgerapp/repos/BudgetRepo.kt index ba9d93b..b641359 100644 --- a/src/main/kotlin/space/luminic/budgerapp/repos/BudgetRepo.kt +++ b/src/main/kotlin/space/luminic/budgerapp/repos/BudgetRepo.kt @@ -25,4 +25,7 @@ interface BudgetRepo: ReactiveMongoRepository { @Query("{ 'space': { '\$ref': 'spaces','\$id': ?0 } }") fun findBySpaceId(spaceId: ObjectId, sort: Sort): Flux + + @Query("{ 'space': { '\$ref': 'spaces','\$id': ?0 } }") + fun findBySpaceId(spaceId: ObjectId): Flux } \ No newline at end of file diff --git a/src/main/kotlin/space/luminic/budgerapp/repos/CategoryRepo.kt b/src/main/kotlin/space/luminic/budgerapp/repos/CategoryRepo.kt index e746028..52148fe 100644 --- a/src/main/kotlin/space/luminic/budgerapp/repos/CategoryRepo.kt +++ b/src/main/kotlin/space/luminic/budgerapp/repos/CategoryRepo.kt @@ -1,8 +1,13 @@ package space.luminic.budgerapp.repos +import org.bson.types.ObjectId +import org.springframework.data.domain.Sort +import org.springframework.data.mongodb.repository.Query import org.springframework.data.mongodb.repository.ReactiveMongoRepository import org.springframework.stereotype.Repository +import reactor.core.publisher.Flux import reactor.core.publisher.Mono + import space.luminic.budgerapp.models.Category @Repository @@ -10,7 +15,8 @@ interface CategoryRepo : ReactiveMongoRepository { fun findByName(name: String): Mono - + @Query("{ 'space': { '\$ref': 'spaces','\$id': ?0 } }") + fun findBySpaceId(spaceId: ObjectId): Flux } \ No newline at end of file diff --git a/src/main/kotlin/space/luminic/budgerapp/repos/TransactionRepo.kt b/src/main/kotlin/space/luminic/budgerapp/repos/TransactionRepo.kt index 0963ca1..6c4e948 100644 --- a/src/main/kotlin/space/luminic/budgerapp/repos/TransactionRepo.kt +++ b/src/main/kotlin/space/luminic/budgerapp/repos/TransactionRepo.kt @@ -1,8 +1,12 @@ package space.luminic.budgerapp.repos +import org.bson.types.ObjectId +import org.springframework.data.mongodb.repository.Query import org.springframework.data.mongodb.repository.ReactiveMongoRepository import org.springframework.stereotype.Repository +import reactor.core.publisher.Flux import reactor.core.publisher.Mono +import space.luminic.budgerapp.models.Category import space.luminic.budgerapp.models.Transaction @@ -15,4 +19,7 @@ interface TransactionRepo: ReactiveMongoRepository { // fun findByOldId(transactionId: Int): Optional fun findByParentId(parentId: String): Mono + + @Query("{ 'space': { '\$ref': 'spaces','\$id': ?0 } }") + fun findBySpaceId(spaceId: ObjectId): Flux } \ No newline at end of file diff --git a/src/main/kotlin/space/luminic/budgerapp/services/CategoryService.kt b/src/main/kotlin/space/luminic/budgerapp/services/CategoryService.kt index 757a68f..e913697 100644 --- a/src/main/kotlin/space/luminic/budgerapp/services/CategoryService.kt +++ b/src/main/kotlin/space/luminic/budgerapp/services/CategoryService.kt @@ -46,6 +46,8 @@ class CategoryService( return categoryRepo.findById(id) } + + // @Cacheable("categories") fun getCategories(spaceId: String, type: String? = null, sortBy: String, direction: String): Mono> { val matchCriteria = mutableListOf() diff --git a/src/main/kotlin/space/luminic/budgerapp/services/FinancialService.kt b/src/main/kotlin/space/luminic/budgerapp/services/FinancialService.kt index 7be5ea3..ed41a79 100644 --- a/src/main/kotlin/space/luminic/budgerapp/services/FinancialService.kt +++ b/src/main/kotlin/space/luminic/budgerapp/services/FinancialService.kt @@ -318,16 +318,15 @@ class FinancialService( fun regenCats(): Mono { - return categoryRepo.findAll()// Получаем список категорий + return categoryRepo.findBySpaceId(ObjectId("67b352b13384483a1c2282ed")) .flatMap { cat -> - spaceService.getSpace("67af3c0f652da946a7dd9931") // Получаем space - .map { space -> - cat.space = space // Привязываем пространство к категории - cat - } +// if (cat.space?.id == "67b352b13384483a1c2282ed") { + categoryRepo.deleteById(cat.id!!) // Возвращаем `Mono` +// } else { +// Mono.empty() // Если не удаляем, возвращаем пустой `Mono` +// } } - .flatMap { updatedCategory -> categoryRepo.save(updatedCategory) } // Сохраняем в БД - .then() // Завершаем Mono + .then() // Убедимся, что все операции завершены } @CacheEvict(cacheNames = ["budgets", "budgetsList"], allEntries = true) diff --git a/src/main/kotlin/space/luminic/budgerapp/services/SpaceService.kt b/src/main/kotlin/space/luminic/budgerapp/services/SpaceService.kt index 1b63add..9e3d352 100644 --- a/src/main/kotlin/space/luminic/budgerapp/services/SpaceService.kt +++ b/src/main/kotlin/space/luminic/budgerapp/services/SpaceService.kt @@ -1,17 +1,15 @@ package space.luminic.budgerapp.services import org.bson.types.ObjectId -import org.springframework.data.domain.Sort -import org.springframework.data.domain.Sort.Direction +import org.springframework.data.mongodb.core.ReactiveMongoTemplate +import org.springframework.data.mongodb.core.query.Query import org.springframework.security.core.context.ReactiveSecurityContextHolder import org.springframework.stereotype.Service import reactor.core.publisher.Mono +import space.luminic.budgerapp.models.Category import space.luminic.budgerapp.models.Space import space.luminic.budgerapp.models.SpaceInvite -import space.luminic.budgerapp.models.Transaction -import space.luminic.budgerapp.repos.BudgetRepo -import space.luminic.budgerapp.repos.SpaceRepo -import space.luminic.budgerapp.repos.UserRepo +import space.luminic.budgerapp.repos.* import java.time.LocalDateTime import java.util.UUID @@ -20,7 +18,11 @@ class SpaceService( private val spaceRepo: SpaceRepo, private val userService: UserService, private val budgetRepo: BudgetRepo, - private val userRepo: UserRepo + private val userRepo: UserRepo, + private val reactiveMongoTemplate: ReactiveMongoTemplate, + private val categoryRepo: CategoryRepo, + private val recurrentRepo: RecurrentRepo, + private val transactionRepo: TransactionRepo ) { fun isValidRequest(spaceId: String): Mono { @@ -66,7 +68,7 @@ class SpaceService( .switchIfEmpty(Mono.error(IllegalArgumentException("SpaceId not found for spaceId: $spaceId"))) } - fun createSpace(space: Space): Mono { + fun createSpace(space: Space, createCategories: Boolean): Mono { return ReactiveSecurityContextHolder.getContext() .map { it.authentication } .flatMap { authentication -> @@ -76,20 +78,50 @@ class SpaceService( .flatMap { user -> space.owner = user space.users.add(user) - spaceRepo.save(space) + spaceRepo.save(space).flatMap { savedSpace -> + if (!createCategories) { + return@flatMap Mono.just(savedSpace) // Если не нужно создавать категории, просто возвращаем пространство + } + + reactiveMongoTemplate.find(Query(), Category::class.java, "categories-etalon") + .map { category -> + category.copy( + id = null, + space = savedSpace + ) // Создаем новую копию без id (чтобы создать новый документ) + } + .flatMap { categoryRepo.save(it) } + .then(Mono.just(savedSpace)) // После сохранения всех категорий, возвращаем пространство + } } } } - fun deleteSpace(spaceId: String): Mono { - return budgetRepo.findBySpaceId(ObjectId(spaceId), Sort.by(Direction.DESC, "dateFrom")) - .flatMap { budget -> - budgetRepo.delete(budget) // Удаляем все бюджеты, связанные с этим Space - } - .then(spaceRepo.deleteById(spaceId)) // Затем удаляем сам Space + fun deleteSpace(space: Space): Mono { + val objectId = ObjectId(space.id) + + return Mono.`when`( + budgetRepo.findBySpaceId(objectId) + .flatMap { budgetRepo.delete(it) } + .then(), + + transactionRepo.findBySpaceId(objectId) + .flatMap { transactionRepo.delete(it) } + .then(), + + categoryRepo.findBySpaceId(objectId) + .flatMap { categoryRepo.delete(it) } + .then(), + + recurrentRepo.findRecurrentsBySpaceId(objectId) + .flatMap { recurrentRepo.delete(it) } + .then() + ).then(spaceRepo.deleteById(space.id!!)) // Исправлено: удаление по ID } + + fun createInviteSpace(spaceId: String): Mono { return ReactiveSecurityContextHolder.getContext() .map { it.authentication }