Learn how to intercept HTTP GET commands with RestController methods.
- [Instructor] Now let's create two new APIs, one to look up all the ratings for tour, and another to calculate the average score of all the ratings. I think it is best to hide the implementation of tour ratings entity from the API, so we will return a list of tour DTO objects, and we will calculate the average by setting the URL with the slash average at the end, and then we'll return adjacent packet with the key value pair of average and then the actual average score.
So we are now in our tour rating controller, and I have a helper method in here that will convert a tour rating to a rating DTO. First let's do our get, get all ratings for a tour. Again, we have our path variable for the tour ID.
Now let's annotate this properly. We want the request mapping to do just get on the base URL. So the first step is we're going to verify the tour using the tour ID that's passed in, and now we're going to look up all the ratings for a tour and stream them to a map, and then for each element in the stream, we're going to convert the tour rating to a tour DTO and gather those into a list and return them.
So looking up by the tour ID, invoke the two DTO method on the tour rating, and now collect them to a list. Now let's create the method to look up all the ratings and then calculate the score average, and I have some common code here, so I'm going to actually grab that and copy it and I will use it when creating the next method.
So we're calling to return a pair of the word average, which is a string, to a double, which would be the calculated average, called the method get average, paste in the common code that I was going to use, and let's annotate it. So here the request mapping is similar, but we need to add the slash average in the path.
Okay. So we get all of the tour ratings. We're not going to return them, we're going to use them. Now we're going to calculate the average, and now depending if there were any tour ratings found, if there were, we will have a value present in the optional double. If not, it will contain a null.
So we're going to return a new, and we have the string average and the result, so we need to check if average is present, then we will turn get as double. Otherwise, it will be a null. So let's run this. Now we can't get ratings unless we create them, so I'm going to create several tour ratings for tour one using the post API that we already created.
So let's say this customer thought tour number one was great then customer number two also thought it was great and gave it a five, and let's say customer number three gave it a three and said it was okay, and then customer number four didn't like it at all and said it was bad.
So now let's get the tour ratings, and here it returns the rating DTOs and let's upend the average onto the URL, and we now have the average score.
- Setting up the project
- Building, deploying, and launch the microservice
- Declaring Spring Data JPA repository interfaces
- Invoking repositories
- Using Spring Data query methods
- Exposing RESTful APIs with Spring Data REST
- Using the /search resource to invoke query methods
- Paging and sorting
- Declaring a new REST controller
- Creating HTTP methods for creating, reading, updating and deleting persistent data.