diff --git a/src/main/java/org/springframework/samples/petclinic/owner/PetController.java b/src/main/java/org/springframework/samples/petclinic/owner/PetController.java index 3234e39d6..44b1f3637 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/PetController.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/PetController.java @@ -59,10 +59,8 @@ class PetController { @ModelAttribute("owner") public Owner findOwner(@PathVariable("ownerId") int ownerId) { - Optional optionalOwner = this.owners.findById(ownerId); - Owner owner = optionalOwner.orElseThrow(() -> new IllegalArgumentException( + return this.owners.findById(ownerId).orElseThrow(() -> new IllegalArgumentException( "Owner not found with id: " + ownerId + ". Please ensure the ID is correct ")); - return owner; } @ModelAttribute("pet") @@ -73,8 +71,7 @@ class PetController { return new Pet(); } - Optional optionalOwner = this.owners.findById(ownerId); - Owner owner = optionalOwner.orElseThrow(() -> new IllegalArgumentException( + Owner owner = this.owners.findById(ownerId).orElseThrow(() -> new IllegalArgumentException( "Owner not found with id: " + ownerId + ". Please ensure the ID is correct ")); return owner.getPet(petId); } @@ -170,4 +167,4 @@ class PetController { this.owners.save(owner); } -} +} \ No newline at end of file