diff --git a/src/main/kotlin/com/kvsinyuk/elasticsearch/adapter/in/http/DealController.kt b/src/main/kotlin/com/kvsinyuk/elasticsearch/adapter/in/http/DealController.kt index 561d1ca..8dad05f 100644 --- a/src/main/kotlin/com/kvsinyuk/elasticsearch/adapter/in/http/DealController.kt +++ b/src/main/kotlin/com/kvsinyuk/elasticsearch/adapter/in/http/DealController.kt @@ -1,6 +1,7 @@ package com.kvsinyuk.elasticsearch.adapter.`in`.http import com.kvsinyuk.elasticsearch.application.port.SearchDealsUseCase +import com.kvsinyuk.elasticsearch.application.port.SearchDealsUseCase.SearchCommand import com.kvsinyuk.elasticsearch.domain.Deal import mu.KLogging import org.springframework.data.domain.Page @@ -14,12 +15,14 @@ class DealController( private val searchDealsUseCase: SearchDealsUseCase, ) { @GetMapping("/deals") - fun generateRandomData( + fun getDeals( @RequestParam search: String, + @RequestParam database: String = "elasticsearch", pageable: Pageable, ): Page { logger.info { "Received request for search deals with $search" } - return searchDealsUseCase.searchDeals(search, pageable) + val command = SearchCommand(search, database, pageable) + return searchDealsUseCase.searchDeals(command) } companion object : KLogging() diff --git a/src/main/kotlin/com/kvsinyuk/elasticsearch/application/impl/SearchDealsUseCaseImpl.kt b/src/main/kotlin/com/kvsinyuk/elasticsearch/application/impl/SearchDealsUseCaseImpl.kt index 7280658..bfed87a 100644 --- a/src/main/kotlin/com/kvsinyuk/elasticsearch/application/impl/SearchDealsUseCaseImpl.kt +++ b/src/main/kotlin/com/kvsinyuk/elasticsearch/application/impl/SearchDealsUseCaseImpl.kt @@ -1,16 +1,19 @@ package com.kvsinyuk.elasticsearch.application.impl import com.kvsinyuk.elasticsearch.adapter.out.elasticsearch.ElasticDealRepository +import com.kvsinyuk.elasticsearch.adapter.out.mongo.MongoDealRepository import com.kvsinyuk.elasticsearch.application.port.SearchDealsUseCase -import org.springframework.data.domain.Pageable +import com.kvsinyuk.elasticsearch.application.port.SearchDealsUseCase.SearchCommand import org.springframework.stereotype.Component @Component class SearchDealsUseCaseImpl( - private val dealRepository: ElasticDealRepository, + private val elasticDealRepository: ElasticDealRepository, + private val mongoDealRepository: MongoDealRepository, ) : SearchDealsUseCase { - override fun searchDeals( - search: String, - page: Pageable, - ) = dealRepository.searchAll(search, page) + override fun searchDeals(command: SearchCommand) = + when (command.databaseType) { + "elasticsearch" -> elasticDealRepository.searchAll(command.search, command.page) + else -> mongoDealRepository.findAll(command.page) + } } diff --git a/src/main/kotlin/com/kvsinyuk/elasticsearch/application/port/SearchDealsUseCase.kt b/src/main/kotlin/com/kvsinyuk/elasticsearch/application/port/SearchDealsUseCase.kt index 6ae246e..5f2ce4d 100644 --- a/src/main/kotlin/com/kvsinyuk/elasticsearch/application/port/SearchDealsUseCase.kt +++ b/src/main/kotlin/com/kvsinyuk/elasticsearch/application/port/SearchDealsUseCase.kt @@ -5,8 +5,11 @@ import org.springframework.data.domain.Page import org.springframework.data.domain.Pageable interface SearchDealsUseCase { - fun searchDeals( - search: String, - page: Pageable, - ): Page + fun searchDeals(command: SearchCommand): Page + + data class SearchCommand( + val search: String, + val databaseType: String, + val page: Pageable, + ) }