mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-17 21:35:50 +00:00
LH :: Enabled search feature
This commit is contained in:
parent
c790a4ba0a
commit
d125f5d478
12 changed files with 89 additions and 19 deletions
6
pom.xml
6
pom.xml
|
@ -71,6 +71,12 @@
|
|||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>3.3.2</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.jadira.usertype</groupId>
|
||||
<artifactId>usertype.core</artifactId>
|
||||
|
|
|
@ -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<Pet> findAll() throws DataAccessException;
|
||||
Collection<Pet> findAll() throws DataAccessException;
|
||||
|
||||
/**
|
||||
* Get all pets by name
|
||||
* @return
|
||||
*
|
||||
*/
|
||||
Collection<Pet> findByName(String query) throws DataAccessException;
|
||||
|
||||
}
|
||||
|
|
|
@ -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<Pet> findByName(String query) throws DataAccessException {
|
||||
|
||||
Map<String, String> paramMap = new HashMap<String, String>();
|
||||
paramMap.put("query","%" + query + "%");
|
||||
|
||||
return this.namedParameterJdbcTemplate.query("SELECT * FROM pets WHERE name LIKE :query", paramMap, new RowMapper<Pet>(){
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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<Pet> findAll() throws DataAccessException {
|
||||
public Collection<Pet> findAll() throws DataAccessException {
|
||||
return this.em.createQuery("SELECT pet FROM Pet pet").getResultList();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Collection<Pet> findByName(String query) throws DataAccessException {
|
||||
return this.em.createQuery("SELECT pet FROM Pet pet WHERE pet.name like '%" + query + "%'").getResultList();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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<Pet> search(@RequestParam String q) {
|
||||
return this.clinicService.findPetByName(q);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<Pet> findPetByName(String name) throws DataAccessException;
|
||||
|
||||
void savePet(Pet pet) throws DataAccessException;
|
||||
|
||||
|
@ -49,6 +50,6 @@ public interface ClinicService {
|
|||
|
||||
Collection<Owner> findOwnerByLastName(String lastName) throws DataAccessException;
|
||||
|
||||
List<Pet> findPets() throws DataAccessException;
|
||||
Collection<Pet> findPets() throws DataAccessException;
|
||||
|
||||
}
|
||||
|
|
|
@ -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<Pet> findPets() throws DataAccessException {
|
||||
public Collection<Pet> findPets() throws DataAccessException {
|
||||
return petRepository.findAll();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Pet> findPetByName(String name) throws DataAccessException {
|
||||
return petRepository.findByName(name);
|
||||
}
|
||||
}
|
||||
|
|
13
src/main/webapp/components/_partials/SearchController.js
Normal file
13
src/main/webapp/components/_partials/SearchController.js
Normal file
|
@ -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 = [];
|
||||
}
|
||||
})
|
||||
}];
|
|
@ -1,19 +1,19 @@
|
|||
<!-- Search Result Modal -->
|
||||
<div class="modal modal-search fade" id="searchModal" tabindex="-1">
|
||||
<div class="modal modal-search fade" id="searchModal" tabindex="-1" data-ng-controller="SearchController">
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
|
||||
<div class="container">
|
||||
<h1 class="sub-header">Search PetClinic</h1>
|
||||
<em class="text-muted">Start typing to search for owner, pets, or veterinarians...</em>
|
||||
<em class="text-muted">Start typing to search for pets ...</em>
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<div class="container">
|
||||
<form>
|
||||
<div class="form-group">
|
||||
<input type="search" class="form-control">
|
||||
<input type="search" class="form-control" data-ng-model="searchText" data-ng-model-options="{debounce: 1000}">
|
||||
<button class="btn btn-default" type="button">
|
||||
<span class="glyphicon glyphicon-search" aria-hidden="true"></span>
|
||||
</button>
|
||||
|
@ -26,15 +26,14 @@
|
|||
<div class="modal-seach-result">
|
||||
<h2 class="form-group-heading">Result of your search query</h2>
|
||||
<div class="row thumbnail-wrapper">
|
||||
<div class="col-md-3" ng-repeat="x in [1,2,3,4,5,6,7]">
|
||||
<div class="thumbnail">
|
||||
<a href="#"><img src="images/owner-default.png" class="img-circle" alt="Generic placeholder image"></a>
|
||||
<div class="caption">
|
||||
<h3 class="caption-heading"><a href="#">Eduardo Rodriguez</a></h3>
|
||||
<p class="caption-meta">2693 Commerce St., McFarland</p>
|
||||
<p class="caption-meta"><span class="glyphicon glyphicon-phone-alt"></span> +6085558763</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-3" data-ng-repeat="pet in results">
|
||||
<div class="thumbnail">
|
||||
<a data-ui-sref="pets({id : pet.id})"><img data-ng-src="images/pets/pet{{pet.id % 10 + 1}}.jpg" alt="Generic placeholder image" class="img-circle"></a>
|
||||
<div class="caption">
|
||||
<h3 class="caption-heading"><a href="#" data-ng-bind="pet.name"></a></h3>
|
||||
<p data-ng-bind="pet.type.name"></p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<!-- /row -->
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
<div class="container">
|
||||
<h2 class="sub-header">Owners</h2>
|
||||
<div class="row thumbnail-wrapper">
|
||||
<div class="col-md-3" ng-repeat="owner in owners | limitTo : 8">
|
||||
<div class="col-md-3" data-ng-repeat="owner in owners | limitTo : 8">
|
||||
<div class="thumbnail">
|
||||
<a data-ui-sref="ownerDetails({id: owner.id})"><img data-ng-src="images/avatars/owner{{owner.id}}.jpg" class="img-circle" alt="Generic placeholder image"></a>
|
||||
<div class="caption">
|
||||
|
|
|
@ -52,6 +52,7 @@
|
|||
<script src="components/owners/OwnerController.js"></script>
|
||||
<script src="components/visits/VisitController.js"></script>
|
||||
<script src="components/dashboard/DashboardController.js"></script>
|
||||
<script src="components/_partials/SearchController.js"></script>
|
||||
<script src="js/app.js"></script>
|
||||
<!-- endbuild -->
|
||||
</body>
|
||||
|
|
|
@ -61,6 +61,7 @@ app.controller('OwnerController', OwnerController);
|
|||
app.controller('OwnerDetailsController', OwnerDetailsController);
|
||||
app.controller('AddOwnerController', AddOwnerController);
|
||||
app.controller('VisitController', VisitController);
|
||||
app.controller('SearchController', SearchController);
|
||||
|
||||
/** Services **/
|
||||
app.factory('Owner', Owner);
|
||||
|
|
Loading…
Reference in a new issue