diff --git a/save-demo/src/main/kotlin/com/saveourtool/save/demo/controller/DemoController.kt b/save-demo/src/main/kotlin/com/saveourtool/save/demo/controller/DemoController.kt index 3719a840f8..96d7f171c3 100644 --- a/save-demo/src/main/kotlin/com/saveourtool/save/demo/controller/DemoController.kt +++ b/save-demo/src/main/kotlin/com/saveourtool/save/demo/controller/DemoController.kt @@ -19,6 +19,7 @@ import org.springframework.web.bind.annotation.RequestParam import org.springframework.web.bind.annotation.RestController import reactor.core.publisher.Flux import reactor.core.publisher.Mono +import reactor.kotlin.core.publisher.toMono import reactor.kotlin.core.util.function.component1 import reactor.kotlin.core.util.function.component2 @@ -43,14 +44,15 @@ class DemoController( */ @PostMapping("/demo-list") fun getFilteredDemoList( - @RequestBody(required = false) filter: DemoFilter = DemoFilter.any, + @RequestBody(required = false) filter: DemoFilter?, @RequestParam(required = false, defaultValue = DemoDto.DEFAULT_FETCH_NUMBER.toString()) demoAmount: Int = DemoDto.DEFAULT_FETCH_NUMBER, - ): Flux = blockingToFlux { - demoService.getFiltered(filter, demoAmount).toList().map { it to demoService.getStatus(it).block() } - } - .filter { (_, status) -> status in filter.statuses } - .map { it.first.toDto() } + ): Flux = filter.toMono() + .switchIfEmpty(DemoFilter.any.toMono()) + .flatMapMany { demoFilter -> + blockingToFlux { demoService.getFiltered(demoFilter, demoAmount) } + } + .map { it.toDto() } /** * @param organizationName saveourtool organization name diff --git a/save-demo/src/main/kotlin/com/saveourtool/save/demo/service/DemoService.kt b/save-demo/src/main/kotlin/com/saveourtool/save/demo/service/DemoService.kt index 5e9cfef777..e868868e84 100644 --- a/save-demo/src/main/kotlin/com/saveourtool/save/demo/service/DemoService.kt +++ b/save-demo/src/main/kotlin/com/saveourtool/save/demo/service/DemoService.kt @@ -14,7 +14,6 @@ import com.saveourtool.save.utils.StringResponse import com.saveourtool.save.utils.blockingToMono import com.saveourtool.save.utils.switchIfEmptyToNotFound -import org.springframework.data.domain.Page import org.springframework.data.domain.PageRequest import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional @@ -95,7 +94,7 @@ class DemoService( * @param pageSize amount of [Demo]s that should be fetched * @return list of [Demo]s that match [DemoFilter] */ - fun getFiltered(demoFilter: DemoFilter, pageSize: Int): Page = demoRepository.findAll({ root, _, cb -> + fun getFiltered(demoFilter: DemoFilter, pageSize: Int): List = demoRepository.findAll({ root, _, cb -> with(demoFilter) { val organizationNamePredicate = if (organizationName.isBlank()) { cb.and() @@ -114,6 +113,8 @@ class DemoService( ) } }, PageRequest.ofSize(pageSize)) + .filter { demoFilter.statuses.isEmpty() || getStatus(it).block() in demoFilter.statuses } + .toList() /** * @param demo [Demo] entity @@ -154,6 +155,7 @@ class DemoService( * @param projectName saveourtool project name * @return [Demo] connected with project [organizationName]/[projectName] or null if not present */ + @Transactional(readOnly = true) fun findBySaveourtoolProject( organizationName: String, projectName: String,