some
This commit is contained in:
@@ -10,10 +10,7 @@ import org.springframework.security.core.context.ReactiveSecurityContextHolder
|
||||
import org.springframework.stereotype.Service
|
||||
import reactor.core.publisher.Flux
|
||||
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.Tag
|
||||
import space.luminic.budgerapp.models.*
|
||||
import space.luminic.budgerapp.repos.*
|
||||
import java.time.LocalDateTime
|
||||
import java.util.UUID
|
||||
@@ -23,7 +20,6 @@ class SpaceService(
|
||||
private val spaceRepo: SpaceRepo,
|
||||
private val userService: UserService,
|
||||
private val budgetRepo: BudgetRepo,
|
||||
private val userRepo: UserRepo,
|
||||
private val reactiveMongoTemplate: ReactiveMongoTemplate,
|
||||
private val categoryRepo: CategoryRepo,
|
||||
private val recurrentRepo: RecurrentRepo,
|
||||
@@ -44,7 +40,7 @@ class SpaceService(
|
||||
.switchIfEmpty(Mono.error(IllegalArgumentException("User not found for username: $username")))
|
||||
.flatMap { user ->
|
||||
// Получаем пространство по ID
|
||||
spaceRepo.findById(spaceId)
|
||||
getSpace(spaceId)
|
||||
.switchIfEmpty(Mono.error(IllegalArgumentException("Space not found for id: $spaceId")))
|
||||
.flatMap { space ->
|
||||
// Проверяем доступ пользователя к пространству
|
||||
@@ -65,10 +61,51 @@ class SpaceService(
|
||||
val username = authentication.name
|
||||
userService.getByUsername(username)
|
||||
.switchIfEmpty(Mono.error(IllegalArgumentException("User not found for username: $username")))
|
||||
.flatMapMany { user ->
|
||||
spaceRepo.findByArrayElement(ObjectId(user.id!!))
|
||||
.flatMap { user ->
|
||||
val userId = ObjectId(user.id!!)
|
||||
|
||||
// Поиск пространств пользователя
|
||||
|
||||
|
||||
// Агрегация для загрузки владельца и пользователей
|
||||
val lookupOwner = lookup("users", "owner.\$id", "_id", "ownerDetails")
|
||||
val unwindOwner = unwind("ownerDetails")
|
||||
|
||||
val lookupUsers = lookup("users", "users.\$id", "_id", "usersDetails")
|
||||
// val unwindUsers = unwind("usersDetails")
|
||||
|
||||
val matchStage = match(Criteria.where("usersDetails._id").`is`(userId))
|
||||
val aggregation = newAggregation(lookupOwner, unwindOwner, lookupUsers, matchStage)
|
||||
|
||||
reactiveMongoTemplate.aggregate(aggregation, "spaces", Document::class.java)
|
||||
.collectList()
|
||||
.map { docs ->
|
||||
docs.map { doc ->
|
||||
val ownerDoc = doc.get("ownerDetails", Document::class.java)
|
||||
val usersDocList = doc.getList("usersDetails", Document::class.java)
|
||||
|
||||
Space(
|
||||
id = doc.getObjectId("_id").toString(),
|
||||
name = doc.getString("name"),
|
||||
description = doc.getString("description"),
|
||||
owner = User(
|
||||
id = ownerDoc.getObjectId("_id").toString(),
|
||||
username = ownerDoc.getString("username"),
|
||||
firstName = ownerDoc.getString("firstName")
|
||||
),
|
||||
users = usersDocList.map { userDoc ->
|
||||
User(
|
||||
id = userDoc.getObjectId("_id").toString(),
|
||||
username = userDoc.getString("username"),
|
||||
firstName = userDoc.getString("firstName")
|
||||
)
|
||||
}.toMutableList()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
.collectList() // Возвращаем Mono<List<Space>>
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,12 +131,10 @@ class SpaceService(
|
||||
|
||||
reactiveMongoTemplate.find(Query(), Category::class.java, "categories-etalon")
|
||||
.map { category ->
|
||||
category.copy(
|
||||
id = null,
|
||||
space = savedSpace
|
||||
) // Создаем новую копию без id (чтобы создать новый документ)
|
||||
category.copy(id = null, space = savedSpace) // Создаем новую копию
|
||||
}
|
||||
.flatMap { categoryRepo.save(it) }
|
||||
.collectList() // Собираем в список перед сохранением
|
||||
.flatMap { categoryRepo.saveAll(it).collectList() } // Сохраняем и возвращаем список
|
||||
.then(Mono.just(savedSpace)) // После сохранения всех категорий, возвращаем пространство
|
||||
}
|
||||
}
|
||||
@@ -111,25 +146,17 @@ class SpaceService(
|
||||
|
||||
return Mono.`when`(
|
||||
financialService.findProjectedBudgets(objectId)
|
||||
.flatMapMany { Flux.fromIterable(it) }
|
||||
.flatMap { budgetRepo.deleteById(it.id!!) }
|
||||
.then(),
|
||||
.flatMap { budgetRepo.deleteAll(it) },
|
||||
|
||||
financialService.getTransactions(objectId.toString())
|
||||
.flatMapMany { Flux.fromIterable(it) }
|
||||
.flatMap { transactionRepo.deleteById(it.id!!) }
|
||||
.then(),
|
||||
.flatMap { transactionRepo.deleteAll(it) },
|
||||
|
||||
categoryService.getCategories(objectId.toString(), null, "name", "ASC")
|
||||
.flatMapMany { Flux.fromIterable(it) }
|
||||
.flatMap { categoryRepo.deleteById(it.id!!) }
|
||||
.then(),
|
||||
.flatMap { categoryRepo.deleteAll(it) },
|
||||
|
||||
recurrentService.getRecurrents(objectId.toString())
|
||||
.flatMapMany { Flux.fromIterable(it) }
|
||||
.flatMap { recurrentRepo.deleteById(it.id!!) }
|
||||
.then()
|
||||
).then(spaceRepo.deleteById(space.id!!)) // Исправлено: удаление по ID
|
||||
.flatMap { recurrentRepo.deleteAll(it) }
|
||||
).then(spaceRepo.deleteById(space.id!!)) // Удаление Space после завершения всех операций
|
||||
}
|
||||
|
||||
|
||||
@@ -331,7 +358,7 @@ class SpaceService(
|
||||
return reactiveMongoTemplate.aggregate(
|
||||
aggregation, "tags", Document::class.java
|
||||
)
|
||||
.collectList() // Преобразуем Flux<Transaction> в Mono<List<Transaction>>
|
||||
.collectList()
|
||||
.map { docs ->
|
||||
docs.map { doc ->
|
||||
Tag(
|
||||
@@ -347,7 +374,7 @@ class SpaceService(
|
||||
fun regenSpaceCategory(): Mono<Category> {
|
||||
return getSpace("67af3c0f652da946a7dd9931")
|
||||
.flatMap { space ->
|
||||
categoryService.findCategory(id= "677bc767c7857460a491bd4f")
|
||||
categoryService.findCategory(id = "677bc767c7857460a491bd4f")
|
||||
.flatMap { category -> // заменил map на flatMap
|
||||
category.space = space
|
||||
category.name = "Сбережения"
|
||||
|
||||
Reference in New Issue
Block a user