Skip to content

Fix duplicate pet ID issue - created by agentic #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@

import org.springframework.core.style.ToStringCreator;
import org.springframework.samples.petclinic.model.Person;
import org.springframework.util.Assert;

import jakarta.persistence.CascadeType;
import org.springframework.util.Assert;import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
Expand All @@ -43,8 +41,7 @@
* @author Oliver Drotbohm
*/
@Entity
@Table(name = "owners")
public class Owner extends Person {
@Table(name = "owners")public class Owner extends Person {

@Column(name = "address")
@NotBlank
Expand Down Expand Up @@ -94,11 +91,16 @@ public List<Pet> getPets() {

public void addPet(Pet pet) {
if (pet.isNew()) {
// Check for duplicate ID if ID is set
if (pet.getId() != null) {
Pet existingPet = getPet(pet.getId());
if (existingPet != null) {
throw new IllegalStateException("Pet with ID " + pet.getId() + " already exists");
}
}
getPets().add(pet);
}
}

/**
}/**
* Return the Pet with the given name, or null if none found for this Owner.
* @param name to test
* @return a pet if pet name is already in use
Expand All @@ -122,9 +124,7 @@ public Pet getPet(Integer id) {
}
}
return null;
}

/**
}/**
* Return the Pet with the given name, or null if none found for this Owner.
* @param name to test
* @return a pet if pet name is already in use
Expand Down Expand Up @@ -152,9 +152,7 @@ public String toString() {
.append("city", this.city)
.append("telephone", this.telephone)
.toString();
}

/**
}/**
* Adds the given {@link Visit} to the {@link Pet} with the given identifier.
* @param petId the identifier of the {@link Pet}, must not be {@literal null}.
* @param visit the visit to add, must not be {@literal null}.
Expand All @@ -171,4 +169,4 @@ public void addVisit(Integer petId, Visit visit) {
pet.addVisit(visit);
}

}
}
2 changes: 2 additions & 0 deletions src/main/resources/db/migration/V2__Add_pet_id_constraint.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- Add unique constraint on pet_id
ALTER TABLE pets ADD CONSTRAINT uk_pet_id UNIQUE (id);