mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-04-24 11:22:49 +00:00
Compare commits
10 commits
3dc92bb2f1
...
972573f141
Author | SHA1 | Date | |
---|---|---|---|
![]() |
972573f141 | ||
![]() |
d7cc02f3d7 | ||
![]() |
fceca7754a | ||
![]() |
7f1af268ec | ||
![]() |
e15623d232 | ||
![]() |
2ab1cef91f | ||
![]() |
1615fda9b2 | ||
![]() |
6bc5bd4af6 | ||
![]() |
98ccc1e2e4 | ||
![]() |
94d6335022 |
8 changed files with 19 additions and 17 deletions
|
@ -110,9 +110,9 @@ class OwnerController {
|
|||
owner = ownersResults.iterator().next();
|
||||
return "redirect:/owners/" + owner.getId();
|
||||
}
|
||||
|
||||
// multiple owners found
|
||||
return addPaginationModel(page, model, ownersResults);
|
||||
|
||||
}
|
||||
|
||||
private String addPaginationModel(int page, Model model, Page<Owner> paginated) {
|
||||
|
|
|
@ -24,6 +24,9 @@ import org.springframework.data.domain.Pageable;
|
|||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* Repository class for <code>Owner</code> domain objects All method names are compliant
|
||||
* with Spring Data naming conventions so this interface can easily be extended for Spring
|
||||
|
@ -52,7 +55,9 @@ public interface OwnerRepository extends JpaRepository<Owner, Integer> {
|
|||
* @return a Collection of matching {@link Owner}s (or an empty Collection if none
|
||||
* found)
|
||||
*/
|
||||
Page<Owner> findByLastNameStartingWith(String lastName, Pageable pageable);
|
||||
|
||||
@Query("SELECT o FROM Owner o WHERE LOWER(CONCAT(o.firstName, ' ', o.lastName)) LIKE LOWER(CONCAT('%', :namePart, '%'))")
|
||||
Page<Owner> findByLastNameStartingWith(@Param("namePart") String namePart, Pageable pageable);
|
||||
|
||||
/**
|
||||
* Retrieve an {@link Owner} from the data store by id.
|
||||
|
|
|
@ -15,7 +15,6 @@
|
|||
*/
|
||||
package org.springframework.samples.petclinic.owner;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.format.Formatter;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
|
@ -38,7 +37,6 @@ public class PetTypeFormatter implements Formatter<PetType> {
|
|||
|
||||
private final OwnerRepository owners;
|
||||
|
||||
@Autowired
|
||||
public PetTypeFormatter(OwnerRepository owners) {
|
||||
this.owners = owners;
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
<div th:with="valid=${!#fields.hasErrors(name)}"
|
||||
th:class="${'form-group' + (valid ? '' : ' has-error')}"
|
||||
class="form-group">
|
||||
<label class="col-sm-2 control-label" th:text="${label}">Label</label>
|
||||
<label th:for="${name}" class="col-sm-2 control-label" th:text="${label}">Label</label>
|
||||
<div class="col-sm-10">
|
||||
<div th:switch="${type}">
|
||||
<input th:case="'text'" class="form-control" type="text" th:field="*{__${name}__}" />
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
<div th:with="valid=${!#fields.hasErrors(name)}"
|
||||
th:class="${'form-group' + (valid ? '' : ' has-error')}"
|
||||
class="form-group">
|
||||
<label class="col-sm-2 control-label" th:text="${label}">Label</label>
|
||||
<label th:for="${name}" class="col-sm-2 control-label" th:text="${label}">Label</label>
|
||||
|
||||
<div class="col-sm-10">
|
||||
<select th:field="*{__${name}__}">
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
class="form-horizontal" id="search-owner-form">
|
||||
<div class="form-group">
|
||||
<div class="control-group" id="lastNameGroup">
|
||||
<label class="col-sm-2 control-label">Last name </label>
|
||||
<label class="col-sm-2 control-label">Search by Name</label>
|
||||
<div class="col-sm-10">
|
||||
<input class="form-control" th:field="*{lastName}" size="30"
|
||||
maxlength="80" /> <span class="help-inline"><div
|
||||
|
@ -21,8 +21,7 @@
|
|||
</div>
|
||||
<div class="form-group">
|
||||
<div class="col-sm-offset-2 col-sm-10">
|
||||
<button type="submit" class="btn btn-primary">Find
|
||||
Owner</button>
|
||||
<button type="submit" class="btn btn-primary">Search</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
|
|
@ -16,7 +16,6 @@
|
|||
|
||||
package org.springframework.samples.petclinic.owner;
|
||||
|
||||
import org.assertj.core.util.Lists;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.condition.DisabledInNativeImage;
|
||||
|
@ -31,6 +30,7 @@ import org.springframework.test.web.servlet.MockMvc;
|
|||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.hamcrest.Matchers.empty;
|
||||
|
@ -92,9 +92,9 @@ class OwnerControllerTests {
|
|||
|
||||
Owner george = george();
|
||||
given(this.owners.findByLastNameStartingWith(eq("Franklin"), any(Pageable.class)))
|
||||
.willReturn(new PageImpl<>(Lists.newArrayList(george)));
|
||||
.willReturn(new PageImpl<>(List.of(george)));
|
||||
|
||||
given(this.owners.findAll(any(Pageable.class))).willReturn(new PageImpl<>(Lists.newArrayList(george)));
|
||||
given(this.owners.findAll(any(Pageable.class))).willReturn(new PageImpl<>(List.of(george)));
|
||||
|
||||
given(this.owners.findById(TEST_OWNER_ID)).willReturn(Optional.of(george));
|
||||
Visit visit = new Visit();
|
||||
|
@ -143,14 +143,14 @@ class OwnerControllerTests {
|
|||
|
||||
@Test
|
||||
void testProcessFindFormSuccess() throws Exception {
|
||||
Page<Owner> tasks = new PageImpl<>(Lists.newArrayList(george(), new Owner()));
|
||||
Page<Owner> tasks = new PageImpl<>(List.of(george(), new Owner()));
|
||||
when(this.owners.findByLastNameStartingWith(anyString(), any(Pageable.class))).thenReturn(tasks);
|
||||
mockMvc.perform(get("/owners?page=1")).andExpect(status().isOk()).andExpect(view().name("owners/ownersList"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testProcessFindFormByLastName() throws Exception {
|
||||
Page<Owner> tasks = new PageImpl<>(Lists.newArrayList(george()));
|
||||
Page<Owner> tasks = new PageImpl<>(List.of(george()));
|
||||
when(this.owners.findByLastNameStartingWith(eq("Franklin"), any(Pageable.class))).thenReturn(tasks);
|
||||
mockMvc.perform(get("/owners?page=1").param("lastName", "Franklin"))
|
||||
.andExpect(status().is3xxRedirection())
|
||||
|
@ -159,7 +159,7 @@ class OwnerControllerTests {
|
|||
|
||||
@Test
|
||||
void testProcessFindFormNoOwnersFound() throws Exception {
|
||||
Page<Owner> tasks = new PageImpl<>(Lists.newArrayList());
|
||||
Page<Owner> tasks = new PageImpl<>(List.of());
|
||||
when(this.owners.findByLastNameStartingWith(eq("Unknown Surname"), any(Pageable.class))).thenReturn(tasks);
|
||||
mockMvc.perform(get("/owners?page=1").param("lastName", "Unknown Surname"))
|
||||
.andExpect(status().isOk())
|
||||
|
|
|
@ -16,7 +16,6 @@
|
|||
|
||||
package org.springframework.samples.petclinic.owner;
|
||||
|
||||
import org.assertj.core.util.Lists;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
@ -30,6 +29,7 @@ import org.springframework.test.context.bean.override.mockito.MockitoBean;
|
|||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.mockito.BDDMockito.given;
|
||||
|
@ -66,7 +66,7 @@ class PetControllerTests {
|
|||
PetType cat = new PetType();
|
||||
cat.setId(3);
|
||||
cat.setName("hamster");
|
||||
given(this.owners.findPetTypes()).willReturn(Lists.newArrayList(cat));
|
||||
given(this.owners.findPetTypes()).willReturn(List.of(cat));
|
||||
|
||||
Owner owner = new Owner();
|
||||
Pet pet = new Pet();
|
||||
|
|
Loading…
Reference in a new issue