diff --git a/src/main/java/org/springframework/samples/petclinic/model/Person.java b/src/main/java/org/springframework/samples/petclinic/model/Person.java index 30b5829d8..6907838e6 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/Person.java +++ b/src/main/java/org/springframework/samples/petclinic/model/Person.java @@ -15,8 +15,7 @@ */ package org.springframework.samples.petclinic.model; -import jakarta.persistence.Column; -import jakarta.persistence.MappedSuperclass; +import jakarta.persistence.*; import jakarta.validation.constraints.NotBlank; /** @@ -27,6 +26,10 @@ import jakarta.validation.constraints.NotBlank; @MappedSuperclass public class Person extends BaseEntity { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Integer id; + @Column @NotBlank private String firstName; @@ -35,6 +38,10 @@ public class Person extends BaseEntity { @NotBlank private String lastName; + public Integer getId() { + return id; + } + public String getFirstName() { return this.firstName; } diff --git a/src/main/java/org/springframework/samples/petclinic/owner/Owner.java b/src/main/java/org/springframework/samples/petclinic/owner/Owner.java index 480a7a690..f8897d24d 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/Owner.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/Owner.java @@ -19,18 +19,11 @@ import java.util.ArrayList; import java.util.List; import java.util.Objects; +import jakarta.persistence.*; import org.springframework.core.style.ToStringCreator; import org.springframework.samples.petclinic.model.Person; import org.springframework.util.Assert; -import jakarta.persistence.CascadeType; -import jakarta.persistence.Column; -import jakarta.persistence.Entity; -import jakarta.persistence.FetchType; -import jakarta.persistence.JoinColumn; -import jakarta.persistence.OneToMany; -import jakarta.persistence.OrderBy; -import jakarta.persistence.Table; import jakarta.validation.constraints.Pattern; import jakarta.validation.constraints.NotBlank; @@ -66,6 +59,11 @@ public class Owner extends Person { @OrderBy("name") private final List pets = new ArrayList<>(); + @Transient + public int getPetCount() { + return pets == null ? 0 : pets.size(); + } + public String getAddress() { return this.address; } diff --git a/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java b/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java index 199ca3611..1e0df90ef 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java @@ -173,4 +173,37 @@ class OwnerController { return mav; } + /** + * Deletes an owner if and only if the owner has no pets. + *

+ * If the owner still has pets associated, the deletion is blocked and + * an error message is shown to the user. + * + * @param ownerId the ID of the owner to be deleted + * @param redirectAttributes used to pass success or error messages after redirect + * @return a redirect to the owners list page or back to the owner details page + */ + @PostMapping("/owners/{ownerId}/delete") + public String deleteOwner( + @PathVariable("ownerId") int ownerId, + RedirectAttributes redirectAttributes) { + + Owner owner = owners.findById(ownerId) + .orElseThrow(() -> new IllegalArgumentException( + "Owner not found with id: " + ownerId)); + + if (owner.getPetCount() > 0) { + redirectAttributes.addFlashAttribute( + "error", "Owner cannot be deleted while pets exist."); + return "redirect:/owners/" + ownerId; + } + + owners.delete(owner); + redirectAttributes.addFlashAttribute( + "message", "Owner deleted successfully."); + + return "redirect:/owners"; + } + + } diff --git a/src/main/resources/templates/owners/ownerDetails.html b/src/main/resources/templates/owners/ownerDetails.html index cc175cd13..9af860448 100644 --- a/src/main/resources/templates/owners/ownerDetails.html +++ b/src/main/resources/templates/owners/ownerDetails.html @@ -31,13 +31,25 @@ Telephone + + Number of Pets + + Edit Owner Add New Pet - +

+ + Delete Owner + +



@@ -93,4 +105,4 @@ - \ No newline at end of file +