Skip to content

Commit

Permalink
list cars by name
Browse files Browse the repository at this point in the history
  • Loading branch information
rmpestano committed Dec 26, 2014
1 parent 2b91154 commit 6f86c59
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 8 deletions.
10 changes: 8 additions & 2 deletions src/main/java/com/cdi/crud/rest/CarEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,24 @@ public Response findById(@PathParam("id") Integer id) {
* @param minPrice minimum car price
* @param maxPrice maximum car price
* @param model list cars with given model
* @param name list cars with given name
*/
@GET
@Path("list")
public List<Car> list(@QueryParam("start") @DefaultValue("0") Integer startPosition,
@QueryParam("max") @DefaultValue("10") Integer maxResult,
@QueryParam("model") String model,
@QueryParam("name") String name,
@QueryParam("minPrice") @DefaultValue("0") Double minPrice,
@QueryParam("maxPrice") @DefaultValue("20000") Double maxPrice) {
Filter<Car> filter = new Filter<>();
Car car = new Car();
filter.setEntity(car);
if(model != null){
Car car = new Car().model(model);
filter.setEntity(car);
filter.getEntity().model(model);
}
if(name != null){
filter.getEntity().name(name);
}
filter.addParam("maxPrice",maxPrice);
filter.addParam("minPrice",minPrice);
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/com/cdi/crud/service/CarService.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ public Criteria configPagination(Filter<Car> filter) {
// see index.xhtml 'model' column facet name filter
if (filter.getEntity() != null) {
crud().ilike("model", filter.getEntity().getModel(), MatchMode.ANYWHERE);
}

if (filter.getEntity() != null) {
crud().ilike("name", filter.getEntity().getName(), MatchMode.ANYWHERE);
}

Expand Down
5 changes: 5 additions & 0 deletions src/main/webapp/apidocs/cars.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@
"description" : "list cars with given model",
"paramType" : "query",
"name" : "model"
}, {
"type" : "string",
"description" : "list cars with given name",
"paramType" : "query",
"name" : "name"
}, {
"type" : "number",
"format" : "double",
Expand Down
7 changes: 6 additions & 1 deletion src/main/webapp/index.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@
</f:facet>
#{c.model}
</p:column>
<p:column headerText="Name" filterBy="#{c.name}" sortBy="#{c.name}" filterMatchMode="contains">
<p:column headerText="Name" sortBy="#{c.name}" filterBy="#{c.name}" >
<f:facet name="filter">
<p:inputText value="#{crudBean.filter.entity.name}" >
<p:ajax process="@this" update="@none" oncomplete="PF('carsTable').filter()"/>
</p:inputText>
</f:facet>
#{c.name}
</p:column>
<p:column headerText="Price" sortBy="#{c.price}" filterBy="#{c.price}" >
Expand Down
21 changes: 19 additions & 2 deletions src/test/java/com/cdi/crud/test/CrudRest.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ public void shouldListCarsByPrice() {
public void shouldListCarsByModel() {
given().
queryParam("model","Porche").
when().
when().
get(basePath + "rest/cars/list").
then().
then().
statusCode(Response.Status.OK.getStatusCode()).
body("", hasSize(2)).
body("model", hasItem("Porche")).
Expand All @@ -108,6 +108,23 @@ public void shouldListCarsByModel() {
body("model",not(hasItem("Ferrari")));
}

@Test
public void shouldListCarsByName() {
given().
queryParam("name","spider").
when().
get(basePath + "rest/cars/list").
then().
statusCode(Response.Status.OK.getStatusCode()).
body("", hasSize(2)).
body("model", hasItem("Mustang")).
body("name",hasItem("mustang spider")).
body("price", hasItem(12999.0f)).
body("name",hasItem("ferrari spider")).
body("price", hasItem(2450.8f)).
body("model",not(hasItem("Porche")));
}

@Test
public void shouldFindCar() {
String json =
Expand Down

0 comments on commit 6f86c59

Please sign in to comment.