From 2610eb04040e2708c73b96fc5696055ccdec5b72 Mon Sep 17 00:00:00 2001 From: xds Date: Mon, 17 Feb 2025 22:16:27 +0300 Subject: [PATCH] fix date to localdate --- .../budgerapp/controllers/SpaceController.kt | 2 ++ .../space/luminic/budgerapp/repos/BudgetRepo.kt | 10 +++++++++- .../luminic/budgerapp/services/FinancialService.kt | 14 +++++++++++++- src/main/resources/application.properties | 1 + 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/space/luminic/budgerapp/controllers/SpaceController.kt b/src/main/kotlin/space/luminic/budgerapp/controllers/SpaceController.kt index 79bcd38..62c4d7a 100644 --- a/src/main/kotlin/space/luminic/budgerapp/controllers/SpaceController.kt +++ b/src/main/kotlin/space/luminic/budgerapp/controllers/SpaceController.kt @@ -124,6 +124,8 @@ class SpaceController( } } + + @GetMapping("/{spaceId}/transactions/{id}") fun getTransaction( @PathVariable spaceId: String, diff --git a/src/main/kotlin/space/luminic/budgerapp/repos/BudgetRepo.kt b/src/main/kotlin/space/luminic/budgerapp/repos/BudgetRepo.kt index b641359..67b32de 100644 --- a/src/main/kotlin/space/luminic/budgerapp/repos/BudgetRepo.kt +++ b/src/main/kotlin/space/luminic/budgerapp/repos/BudgetRepo.kt @@ -15,6 +15,14 @@ import java.time.LocalDate @Repository interface BudgetRepo: ReactiveMongoRepository { + + // @Id var id: String? = null, + // @DBRef var space: Space? = null, + // var name: String, + // var dateFrom: LocalDate, + // var dateTo: LocalDate, + + override fun findAll(sort: Sort): Flux fun findByDateFromLessThanEqualAndDateToGreaterThanEqual(dateOne: LocalDate, dateTwo: LocalDate): Mono @@ -26,6 +34,6 @@ interface BudgetRepo: ReactiveMongoRepository { @Query("{ 'space': { '\$ref': 'spaces','\$id': ?0 } }") fun findBySpaceId(spaceId: ObjectId, sort: Sort): Flux - @Query("{ 'space': { '\$ref': 'spaces','\$id': ?0 } }") + @Query("{ 'space': { '\$ref': 'spaces','\$id': ?0 }, }", fields = "{'id': 1, 'name':1, 'dateFrom':1, 'dateTo':1}") fun findBySpaceId(spaceId: ObjectId): Flux } \ No newline at end of file diff --git a/src/main/kotlin/space/luminic/budgerapp/services/FinancialService.kt b/src/main/kotlin/space/luminic/budgerapp/services/FinancialService.kt index 07447f4..e4b3c26 100644 --- a/src/main/kotlin/space/luminic/budgerapp/services/FinancialService.kt +++ b/src/main/kotlin/space/luminic/budgerapp/services/FinancialService.kt @@ -10,6 +10,7 @@ import org.springframework.data.domain.Sort import org.springframework.data.domain.Sort.Direction import org.springframework.data.mongodb.core.MongoTemplate import org.springframework.data.mongodb.core.ReactiveMongoTemplate +import org.springframework.data.mongodb.core.aggregation.Aggregation import org.springframework.data.mongodb.core.aggregation.Aggregation.* import org.springframework.data.mongodb.core.aggregation.DateOperators.DateToString import org.springframework.data.mongodb.core.query.Criteria @@ -222,7 +223,7 @@ class FinancialService( println("Space ID type: ${spaceObjectId::class.java}, value: $spaceObjectId") // Применяем сортировку к запросу - budgetRepo.findBySpaceId(spaceObjectId, sort).collectList() + findProjectedBudgets(spaceObjectId).collectList() } @@ -230,6 +231,17 @@ class FinancialService( } } } + fun findProjectedBudgets(spaceId: ObjectId): Flux { + val lookupSpaces = lookup("spaces", "space.\$id", "_id", "spaceDetails") + + val matchStage = Aggregation.match(Criteria.where("spaceDetails._id").`is`(spaceId)) +// matchCriteria.add(Criteria.where("spaceDetails._id").`is`(ObjectId(spaceId))) + val projectStage = Aggregation.project("_id", "name", "dateFrom", "dateTo") // Оставляем только нужные поля + val sort = sort(Sort.by(Sort.Order.desc("_id"))) + val aggregation = Aggregation.newAggregation(lookupSpaces, matchStage, projectStage, sort) + + return reactiveMongoTemplate.aggregate(aggregation, "budgets", Budget::class.java) + } // @Cacheable("budgets", key = "#id") fun getBudget(id: String): Mono { diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index e1c17cb..d87391a 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -15,6 +15,7 @@ logging.level.org.springframework.data.mongodb.core.ReactiveMongoTemplate=INFO logging.level.org.springframework.security = INFO logging.level.org.springframework.data.mongodb.code = INFO logging.level.org.springframework.web.reactive=INFO +logging.level.org.mongodb.driver.protocol.command = INFO server.compression.enabled=true server.compression.mime-types=application/json