Skip to content

Commit 4beb7e4

Browse files
committed
Created proper byExample in VehicleInspeection and in template and added test
1 parent 0cc9cfb commit 4beb7e4

7 files changed

Lines changed: 66 additions & 19 deletions

File tree

memex/README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -392,24 +392,23 @@ classes for an Entity including all the new
392392
derived classes by running the maven goal. This adds a Generic flexible model.
393393

394394
```shell
395-
mvn generate-sources -Pgenerate-entity -Dentity=MyEntity -Dplural=entities -DidFieldName=entityId -DidFieldType=Long
395+
mvn generate-sources -Pgenerate-entity -Dentity=Customer -Dplural=customers -DidFieldName=customerId -DidFieldType=Long
396396
```
397397

398398
You can delete it with
399399

400400
```shell
401-
mvn generate-sources -Pgenerate-entity -Dentity=MyEntity -DidType=Long -Ddelete
401+
mvn generate-sources -Pgenerate-entity -Dentity=Customer -DidType=Long -Ddelete
402402
```
403403

404404
# Model Generation
405405

406406
You can also Generate a data specific model from a JSON file using the maven
407-
goal below. customId shodu lmap to the id
408-
field in the JSON
407+
goal below. customId should be the name of the id field in the JSON and entity
409408

410409
```shell
411410

412-
mvn generate-sources -Pgenerate-models-from-json -DjsonFile=../DataGen/mot.json -DbasePackage=com.johnlpage.memex -Dentity=MyEntity -DidFieldName=myentityId
411+
mvn generate-sources -Pgenerate-models-from-json -DjsonFile=../DataGen/mot.json -DbasePackage=com.johnlpage.memex -Dentity=Customer -DidFieldName=customerId
413412

414413
```
415414

memex/src/main/java/com/johnlpage/memex/VehicleInspection/controller/VehicleInspectionController.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ public ResponseEntity<VehicleInspection> getById(@PathVariable Long id) {
131131
.orElse(ResponseEntity.notFound().build());
132132
}
133133

