Search in sources :

Example 1 with MyUserPredicatesBuilder

use of org.baeldung.persistence.dao.MyUserPredicatesBuilder in project tutorials by eugenp.

the class UserController method findAllByQuerydsl.

@RequestMapping(method = RequestMethod.GET, value = "/myusers")
@ResponseBody
public Iterable<MyUser> findAllByQuerydsl(@RequestParam(value = "search") String search) {
    MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder();
    if (search != null) {
        Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),");
        Matcher matcher = pattern.matcher(search + ",");
        while (matcher.find()) {
            builder.with(matcher.group(1), matcher.group(2), matcher.group(3));
        }
    }
    BooleanExpression exp = builder.build();
    return myUserRepository.findAll(exp);
}
Also used : Pattern(java.util.regex.Pattern) BooleanExpression(com.querydsl.core.types.dsl.BooleanExpression) MyUserPredicatesBuilder(org.baeldung.persistence.dao.MyUserPredicatesBuilder) Matcher(java.util.regex.Matcher) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 2 with MyUserPredicatesBuilder

use of org.baeldung.persistence.dao.MyUserPredicatesBuilder in project tutorials by eugenp.

the class JPAQuerydslIntegrationTest method givenLastAndAge_whenGettingListOfUsers_thenCorrect.

@Test
public void givenLastAndAge_whenGettingListOfUsers_thenCorrect() {
    final MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder().with("lastName", ":", "doe").with("age", ">", "25");
    final Iterable<MyUser> results = repo.findAll(builder.build());
    assertThat(results, contains(userTom));
    assertThat(results, not(contains(userJohn)));
}
Also used : MyUser(org.baeldung.persistence.model.MyUser) MyUserPredicatesBuilder(org.baeldung.persistence.dao.MyUserPredicatesBuilder) Test(org.junit.Test)

Example 3 with MyUserPredicatesBuilder

use of org.baeldung.persistence.dao.MyUserPredicatesBuilder in project tutorials by eugenp.

the class JPAQuerydslIntegrationTest method givenPartialFirst_whenGettingListOfUsers_thenCorrect.

@Test
public void givenPartialFirst_whenGettingListOfUsers_thenCorrect() {
    final MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder().with("firstName", ":", "jo");
    final Iterable<MyUser> results = repo.findAll(builder.build());
    assertThat(results, contains(userJohn));
    assertThat(results, not(contains(userTom)));
}
Also used : MyUser(org.baeldung.persistence.model.MyUser) MyUserPredicatesBuilder(org.baeldung.persistence.dao.MyUserPredicatesBuilder) Test(org.junit.Test)

Example 4 with MyUserPredicatesBuilder

use of org.baeldung.persistence.dao.MyUserPredicatesBuilder in project tutorials by eugenp.

the class JPAQuerydslIntegrationTest method givenWrongFirstAndLast_whenGettingListOfUsers_thenCorrect.

@Test
public void givenWrongFirstAndLast_whenGettingListOfUsers_thenCorrect() {
    final MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder().with("firstName", ":", "adam").with("lastName", ":", "fox");
    final Iterable<MyUser> results = repo.findAll(builder.build());
    assertThat(results, emptyIterable());
}
Also used : MyUser(org.baeldung.persistence.model.MyUser) MyUserPredicatesBuilder(org.baeldung.persistence.dao.MyUserPredicatesBuilder) Test(org.junit.Test)

Example 5 with MyUserPredicatesBuilder

use of org.baeldung.persistence.dao.MyUserPredicatesBuilder in project tutorials by eugenp.

the class JPAQuerydslIntegrationTest method givenFirstAndLastName_whenGettingListOfUsers_thenCorrect.

@Test
public void givenFirstAndLastName_whenGettingListOfUsers_thenCorrect() {
    final MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder().with("firstName", ":", "john").with("lastName", ":", "doe");
    final Iterable<MyUser> results = repo.findAll(builder.build());
    assertThat(results, contains(userJohn));
    assertThat(results, not(contains(userTom)));
}
Also used : MyUser(org.baeldung.persistence.model.MyUser) MyUserPredicatesBuilder(org.baeldung.persistence.dao.MyUserPredicatesBuilder) Test(org.junit.Test)

Aggregations

MyUserPredicatesBuilder (org.baeldung.persistence.dao.MyUserPredicatesBuilder)6 MyUser (org.baeldung.persistence.model.MyUser)5 Test (org.junit.Test)5 BooleanExpression (com.querydsl.core.types.dsl.BooleanExpression)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1