2

I am working on Angular 2 with a Spring Boot application, and I am passing some search parameters through my UI to SpringBoot RestController where I am using the CrudRepository<User, id> interface for CRUD operations and am using some methods like findByValue(), but in my app I have four search boxes, like name, age, city, and country.

If I fill only two boxes then I would like to search for that given two parameters like Age=22, City=New York then only those people who lives in New York with age 22 should be as result if I add name also like name=James then it should search with name and age 22 and city New York.

How do I achieve this kind of functionality in my SpringBoot with a Angular 2 app?

Repository Class:

public interface UserRepository extends CrudRepository<User, Integer> {
    public List<User> findAll();
    public List<User> findByName(String name);
}

My Controller Code:

@CrossOrigin(origins = "http://localhost:4200", maxAge = 3600)
@RequestMapping(value = "/user/list", method=RequestMethod.POST)
public List<User> getRequestedUsers(@RequestBody User userObject) {

   return userRepository.findByAllParam();
   // I want to write some method here to get data with all or may be 3, 2, or 1 parameters
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
rahul sharma
  • 101
  • 4
  • 10
  • 1
    Use Querydsl to build your custom query [Spring Data JPA](https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#core.extensions.querydsl) and [querydsl tutorial](http://www.baeldung.com/rest-api-search-language-spring-data-querydsl) – Pavan Kumar Jorrigala Feb 18 '18 at 22:27
  • Create a custom implementation of your repo, then you can use the criteria api to achive what you want to do. – Wilder Valera Feb 19 '18 at 17:06

2 Answers2

0

If your controller is simply returning the repository response, you're better off using Spring Data REST:

https://spring.io/guides/gs/accessing-data-rest/

Once setup, you can expose your repository endpoints directly over REST:

@RepositoryRestResource(collectionResourceRel = "users", path = "users")
public interface UserRepository extends PagingAndSortingRepository<User, Long> {

    List<User> findByName(@Param("name") String name);
    List<User> findByNameAndAge(@Param("name") String name, @Param("age") Integer age)
    List<User> findByNameAndAgeAndCity(@Param("name") String name, @Param("age") Integer age, @Param("city") String city);
}

This then takes care of paging, sorting etc also.

You will end up with an API similar to:

curl -i -X POST -H "Content-Type:application/json" -d "{  \"name\" : \"...\",  \"age\" : 0, \"city\" : \"...\" }" http://localhost: 4200/users

In terms of searching with optional parameters, you could manage this on the client side in JavaScript. The following solution doesn't scale but should help you get something working:

function hasValue(id) {
  return document.getElementById("age").value !== ''
}

function doSearch() {
  const nameHasValue = hasValue("name");
  const ageHasValue = hasValue("age");
  const cityHasValue = hasValue("city");

  if (nameHasValue && ageHasValue && cityHasValue) {
    // search by name, age and city
  } else if (nameHasValue && ageHasValue) {
    // search by name and age
  } else if (nameHasValue && cityHasValue) {
    // search by name and city
  } else if (ageHasValue && cityHasValue) {
    // search by age and city
  } else if (nameHasValue) {
    // search by name
  } else if (ageHasValue) {
    // search by age
  } else if (cityHasValue) {
    // search by city
  }
}
timothyclifford
  • 6,799
  • 7
  • 57
  • 85
0
public interface OrdersRepository extends CrudRepository<OrdersModel, Integer> {
    List<OrdersModel> findAllByCityLikeAndPartLike(String city, String part);
}

And if you want searching by city = New York and part = any String:

findAllByCityLikeAndPartLike("New York", "%");

Not exactly problem like yours but I think that will be helpful.