134+
134135
/**
135136
* JPA Get By Example Query - Needs an Index to be efficient It still finds ALL the results each
136137
* time and returns a subset
@@ -141,15 +142,23 @@ public ResponseEntity<PageDto<VehicleInspection>> getInspectionsByModel(
141142
@RequestParam(name = "page", required = false, defaultValue = "0") int page,
142143
@RequestParam(name = "size", required = false, defaultValue = "10") int size) {
143144

144-
// This is where we are hard coding a query for this endpoint.
145-
// By creating an example of the class, clumsy but works.
146-
VehicleInspection example = new VehicleInspection();
147-
Vehicle v = new Vehicle();
148-
v.setModel(model);
149-
example.setVehicle(v);
150-
151145
// Use the line below for immutable model
152146
// VehicleInspection probe = VehicleInspection.builder().model(model).build();
147+
Slice<VehicleInspection> returnPage = queryService.getByVehicleModel(model, page, size);
148+
PageDto<VehicleInspection> entity = new PageDto<>(returnPage);
149+
return ResponseEntity.ok(entity);
150+
}
151+
152+
/**
153+
* JPA Get By Example Query , pass in some fields get matching in pages
154+
* Post as we are sending a body
155+
*/
156+
157+
@PostMapping("/inspections/byExample")
158+
public ResponseEntity<PageDto<VehicleInspection>> getInspectionByExample(
159+
@RequestBody VehicleInspection example,
160+
@RequestParam(name = "page", required = false, defaultValue = "0") int page,
161+
@RequestParam(name = "size", required = false, defaultValue = "10") int size) {
153162
Slice<VehicleInspection> returnPage = queryService.getByExample(example, page, size);
154163
PageDto<VehicleInspection> entity = new PageDto<>(returnPage);
155164
return ResponseEntity.ok(entity);

memex/src/main/java/com/johnlpage/memex/VehicleInspection/repository/VehicleInspectionRepository.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import java.util.stream.Stream;
1111

1212
import org.bson.Document;
13+
import org.springframework.data.domain.PageRequest;
14+
import org.springframework.data.domain.Slice;
1315
import org.springframework.data.mongodb.repository.Aggregation;
1416
import org.springframework.data.mongodb.repository.MongoRepository;
1517
import org.springframework.data.mongodb.repository.Query;
@@ -35,6 +37,9 @@ public interface VehicleInspectionRepository
3537
// Derived Query with Boolean and
3638
List<VehicleInspection> findByVehicleColourAndVehicleModel(String colour, String model);
3739

40+
// Version that gets a page using Slice as Page is a lot slower due to counting
41+
Slice<VehicleInspection> findByVehicleModel(String model, PageRequest page);
42+
3843
// Annotation-based aggregation (Group By in this case) returning a Docuement as a generic type
3944
@Aggregation(
4045
pipeline = {
@@ -56,4 +61,6 @@ public interface VehicleInspectionRepository
5661

5762
// Fetch whole collection as a stream.
5863
Stream<VehicleInspection> findAllBy();
64+
65+
5966
}

memex/src/main/java/com/johnlpage/memex/VehicleInspection/service/VehicleInspectionQueryService.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,13 @@ public Optional<VehicleInspection> getById(Long id) {
4848
}
4949

5050
public Slice<VehicleInspection> getByExample(VehicleInspection probe, int page, int size) {
51+
//This line means null values in example do not need ot be null in database
5152
ExampleMatcher matcher = ExampleMatcher.matching().withIgnoreNullValues();
5253
Example<VehicleInspection> example = Example.of(probe, matcher);
5354
return repository.findAll(example, PageRequest.of(page, size));
5455
}
56+
57+
public Slice<VehicleInspection> getByVehicleModel(String model, int page, int size) {
58+
return repository.findByVehicleModel(model,PageRequest.of(page,size));
59+
}
5560
}

memex/src/main/resources/application.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ memex.preflight.dropAllCollections=false
1919
#
2020
#If not in URI or other Auth Method
2121
logging.level.org.springframework.data.repository=INFO
22-
logging.level.org.springframework.data.mongodb.core=INFO
22+
logging.level.org.springframework.data.mongodb.core=DEBUG
2323
# This one is handy to see what is being done against MDB
2424
logging.level.org.mongodb.driver=INFO
2525
#debug=true

memex/src/test/resources/features/inspections.rest.crud.feature

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,24 @@ Feature: Vehicle Inspection REST API - Core CRUD Operations
7676
Then the response status code should be 404
7777
And the response should be empty
7878

79+
@post @by_example @sunny_day
80+
Scenario: Successfully fetch a page of result by example
81+
Given the following vehicle inspections exist:
82+
| vehicleinspection |
83+
| {"testid": 10001, "vehicle": {"model": "Corolla"}} |
84+
When I send a POST request to "/api/inspections/byExample?page=0&size=10" with the payload:
85+
"""
86+
{
87+
"vehicle": {
88+
"model": "Corolla"
89+
}
90+
}
91+
"""
92+
Then the response status code should be 200
93+
And the response should contain "content" with 1 items
94+
And the response should contain "pageNumber": 0
95+
And the response should contain "pageSize": 10
96+
7997
@get @by_model @sunny_day
8098
Scenario Outline: Successfully retrieve vehicle inspections by model with pagination
8199
Given the following vehicle inspections exist:
@@ -106,3 +124,14 @@ Feature: Vehicle Inspection REST API - Core CRUD Operations
106124
And the response should contain "content" with 0 items
107125
And the response should contain "pageNumber": 0
108126
And the response should contain "pageSize": 10
127+
128+
@get @by_model @sunny_day
129+
Scenario: Retrieve no vehicle inspections for a non-existent model
130+
Given the following vehicle inspections do not exist:
131+
| vehicleinspection |
132+
| {"vehicle.model": "NonExistentModel"} |
133+
When I send a GET request to "/api/inspections/model/NonExistentModel"
134+
Then the response status code should be 200
135+
And the response should contain "content" with 0 items
136+
And the response should contain "pageNumber": 0
137+
And the response should contain "pageSize": 10

memex/templates/controller/Controller.java.template

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,15 @@ public class __className__Controller {
114114

115115
/**
116116
* Get by example with pagination.
117-
* TODO: Customize this endpoint for your specific query needs
117+
* Don't forget to add indexes as required.
118118
*/
119-
@GetMapping("/__apiPath__/byExample")
119+
120+
@PostMapping("/__apiPath__/byExample")
120121
public ResponseEntity<PageDto<__className__>> getByExample(
122+
@RequestBody __className__ example,
121123
@RequestParam(name = "page", required = false, defaultValue = "0") int page,
122124
@RequestParam(name = "size", required = false, defaultValue = "10") int size) {
123125

124-
// TODO: Build your example/probe object based on request parameters
125-
__className__ example = new __className__();
126-
// example.setSomeField(someValue);
127-
128126
Slice<__className__> returnPage = queryService.getByExample(example, page, size);
129127
PageDto<__className__> entity = new PageDto<>(returnPage);
130128
return ResponseEntity.ok(entity);

0 commit comments

Comments
 (0)