Challenge Solution

In this lesson, let’s check the solution for the challenge posed in the previous lesson.

Controller

This code is quite simple and very similar to the API for getAllOrders.

Press + to interact
// get orderitems for an order
@GetMapping("/{id}")
public ResponseEntity<Object> getOrderById(@PathVariable("id") Integer id, @RequestParam("token") String token)
throws AuthenticationFailException, OrderNotFoundException {
// 1. validate token
// validate token
authenticationService.authenticate(token);
// 2. find user
// retrieve user
User user = authenticationService.getUser(token);
// 3. call getOrder method of order service an pass orderId and user
Order order = orderService.getOrder(id, user);
// 4. display order in json response
return new ResponseEntity<>(order, HttpStatus.OK);
}

Service

First, we’ll use the ...