From e3a5dd7a46d4506de923bd99a8d93ce59f6cf409 Mon Sep 17 00:00:00 2001 From: Nathan Cannon Date: Thu, 22 Nov 2018 16:40:32 +0000 Subject: [PATCH] Queries are now ordered by date explicitly. --- .../neviyn/observationdatabase/Controller.kt | 12 ++++++------ .../neviyn/observationdatabase/Repository.kt | 12 ++++++------ .../observationdatabase/ControllerTest.kt | 10 +++++----- .../observationdatabase/RepositoryTest.kt | 18 +++++++++--------- 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/Controller.kt b/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/Controller.kt index 756fc4d..a51f41a 100644 --- a/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/Controller.kt +++ b/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/Controller.kt @@ -123,10 +123,10 @@ class Controller { if (observationsRequest.tutor != null) { return tutorRepository.findById(observationsRequest.tutor).map { when { - (observationsRequest.whom == null || observationsRequest.whom.isEmpty()) -> observationRepository.findByTutorsAndDateBetween(tutor = it, + (observationsRequest.whom == null || observationsRequest.whom.isEmpty()) -> observationRepository.findByTutorsAndDateBetweenOrderByDateAsc(tutor = it, startDate = observationsRequest.startDate, endDate = observationsRequest.endDate) - else -> observationRepository.findByTutorsAndWhomIgnoreCaseAndDateBetween(tutor = it, + else -> observationRepository.findByTutorsAndWhomIgnoreCaseAndDateBetweenOrderByDateAsc(tutor = it, whom = observationsRequest.whom, startDate = observationsRequest.startDate, endDate = observationsRequest.endDate) @@ -136,16 +136,16 @@ class Controller { 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) + observationRepository.findBySiteAndPersonsAndDateBetweenOrderByDateAsc(site, person, observationsRequest.startDate, observationsRequest.endDate) else - observationRepository.findBySiteAndWhomIgnoreCaseAndPersonsAndDateBetween(site, observationsRequest.whom, person, observationsRequest.startDate, observationsRequest.endDate) + observationRepository.findBySiteAndWhomIgnoreCaseAndPersonsAndDateBetweenOrderByDateAsc(site, observationsRequest.whom, person, observationsRequest.startDate, observationsRequest.endDate) } else if (observationsRequest.site != null) { return siteRepository.findById(observationsRequest.site).map { when { - (observationsRequest.whom == null || observationsRequest.whom.isEmpty()) -> observationRepository.findBySiteAndDateBetween(site = it, + (observationsRequest.whom == null || observationsRequest.whom.isEmpty()) -> observationRepository.findBySiteAndDateBetweenOrderByDateAsc(site = it, startDate = observationsRequest.startDate, endDate = observationsRequest.endDate) - else -> observationRepository.findBySiteAndWhomIgnoreCaseAndDateBetween(site = it, + else -> observationRepository.findBySiteAndWhomIgnoreCaseAndDateBetweenOrderByDateAsc(site = it, whom = observationsRequest.whom, startDate = observationsRequest.startDate, endDate = observationsRequest.endDate) diff --git a/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/Repository.kt b/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/Repository.kt index ef80fcf..b1fc041 100644 --- a/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/Repository.kt +++ b/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/Repository.kt @@ -13,17 +13,17 @@ interface TutorRepository : CrudRepository @Repository interface ObservationRepository : CrudRepository { - fun findBySiteAndDateBetween(site: Site, startDate: LocalDate, endDate: LocalDate): List + fun findBySiteAndDateBetweenOrderByDateAsc(site: Site, startDate: LocalDate, endDate: LocalDate): List - fun findByTutorsAndDateBetween(tutor: Tutor, startDate: LocalDate, endDate: LocalDate): List + fun findByTutorsAndDateBetweenOrderByDateAsc(tutor: Tutor, startDate: LocalDate, endDate: LocalDate): List - fun findBySiteAndWhomIgnoreCaseAndDateBetween(site: Site, whom: String, startDate: LocalDate, endDate: LocalDate): List + fun findBySiteAndWhomIgnoreCaseAndDateBetweenOrderByDateAsc(site: Site, whom: String, startDate: LocalDate, endDate: LocalDate): List - fun findByTutorsAndWhomIgnoreCaseAndDateBetween(tutor: Tutor, whom: String, startDate: LocalDate, endDate: LocalDate): List + fun findByTutorsAndWhomIgnoreCaseAndDateBetweenOrderByDateAsc(tutor: Tutor, whom: String, startDate: LocalDate, endDate: LocalDate): List - fun findBySiteAndPersonsAndDateBetween(site: Site, person: Person, startDate: LocalDate, endDate: LocalDate): List + fun findBySiteAndPersonsAndDateBetweenOrderByDateAsc(site: Site, person: Person, startDate: LocalDate, endDate: LocalDate): List - fun findBySiteAndWhomIgnoreCaseAndPersonsAndDateBetween(site: Site, whom: String, person: Person, startDate: LocalDate, endDate: LocalDate): List + fun findBySiteAndWhomIgnoreCaseAndPersonsAndDateBetweenOrderByDateAsc(site: Site, whom: String, person: Person, startDate: LocalDate, endDate: LocalDate): List } @Repository diff --git a/backend/src/test/kotlin/uk/co/neviyn/observationdatabase/ControllerTest.kt b/backend/src/test/kotlin/uk/co/neviyn/observationdatabase/ControllerTest.kt index 5f2b3a9..00c7cd0 100644 --- a/backend/src/test/kotlin/uk/co/neviyn/observationdatabase/ControllerTest.kt +++ b/backend/src/test/kotlin/uk/co/neviyn/observationdatabase/ControllerTest.kt @@ -119,7 +119,7 @@ class ControllerTest { teamworkWorkload = 1.0, knowledge = 1.0, entries = listOf(), tutors = setOf(), persons = setOf()) doReturn(Optional.of(site)).`when`(siteRepository).findById(1) doReturn(listOf(observation)) - .`when`(observationRepository).findBySiteAndDateBetween(site, LocalDate.now(), LocalDate.now()) + .`when`(observationRepository).findBySiteAndDateBetweenOrderByDateAsc(site, LocalDate.now(), LocalDate.now()) val result = controller.getObservations(request) assertEquals(1, result.size) assertEquals(observation, result[0]) @@ -135,7 +135,7 @@ class ControllerTest { teamworkWorkload = 1.0, knowledge = 1.0, entries = listOf(), tutors = setOf(), persons = setOf()) doReturn(Optional.of(site)).`when`(siteRepository).findById(1) doReturn(listOf(observation)) - .`when`(observationRepository).findBySiteAndWhomIgnoreCaseAndDateBetween(site, "Group A", LocalDate.now(), LocalDate.now()) + .`when`(observationRepository).findBySiteAndWhomIgnoreCaseAndDateBetweenOrderByDateAsc(site, "Group A", LocalDate.now(), LocalDate.now()) val result = controller.getObservations(request) assertEquals(1, result.size) assertEquals(observation, result[0]) @@ -152,7 +152,7 @@ class ControllerTest { teamworkWorkload = 1.0, knowledge = 1.0, entries = listOf(), tutors = setOf(tutor), persons = setOf()) doReturn(Optional.of(tutor)).`when`(tutorRepository).findById(1) doReturn(listOf(observation)) - .`when`(observationRepository).findByTutorsAndDateBetween(tutor, LocalDate.now(), LocalDate.now()) + .`when`(observationRepository).findByTutorsAndDateBetweenOrderByDateAsc(tutor, LocalDate.now(), LocalDate.now()) val result = controller.getObservations(request) assertEquals(1, result.size) assertEquals(observation, result[0]) @@ -169,7 +169,7 @@ class ControllerTest { teamworkWorkload = 1.0, knowledge = 1.0, entries = listOf(), tutors = setOf(tutor), persons = setOf()) doReturn(Optional.of(tutor)).`when`(tutorRepository).findById(1) doReturn(listOf(observation)) - .`when`(observationRepository).findByTutorsAndWhomIgnoreCaseAndDateBetween(tutor, "Group A", LocalDate.now(), LocalDate.now()) + .`when`(observationRepository).findByTutorsAndWhomIgnoreCaseAndDateBetweenOrderByDateAsc(tutor, "Group A", LocalDate.now(), LocalDate.now()) val result = controller.getObservations(request) assertEquals(1, result.size) assertEquals(observation, result[0]) @@ -187,7 +187,7 @@ class ControllerTest { 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).findFirstByNameLike("FOO BAR") - doReturn(listOf(observation)).`when`(observationRepository).findBySiteAndPersonsAndDateBetween(site, person, LocalDate.now(), LocalDate.now()) + doReturn(listOf(observation)).`when`(observationRepository).findBySiteAndPersonsAndDateBetweenOrderByDateAsc(site, person, LocalDate.now(), LocalDate.now()) val result = controller.getObservations(request) assertEquals(1, result.size) assertEquals(observation, result[0]) diff --git a/backend/src/test/kotlin/uk/co/neviyn/observationdatabase/RepositoryTest.kt b/backend/src/test/kotlin/uk/co/neviyn/observationdatabase/RepositoryTest.kt index 27abfa4..9d2d85e 100644 --- a/backend/src/test/kotlin/uk/co/neviyn/observationdatabase/RepositoryTest.kt +++ b/backend/src/test/kotlin/uk/co/neviyn/observationdatabase/RepositoryTest.kt @@ -24,7 +24,7 @@ class RepositoryTest { @Test fun testFindBySiteAndDateBetween_EmptyRepository() { - val result = observationRepository.findBySiteAndDateBetween(Site( + val result = observationRepository.findBySiteAndDateBetweenOrderByDateAsc(Site( id = 1, name = "x" ), LocalDate.now(), LocalDate.now()) @@ -33,7 +33,7 @@ class RepositoryTest { @Test fun testFindBySiteAndWhomIgnoreCaseAndDateBetween_EmptyRepository() { - val result = observationRepository.findBySiteAndWhomIgnoreCaseAndDateBetween(Site( + val result = observationRepository.findBySiteAndWhomIgnoreCaseAndDateBetweenOrderByDateAsc(Site( id = 1, name = "x" ), "none", LocalDate.now(), LocalDate.now()) @@ -42,7 +42,7 @@ class RepositoryTest { @Test fun testFindByTutorsAndDateBetween_EmptyRepository() { - val result = observationRepository.findByTutorsAndDateBetween(Tutor( + val result = observationRepository.findByTutorsAndDateBetweenOrderByDateAsc(Tutor( id = 1, name = "x", site = Site(1, "x") @@ -52,7 +52,7 @@ class RepositoryTest { @Test fun testFindByTutorsAndWhomIgnoreCaseAndDateBetween_EmptyRepository() { - val result = observationRepository.findByTutorsAndWhomIgnoreCaseAndDateBetween(Tutor( + val result = observationRepository.findByTutorsAndWhomIgnoreCaseAndDateBetweenOrderByDateAsc(Tutor( id = 1, name = "x", site = Site(1, "x") @@ -143,7 +143,7 @@ class RepositoryTest { entityManager.persist(tooLate) entityManager.persist(wrongSite) entityManager.persist(justRight) - val result = observationRepository.findBySiteAndDateBetween( + val result = observationRepository.findBySiteAndDateBetweenOrderByDateAsc( site = correctSite, startDate = LocalDate.now().minusDays(1), endDate = LocalDate.now().plusDays(1) @@ -254,7 +254,7 @@ class RepositoryTest { entityManager.persist(wrongSite) entityManager.persist(justRight) entityManager.persist(wrongWhom) - val result = observationRepository.findBySiteAndWhomIgnoreCaseAndDateBetween( + val result = observationRepository.findBySiteAndWhomIgnoreCaseAndDateBetweenOrderByDateAsc( site = correctSite, startDate = LocalDate.now().minusDays(1), endDate = LocalDate.now().plusDays(1), @@ -369,7 +369,7 @@ class RepositoryTest { entityManager.persist(correctTutor) incorrectTutor.observations.addAll(listOf(wrongTutor, justRightMultipleTutors)) entityManager.persist(incorrectTutor) - val result = observationRepository.findByTutorsAndDateBetween( + val result = observationRepository.findByTutorsAndDateBetweenOrderByDateAsc( tutor = correctTutor, startDate = LocalDate.now().minusDays(1), endDate = LocalDate.now().plusDays(1) @@ -504,7 +504,7 @@ class RepositoryTest { entityManager.persist(correctTutor) incorrectTutor.observations.addAll(listOf(wrongTutor, justRightMultipleTutors)) entityManager.persist(incorrectTutor) - val result = observationRepository.findByTutorsAndWhomIgnoreCaseAndDateBetween( + val result = observationRepository.findByTutorsAndWhomIgnoreCaseAndDateBetweenOrderByDateAsc( tutor = correctTutor, startDate = LocalDate.now().minusDays(1), endDate = LocalDate.now().plusDays(1), @@ -599,7 +599,7 @@ class RepositoryTest { entityManager.persist(tooLate) entityManager.persist(wrongPerson) entityManager.persist(justRight) - val result = observationRepository.findBySiteAndPersonsAndDateBetween( + val result = observationRepository.findBySiteAndPersonsAndDateBetweenOrderByDateAsc( site = site, person = person, startDate = LocalDate.now().minusDays(1),