+ category filter by type
This commit is contained in:
@@ -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<List<Category>> {
|
||||
return categoryService.getCategories(sortBy, direction)
|
||||
return categoryService.getCategories(type, sortBy, direction)
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -12,4 +12,5 @@ interface CategoryRepo : ReactiveMongoRepository<Category, String> {
|
||||
fun findByName(name: String): Mono<Category>
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -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<List<Category>> {
|
||||
val matchCriteria = mutableListOf<Criteria>()
|
||||
|
||||
// Добавляем фильтры
|
||||
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<Transaction> в Mono<List<Transaction>>
|
||||
.map { it.toMutableList() }
|
||||
}
|
||||
|
||||
|
||||
@Cacheable("getAllCategories")
|
||||
fun getCategories(sortBy: String, direction: String): Mono<List<Category>> {
|
||||
fun getCategories2(type: String? = null, sortBy: String, direction: String): Mono<List<Category>> {
|
||||
return categoryRepo.findAll(Sort.by(if (direction == "ASC") Direction.ASC else Direction.DESC, sortBy))
|
||||
.collectList()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user