add categories thought spaces

This commit is contained in:
xds
2025-02-17 18:31:46 +03:00
parent d680345a9f
commit 0c445a5141
8 changed files with 91 additions and 33 deletions

View File

@@ -46,6 +46,8 @@ class CategoryService(
return categoryRepo.findById(id)
}
// @Cacheable("categories")
fun getCategories(spaceId: String, type: String? = null, sortBy: String, direction: String): Mono<List<Category>> {
val matchCriteria = mutableListOf<Criteria>()

View File

@@ -318,16 +318,15 @@ class FinancialService(
fun regenCats(): Mono<Void> {
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<Void>`
// } else {
// Mono.empty() // Если не удаляем, возвращаем пустой `Mono`
// }
}
.flatMap { updatedCategory -> categoryRepo.save(updatedCategory) } // Сохраняем в БД
.then() // Завершаем Mono<Void>
.then() // Убедимся, что все операции завершены
}
@CacheEvict(cacheNames = ["budgets", "budgetsList"], allEntries = true)

View File

@@ -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<Space> {
@@ -66,7 +68,7 @@ class SpaceService(
.switchIfEmpty(Mono.error(IllegalArgumentException("SpaceId not found for spaceId: $spaceId")))
}
fun createSpace(space: Space): Mono<Space> {
fun createSpace(space: Space, createCategories: Boolean): Mono<Space> {
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<Void> {
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<Void> {
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<SpaceInvite> {
return ReactiveSecurityContextHolder.getContext()
.map { it.authentication }