Challenge Solution
In this lesson, let’s check the solution for the challenge posed in the previous lesson.
We'll cover the following...
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 tokenauthenticationService.authenticate(token);// 2. find user// retrieve userUser user = authenticationService.getUser(token);// 3. call getOrder method of order service an pass orderId and userOrder order = orderService.getOrder(id, user);// 4. display order in json responsereturn new ResponseEntity<>(order, HttpStatus.OK);}
Service
First, we’ll use the ...