Finding a person now definitely only returns a single result.

This commit is contained in:
neviyn 2018-11-01 10:23:25 +00:00
parent e7d4683970
commit 058de81acd
4 changed files with 7 additions and 7 deletions

View File

@ -103,7 +103,7 @@ class Controller {
entries = newObservation.entries,
tutors = tutors,
persons = newObservation.persons.asSequence().map {
personRepository.findByName(it.toUpperCase()) ?: personRepository.save(Person(name = it.toUpperCase()))
personRepository.findFirstByNameLike(it.toUpperCase()) ?: personRepository.save(Person(name = it.toUpperCase()))
}.toSet()
)
observation = observationRepository.save(observation)
@ -133,7 +133,7 @@ class Controller {
}
}.orElse(listOf())
} else if (observationsRequest.person != null && observationsRequest.person.isNotEmpty() && observationsRequest.site != null) {
val person = personRepository.findByName(observationsRequest.person.toUpperCase()) ?: return listOf()
val person = personRepository.findFirstByNameLike(observationsRequest.person.toUpperCase()) ?: return listOf()
val site = siteRepository.findById(observationsRequest.site).get()
return if (observationsRequest.whom == null || observationsRequest.whom.isEmpty())
observationRepository.findBySiteAndPersonsAndDateBetween(site, person, observationsRequest.startDate, observationsRequest.endDate)

View File

@ -29,5 +29,5 @@ interface ObservationRepository : CrudRepository<Observation, Long> {
@Repository
interface PersonRepository : CrudRepository<Person, Long> {
fun findByName(name: String): Person?
fun findFirstByNameLike(name: String): Person?
}

View File

@ -186,7 +186,7 @@ class ControllerTest {
control = 4.0, conservatism = 3.0, teamworkCommunications = 2.0, teamworkLeadership = 3.0,
teamworkWorkload = 1.0, knowledge = 1.0, entries = listOf(), tutors = setOf(tutor), persons = setOf(person))
doReturn(Optional.of(site)).`when`(siteRepository).findById(1)
doReturn(person).`when`(personRepository).findByName("FOO BAR")
doReturn(person).`when`(personRepository).findFirstByNameLike("FOO BAR")
doReturn(listOf(observation)).`when`(observationRepository).findBySiteAndPersonsAndDateBetween(site, person, LocalDate.now(), LocalDate.now())
val result = controller.getObservations(request)
assertEquals(1, result.size)

View File

@ -613,7 +613,7 @@ class RepositoryTest {
fun testFindPersonByName() {
val person = entityManager.persist(Person(name = "Foo Bar"))
entityManager.persist(person)
val result = personRepository.findByName("Foo Bar")
val result = personRepository.findFirstByNameLike("Foo Bar")
assertEquals(person, result)
}
@ -621,14 +621,14 @@ class RepositoryTest {
fun testFindPersonByName_IncorrectName() {
val person = entityManager.persist(Person(name = "Foo Bar"))
entityManager.persist(person)
val result = personRepository.findByName("Foo")
val result = personRepository.findFirstByNameLike("Foo")
assertNotSame(person, result)
assertNull(result)
}
@Test
fun testFindPersonByName_NoPersons() {
val result = personRepository.findByName("Foo")
val result = personRepository.findFirstByNameLike("Foo")
assertNull(result)
}
}