diff --git a/pom.xml b/pom.xml index 13a5a94cf..d305a8811 100644 --- a/pom.xml +++ b/pom.xml @@ -71,6 +71,12 @@ + + org.apache.commons + commons-lang3 + 3.3.2 + + org.jadira.usertype usertype.core diff --git a/src/main/java/org/springframework/samples/petclinic/repository/PetRepository.java b/src/main/java/org/springframework/samples/petclinic/repository/PetRepository.java index c47e72ff4..9998af900 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/PetRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/PetRepository.java @@ -15,6 +15,7 @@ */ package org.springframework.samples.petclinic.repository; +import java.util.Collection; import java.util.List; import org.springframework.dao.DataAccessException; @@ -63,6 +64,13 @@ public interface PetRepository { * @return * */ - List findAll() throws DataAccessException; + Collection findAll() throws DataAccessException; + + /** + * Get all pets by name + * @return + * + */ + Collection findByName(String query) throws DataAccessException; } diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetRepositoryImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetRepositoryImpl.java index f15bff055..4bbd7d002 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetRepositoryImpl.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetRepositoryImpl.java @@ -17,6 +17,7 @@ package org.springframework.samples.petclinic.repository.jdbc; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -155,4 +156,28 @@ public class JdbcPetRepositoryImpl implements PetRepository { }); } + @Override + public Collection findByName(String query) throws DataAccessException { + + Map paramMap = new HashMap(); + paramMap.put("query","%" + query + "%"); + + return this.namedParameterJdbcTemplate.query("SELECT * FROM pets WHERE name LIKE :query", paramMap, new RowMapper(){ + + @Override + public Pet mapRow(ResultSet rs, int idx) throws SQLException { + Pet pet = new Pet(); + PetType type = new PetType(); + type.setId(rs.getInt("type_id")); + pet.setName(rs.getString("name")); + pet.setId(rs.getInt("id")); + pet.setBirthDate(new DateTime(rs.getDate("birth_date"))); + pet.setType(type); + + return pet; + } + + }); + } + } diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaPetRepositoryImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaPetRepositoryImpl.java index 9173869ed..6ba194ef0 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaPetRepositoryImpl.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaPetRepositoryImpl.java @@ -15,6 +15,7 @@ */ package org.springframework.samples.petclinic.repository.jpa; +import java.util.Collection; import java.util.List; import javax.persistence.EntityManager; @@ -64,8 +65,14 @@ public class JpaPetRepositoryImpl implements PetRepository { @SuppressWarnings("unchecked") @Override - public List findAll() throws DataAccessException { + public Collection findAll() throws DataAccessException { return this.em.createQuery("SELECT pet FROM Pet pet").getResultList(); } + @SuppressWarnings("unchecked") + @Override + public Collection findByName(String query) throws DataAccessException { + return this.em.createQuery("SELECT pet FROM Pet pet WHERE pet.name like '%" + query + "%'").getResultList(); + } + } diff --git a/src/main/java/org/springframework/samples/petclinic/rest/PetRestController.java b/src/main/java/org/springframework/samples/petclinic/rest/PetRestController.java index 603255618..b32f44c0e 100644 --- a/src/main/java/org/springframework/samples/petclinic/rest/PetRestController.java +++ b/src/main/java/org/springframework/samples/petclinic/rest/PetRestController.java @@ -26,6 +26,7 @@ import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; /** @@ -61,4 +62,8 @@ public class PetRestController { return this.clinicService.findPets(); } + @RequestMapping(value = "/pets/search", method = RequestMethod.GET) + public Collection search(@RequestParam String q) { + return this.clinicService.findPetByName(q); + } } diff --git a/src/main/java/org/springframework/samples/petclinic/service/ClinicService.java b/src/main/java/org/springframework/samples/petclinic/service/ClinicService.java index 4221fe531..67e2d353f 100644 --- a/src/main/java/org/springframework/samples/petclinic/service/ClinicService.java +++ b/src/main/java/org/springframework/samples/petclinic/service/ClinicService.java @@ -16,7 +16,6 @@ package org.springframework.samples.petclinic.service; import java.util.Collection; -import java.util.List; import org.springframework.dao.DataAccessException; import org.springframework.samples.petclinic.model.Owner; @@ -38,6 +37,8 @@ public interface ClinicService { Owner findOwnerById(int id) throws DataAccessException; Pet findPetById(int id) throws DataAccessException; + + Collection findPetByName(String name) throws DataAccessException; void savePet(Pet pet) throws DataAccessException; @@ -49,6 +50,6 @@ public interface ClinicService { Collection findOwnerByLastName(String lastName) throws DataAccessException; - List findPets() throws DataAccessException; + Collection findPets() throws DataAccessException; } diff --git a/src/main/java/org/springframework/samples/petclinic/service/ClinicServiceImpl.java b/src/main/java/org/springframework/samples/petclinic/service/ClinicServiceImpl.java index 012c9fe46..baadc0740 100644 --- a/src/main/java/org/springframework/samples/petclinic/service/ClinicServiceImpl.java +++ b/src/main/java/org/springframework/samples/petclinic/service/ClinicServiceImpl.java @@ -16,7 +16,6 @@ package org.springframework.samples.petclinic.service; import java.util.Collection; -import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.Cacheable; @@ -109,7 +108,12 @@ public class ClinicServiceImpl implements ClinicService { @Override @Transactional(readOnly = true) @Cacheable(value = "pets") - public List findPets() throws DataAccessException { + public Collection findPets() throws DataAccessException { return petRepository.findAll(); } + + @Override + public Collection findPetByName(String name) throws DataAccessException { + return petRepository.findByName(name); + } } diff --git a/src/main/webapp/components/_partials/SearchController.js b/src/main/webapp/components/_partials/SearchController.js new file mode 100644 index 000000000..2eaada145 --- /dev/null +++ b/src/main/webapp/components/_partials/SearchController.js @@ -0,0 +1,13 @@ +var SearchController = ['$scope','$http','$timeout','context', function($scope,$http,$timeout,context) { + $scope.results = []; + $scope.searchText = ''; + $scope.$watch('searchText', function (val) { + if(val != '') { + $http.get(context + '/api/pets/search', { params : { q : val } }).then(function(response) { + $scope.results = response.data; + }) + } else { + $scope.results = []; + } + }) +}]; \ No newline at end of file diff --git a/src/main/webapp/components/_partials/_modal_search.html b/src/main/webapp/components/_partials/_modal_search.html index 9ee758d46..323592cb8 100644 --- a/src/main/webapp/components/_partials/_modal_search.html +++ b/src/main/webapp/components/_partials/_modal_search.html @@ -1,19 +1,19 @@ -