diff --git a/src/main/kotlin/database/model/MatchResult.kt b/src/main/kotlin/database/model/MatchResult.kt index 0d7fd42..f2f93da 100644 --- a/src/main/kotlin/database/model/MatchResult.kt +++ b/src/main/kotlin/database/model/MatchResult.kt @@ -1,18 +1,11 @@ package database.model sealed class MatchResult { + data class CustomerAndPet( val filteredCustomers: Map, val filteredPets: Map ) : MatchResult() - data class Customer( - val filteredCustomers: Map - ) : MatchResult() - - data class Pet( - val filteredPets: Map - ) : MatchResult() - object NoMatch : MatchResult() } diff --git a/src/main/kotlin/database/model/SearchQueryResult.kt b/src/main/kotlin/database/model/SearchQueryResult.kt index bbdf318..7df998e 100644 --- a/src/main/kotlin/database/model/SearchQueryResult.kt +++ b/src/main/kotlin/database/model/SearchQueryResult.kt @@ -4,15 +4,8 @@ import database.entity.CustomerEntity import database.entity.PetEntity sealed class SearchQueryResult { - data class CustomerSuccess( - val customers: List - ) : SearchQueryResult() - - data class PetSuccess( - val pets: List - ) : SearchQueryResult() - data class CustomerAndPetSuccess( + data class Success( val customers: List, val pets: List ) : SearchQueryResult() diff --git a/src/main/kotlin/database/search/SearchDatabase.kt b/src/main/kotlin/database/search/SearchDatabase.kt index 908eda9..1a91e6a 100644 --- a/src/main/kotlin/database/search/SearchDatabase.kt +++ b/src/main/kotlin/database/search/SearchDatabase.kt @@ -30,13 +30,7 @@ class SearchDatabase( is MatchResult.CustomerAndPet -> { val customers = getAllMatchingCustomerEntities(result.filteredCustomers) val pets = getAllMatchingPetEntities(result.filteredPets) - SearchQueryResult.CustomerAndPetSuccess(customers, pets) - } - is MatchResult.Customer -> { - SearchQueryResult.CustomerSuccess(getAllMatchingCustomerEntities(result.filteredCustomers)) - } - is MatchResult.Pet -> { - SearchQueryResult.PetSuccess(getAllMatchingPetEntities(result.filteredPets)) + SearchQueryResult.Success(customers, pets) } is MatchResult.NoMatch -> { SearchQueryResult.Error("no match found") @@ -45,27 +39,15 @@ class SearchDatabase( } private fun checkTablesForAnyMatch(query: String): MatchResult { - val doesAnyCustomersMatch = vetPracticeDatabase.getAllCustomers().entries.any { - it.value.name.lowercase() == query - } - val doesAnyPetsMatch = vetPracticeDatabase.getAllPets().entries.any { - it.value.name.lowercase() == query - } + val doesAnyCustomersMatch = getMatchingResultsForCustomers(query) + val doesAnyPetsMatch = getMatchingResultsForPets(query) return when { - doesAnyCustomersMatch && doesAnyPetsMatch -> { + doesAnyCustomersMatch.isNotEmpty() || doesAnyPetsMatch.isNotEmpty() -> { val filteredCustomers = getMatchingResultsForCustomers(query) val filteredPets = getMatchingResultsForPets(query) MatchResult.CustomerAndPet(filteredCustomers, filteredPets) } - doesAnyCustomersMatch -> { - val filteredCustomers = getMatchingResultsForCustomers(query) - MatchResult.Customer(filteredCustomers) - } - doesAnyPetsMatch -> { - val filteredPets = getMatchingResultsForPets(query) - MatchResult.Pet(filteredPets) - } else -> { MatchResult.NoMatch } diff --git a/src/main/kotlin/presenter/VeterinaryPracticePresenter.kt b/src/main/kotlin/presenter/VeterinaryPracticePresenter.kt index 13ee56e..a5e7178 100644 --- a/src/main/kotlin/presenter/VeterinaryPracticePresenter.kt +++ b/src/main/kotlin/presenter/VeterinaryPracticePresenter.kt @@ -50,9 +50,7 @@ class VeterinaryPracticePresenter( override fun search(query: String) { when (val result = vetPracticeDatabase.query(query)) { - is SearchQueryResult.CustomerAndPetSuccess -> view.showCustomersAndPets(result.customers, result.pets) - is SearchQueryResult.CustomerSuccess -> view.showCustomers(customers = result.customers) - is SearchQueryResult.PetSuccess -> view.showPets(result.pets) + is SearchQueryResult.Success -> view.showQueryResult(result.customers, result.pets) is SearchQueryResult.Error -> view.showError(result.message) } } diff --git a/src/main/kotlin/view/ConsoleView.kt b/src/main/kotlin/view/ConsoleView.kt index c00aa09..a5e160e 100644 --- a/src/main/kotlin/view/ConsoleView.kt +++ b/src/main/kotlin/view/ConsoleView.kt @@ -6,9 +6,7 @@ import database.entity.PetEntity interface ConsoleView { fun startProgram(fileName: String) fun searchProgram(searchQuery: String) - fun showCustomers(customers: List) - fun showPets(pets: List) - fun showCustomersAndPets(customers: List, pets: List) + fun showQueryResult(customers: List, pets: List) fun showInvalidErrorMessage(lineNumber: Int) fun showErrorOnLineWithMessage(lineNumber: Int, message: String) fun showError(message: String) diff --git a/src/main/kotlin/view/VeterinaryPracticeConsoleView.kt b/src/main/kotlin/view/VeterinaryPracticeConsoleView.kt index 26937da..e6e45a5 100644 --- a/src/main/kotlin/view/VeterinaryPracticeConsoleView.kt +++ b/src/main/kotlin/view/VeterinaryPracticeConsoleView.kt @@ -15,16 +15,13 @@ class VeterinaryPracticeConsoleView(private val presenter: Presenter) : ConsoleV presenter.search(searchQuery) } - override fun showCustomers(customers: List) { + override fun showQueryResult(customers: List, pets: List) { customers.forEach { customer -> println("Customer: ${customer.customer}") println("Identifier: ${customer.identifier}") println("Pets: ${customer.pets}") println() } - } - - override fun showPets(pets: List) { pets.forEach { pet -> println("Pet: ${pet.pet}") println("Type: ${pet.type}") @@ -35,22 +32,6 @@ class VeterinaryPracticeConsoleView(private val presenter: Presenter) : ConsoleV } } - override fun showCustomersAndPets(customers: List, pets: List) { - customers.forEach { customer -> - println("Customer: ${customer.customer}") - println("Identifier: ${customer.identifier}") - println("Pets: ${customer.pets}") - println() - } - pets.forEach { pet -> - println("Pet: ${pet.pet}") - println("Type: ${pet.type}") - println("Identifier: ${pet.identifier}") - println("Owner: ${pet.owner}") - println("Owner ID: ${pet.ownerId}") - } - } - override fun showInvalidErrorMessage(lineNumber: Int) { println("Error on line ${lineNumber}: Invalid syntax") } diff --git a/src/test/kotlin/database/search/SearchDatabaseTest.kt b/src/test/kotlin/database/search/SearchDatabaseTest.kt index 8a1f906..8676542 100644 --- a/src/test/kotlin/database/search/SearchDatabaseTest.kt +++ b/src/test/kotlin/database/search/SearchDatabaseTest.kt @@ -64,7 +64,7 @@ class SearchDatabaseTest { } @Test - fun `queryAllDatabaseTable() - given query has a customer and pet match in the database then SearchQueryResultCustomerAndPetSuccess is returned with customer and pet`() { + fun `queryAllDatabaseTable() - given query has a customer and pet match in the database then SearchQueryResultSuccess is returned with customer and pet`() { //given every { mockLocalDatabase.getAllCustomers() } returns hashMapOf( Pair("1", Customer("1", "Albert")), @@ -84,14 +84,15 @@ class SearchDatabaseTest { //then assertThat( result - ).isEqualTo(SearchQueryResult.CustomerAndPetSuccess(listOf(secondCustomerEntity), listOf(secondPetEntity))) + ).isEqualTo(SearchQueryResult.Success(customers = listOf(secondCustomerEntity), pets = listOf(secondPetEntity))) } @Test - fun `queryAllDatabaseTable() - given query has a customer match only in the database then SearchQueryResultCustomerSuccess is returned with customer entity`() { + fun `queryAllDatabaseTable() - given query has a customer match only in the database then SearchQueryResultSuccess is returned with customer entity`() { //given val query = "Albert" every { mockCustomerEntityMapper.map(any(), any()) } returns listOf(customerEntity) + every { mockPetEntityMapper.map(any(), any()) } returns emptyList() //when val result = subject.queryAllDatabaseTable(query) @@ -99,14 +100,15 @@ class SearchDatabaseTest { //then assertThat( result - ).isEqualTo(SearchQueryResult.CustomerSuccess(listOf(customerEntity))) + ).isEqualTo(SearchQueryResult.Success(customers = listOf(customerEntity), pets = emptyList())) } @Test - fun `queryAllDatabaseTable() - given query has a pet match only in the database then SearchQueryResultPetSuccess is returned with pet entity`() { + fun `queryAllDatabaseTable() - given query has a pet match only in the database then SearchQueryResultSuccess is returned with pet entity`() { //given val query = "Johnny" every { mockPetEntityMapper.map(any(), any()) } returns listOf(petEntity) + every { mockCustomerEntityMapper.map(any(), any()) } returns emptyList() //when val result = subject.queryAllDatabaseTable(query) @@ -114,7 +116,7 @@ class SearchDatabaseTest { //then assertThat( result - ).isEqualTo(SearchQueryResult.PetSuccess(listOf(petEntity))) + ).isEqualTo(SearchQueryResult.Success(customers = emptyList(), pets = listOf(petEntity))) } @Test diff --git a/src/test/kotlin/presenter/VeterinaryPracticePresenterTest.kt b/src/test/kotlin/presenter/VeterinaryPracticePresenterTest.kt index 8778d07..f4c7f32 100644 --- a/src/test/kotlin/presenter/VeterinaryPracticePresenterTest.kt +++ b/src/test/kotlin/presenter/VeterinaryPracticePresenterTest.kt @@ -141,40 +141,40 @@ class VeterinaryPracticePresenterTest { every { mockLocalDatabase.query(searchQuery) }.returns( - SearchQueryResult.CustomerAndPetSuccess(customerEntities, petEntities) + SearchQueryResult.Success(customerEntities, petEntities) ) //when subject.search(searchQuery) //then - verify { mockView.showCustomersAndPets(customerEntities, petEntities) } + verify { mockView.showQueryResult(customerEntities, petEntities) } } @Test fun `search() - given query that matches a customer in the database then view customer is printed`() { //given val searchQuery = "sam" - every { mockLocalDatabase.query(searchQuery) }.returns(SearchQueryResult.CustomerSuccess(customerEntities)) + every { mockLocalDatabase.query(searchQuery) }.returns(SearchQueryResult.Success(customerEntities, emptyList())) //when subject.search(searchQuery) //then - verify { mockView.showCustomers(customerEntities) } + verify { mockView.showQueryResult(customers = customerEntities, pets = emptyList()) } } @Test fun `search() - given query that matches a pet in the database then pet is printed`() { //given val searchQuery = "sam" - every { mockLocalDatabase.query(searchQuery) }.returns(SearchQueryResult.PetSuccess(petEntities)) + every { mockLocalDatabase.query(searchQuery) }.returns(SearchQueryResult.Success(emptyList(), petEntities)) //when subject.search(searchQuery) //then - verify { mockView.showPets(petEntities) } + verify { mockView.showQueryResult(emptyList(), petEntities) } } @Test diff --git a/src/test/kotlin/view/VeterinaryPracticeConsoleViewTest.kt b/src/test/kotlin/view/VeterinaryPracticeConsoleViewTest.kt index ff06f99..4ef20cd 100644 --- a/src/test/kotlin/view/VeterinaryPracticeConsoleViewTest.kt +++ b/src/test/kotlin/view/VeterinaryPracticeConsoleViewTest.kt @@ -60,7 +60,7 @@ class VeterinaryPracticeConsoleViewTest { @Test fun `showCustomers() - given customer information then customer details are printed`() { //when - subject.showCustomers(listOf(singleCustomerEntity)) + subject.showQueryResult(customers = listOf(singleCustomerEntity), pets = emptyList()) //then assertThat( @@ -75,7 +75,7 @@ class VeterinaryPracticeConsoleViewTest { @Test fun `showCustomers() - given list of customer information then customer details are printed`() { //when - subject.showCustomers(customerEntities) + subject.showQueryResult(customers = customerEntities, pets = emptyList()) //then assertThat( @@ -94,7 +94,7 @@ class VeterinaryPracticeConsoleViewTest { @Test fun `showPets() - given pet information then the pet details are printed`() { //when - subject.showPets(listOf(singlePetEntity)) + subject.showQueryResult(customers = emptyList(), pets = listOf(singlePetEntity)) //then assertThat( @@ -111,7 +111,7 @@ class VeterinaryPracticeConsoleViewTest { @Test fun `showPets() - given list of pet information then pet details are printed`() { //when - subject.showPets(petEntities) + subject.showQueryResult(customers = emptyList(), pets = petEntities) //then assertThat( @@ -134,7 +134,7 @@ class VeterinaryPracticeConsoleViewTest { @Test fun `showCustomersAndPets() - given customer and pet information then customer and pet details are printed`() { //when - subject.showCustomersAndPets(listOf(singleCustomerEntity), listOf(singlePetEntity)) + subject.showQueryResult(listOf(singleCustomerEntity), listOf(singlePetEntity)) //then assertThat(