Pending changes exported from your codespace

This commit is contained in:
Rome Li 2023-03-28 08:02:09 +00:00
parent 0962ed7e8f
commit a810e847f6
3 changed files with 14 additions and 4 deletions

View file

@ -93,7 +93,7 @@ class OwnerController {
} }
// find owners by last name // find owners by last name
Page<Owner> ownersResults = findPaginatedForOwnersLastName(page, owner.getLastName()); Page<Owner> ownersResults = findPaginatedForOwnersName(page, owner.getLastName());
if (ownersResults.isEmpty()) { if (ownersResults.isEmpty()) {
// no owners found // no owners found
result.rejectValue("lastName", "notFound", "not found"); result.rejectValue("lastName", "notFound", "not found");
@ -120,6 +120,12 @@ class OwnerController {
return "owners/ownersList"; return "owners/ownersList";
} }
private Page<Owner> findPaginatedForOwnersName(int page, String name) {
int pageSize = 5;
Pageable pageable = PageRequest.of(page - 1, pageSize);
return owners.findByName(name, pageable);
}
private Page<Owner> findPaginatedForOwnersLastName(int page, String lastname) { private Page<Owner> findPaginatedForOwnersLastName(int page, String lastname) {
int pageSize = 5; int pageSize = 5;
Pageable pageable = PageRequest.of(page - 1, pageSize); Pageable pageable = PageRequest.of(page - 1, pageSize);

View file

@ -45,6 +45,10 @@ public interface OwnerRepository extends Repository<Owner, Integer> {
@Transactional(readOnly = true) @Transactional(readOnly = true)
List<PetType> findPetTypes(); List<PetType> findPetTypes();
@Query("SELECT DISTINCT owner FROM Owner owner left join owner.pets WHERE owner.firstName LIKE :name% OR owner.lastName LIKE :name%")
@Transactional(readOnly = true)
Page<Owner> findByName(@Param("name") String name, Pageable pageable);
/** /**
* Retrieve {@link Owner}s from the data store by last name, returning all owners * Retrieve {@link Owner}s from the data store by last name, returning all owners
* whose last name <i>starts</i> with the given name. * whose last name <i>starts</i> with the given name.

View file

@ -139,14 +139,14 @@ class OwnerControllerTests {
@Test @Test
void testProcessFindFormSuccess() throws Exception { void testProcessFindFormSuccess() throws Exception {
Page<Owner> tasks = new PageImpl<Owner>(Lists.newArrayList(george(), new Owner())); Page<Owner> tasks = new PageImpl<Owner>(Lists.newArrayList(george(), new Owner()));
Mockito.when(this.owners.findByLastName(anyString(), any(Pageable.class))).thenReturn(tasks); Mockito.when(this.owners.findByName(anyString(), any(Pageable.class))).thenReturn(tasks);
mockMvc.perform(get("/owners?page=1")).andExpect(status().isOk()).andExpect(view().name("owners/ownersList")); mockMvc.perform(get("/owners?page=1")).andExpect(status().isOk()).andExpect(view().name("owners/ownersList"));
} }
@Test @Test
void testProcessFindFormByLastName() throws Exception { void testProcessFindFormByLastName() throws Exception {
Page<Owner> tasks = new PageImpl<Owner>(Lists.newArrayList(george())); Page<Owner> tasks = new PageImpl<Owner>(Lists.newArrayList(george()));
Mockito.when(this.owners.findByLastName(eq("Franklin"), any(Pageable.class))).thenReturn(tasks); Mockito.when(this.owners.findByName(eq("Franklin"), any(Pageable.class))).thenReturn(tasks);
mockMvc.perform(get("/owners?page=1").param("lastName", "Franklin")) mockMvc.perform(get("/owners?page=1").param("lastName", "Franklin"))
.andExpect(status().is3xxRedirection()) .andExpect(status().is3xxRedirection())
.andExpect(view().name("redirect:/owners/" + TEST_OWNER_ID)); .andExpect(view().name("redirect:/owners/" + TEST_OWNER_ID));
@ -155,7 +155,7 @@ class OwnerControllerTests {
@Test @Test
void testProcessFindFormNoOwnersFound() throws Exception { void testProcessFindFormNoOwnersFound() throws Exception {
Page<Owner> tasks = new PageImpl<Owner>(Lists.newArrayList()); Page<Owner> tasks = new PageImpl<Owner>(Lists.newArrayList());
Mockito.when(this.owners.findByLastName(eq("Unknown Surname"), any(Pageable.class))).thenReturn(tasks); Mockito.when(this.owners.findByName(eq("Unknown Surname"), any(Pageable.class))).thenReturn(tasks);
mockMvc.perform(get("/owners?page=1").param("lastName", "Unknown Surname")) mockMvc.perform(get("/owners?page=1").param("lastName", "Unknown Surname"))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(model().attributeHasFieldErrors("owner", "lastName")) .andExpect(model().attributeHasFieldErrors("owner", "lastName"))