I am trying to retrieve a user's particular order.
This is how I retrieve the user's orders in my OrderController
@GetMapping("/{id}/orders")
public List<Order> findAll(@PathVariable Long id) throws UserNotFoundException {
Optional<User> existingUser = this.userRepository.findById(id);
if (existingUser.isEmpty()) {
throw new UserNotFoundException("User not found");
}
return existingUser.get().getOrders();
}
With the RequestMapping
@RestController
@RequestMapping("/api/users")
public class OrderController {(...)}
This is the OneToMany relationship
User Entity
// ONE TO MANY
@OneToMany(mappedBy = "user")
private List<Order> orders;
Order Entity
// MANY TO ONE
@ManyToOne(fetch = FetchType.LAZY)
@JsonIgnore
private User user;
The UserRepository and OrderRepository interface, both extend JpaRepository
I do manage to retrieve all the user's orders through Postman
I am now wondering, how can I retrieve a user's particular order?
So for instance, as shown in the image let's say I would only like to retrieve the order with the id of 2, in this particular address :
http://localhost:8070/api/users/1/orders/2
How can I make it please ?