diff --git a/src/main/kotlin/space/luminic/budgerapp/controllers/CategoriesController.kt b/src/main/kotlin/space/luminic/budgerapp/controllers/CategoriesController.kt index c014b36..ca34976 100644 --- a/src/main/kotlin/space/luminic/budgerapp/controllers/CategoriesController.kt +++ b/src/main/kotlin/space/luminic/budgerapp/controllers/CategoriesController.kt @@ -34,10 +34,11 @@ class CategoriesController( @GetMapping() fun getCategories( + @RequestParam("type") type: String? = null, @RequestParam("sort") sortBy: String = "name", @RequestParam("direction") direction: String = "ASC" ): Mono> { - return categoryService.getCategories(sortBy, direction) + return categoryService.getCategories(type, sortBy, direction) } diff --git a/src/main/kotlin/space/luminic/budgerapp/repos/CategoryRepo.kt b/src/main/kotlin/space/luminic/budgerapp/repos/CategoryRepo.kt index c894dd5..e746028 100644 --- a/src/main/kotlin/space/luminic/budgerapp/repos/CategoryRepo.kt +++ b/src/main/kotlin/space/luminic/budgerapp/repos/CategoryRepo.kt @@ -12,4 +12,5 @@ interface CategoryRepo : ReactiveMongoRepository { fun findByName(name: String): Mono + } \ 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 b6d35d2..f7f26b4 100644 --- a/src/main/kotlin/space/luminic/budgerapp/services/CategoryService.kt +++ b/src/main/kotlin/space/luminic/budgerapp/services/CategoryService.kt @@ -2,18 +2,29 @@ package space.luminic.budgerapp.services import org.bson.Document +import org.bson.types.ObjectId import org.slf4j.LoggerFactory import org.springframework.cache.annotation.CacheEvict import org.springframework.cache.annotation.Cacheable 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.aggregation.Aggregation.limit +import org.springframework.data.mongodb.core.aggregation.Aggregation.lookup +import org.springframework.data.mongodb.core.aggregation.Aggregation.match +import org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation +import org.springframework.data.mongodb.core.aggregation.Aggregation.skip +import org.springframework.data.mongodb.core.aggregation.Aggregation.sort +import org.springframework.data.mongodb.core.query.Criteria +import org.springframework.data.mongodb.core.query.isEqualTo import org.springframework.stereotype.Service import reactor.core.publisher.Flux import reactor.core.publisher.Mono import space.luminic.budgerapp.models.BudgetCategory import space.luminic.budgerapp.models.Category import space.luminic.budgerapp.models.CategoryType +import space.luminic.budgerapp.models.SortSetting +import space.luminic.budgerapp.models.Transaction import space.luminic.budgerapp.repos.CategoryRepo import java.time.LocalDate import java.time.LocalDateTime @@ -37,8 +48,38 @@ class CategoryService( return categoryRepo.findById(id) } +// @Cacheable("categories") + fun getCategories(type: String? = null, sortBy: String, direction: String): Mono> { + val matchCriteria = mutableListOf() + + // Добавляем фильтры + type?.let { matchCriteria.add(Criteria.where("type.code").isEqualTo(it)) } + + val match = match(Criteria().andOperator(*matchCriteria.toTypedArray())) + + + val sort = sort(Sort.by(direction, sortBy)) + + + val aggregationBuilder = mutableListOf( + + + match.takeIf { matchCriteria.isNotEmpty() }, + sort, + ).filterNotNull() + + val aggregation = newAggregation(aggregationBuilder) + + return mongoTemplate.aggregate( + aggregation, "categories", Category::class.java + ) + .collectList() // Преобразуем Flux в Mono> + .map { it.toMutableList() } + } + + @Cacheable("getAllCategories") - fun getCategories(sortBy: String, direction: String): Mono> { + fun getCategories2(type: String? = null, sortBy: String, direction: String): Mono> { return categoryRepo.findAll(Sort.by(if (direction == "ASC") Direction.ASC else Direction.DESC, sortBy)) .collectList()