Problem statement
Build a REST API for veterinary visit records that loads a pet with its full visit history in a single query.
Requirements
PetRepository
Add two query methods:
- One loads a single pet by its id, together with its owner and all of its visits, in one trip to the database, so the pet detail is already complete when the query returns. A pet that has no visits is still returned, carrying an empty visit list rather than dropping out of the result, and an id that matches no pet yields no result at all
- One returns every pet belonging to one owner, and an empty list when that owner has no pets
Relationships are lazy and lazy loading outside a query is disabled, so touching an unloaded owner or visit list while building a response fails at runtime.
PetService
The service owns the read logic, and the endpoints delegate to it. Responses are built only from the provided DTO classes, and an entity is never serialized directly: the pet detail names its owner only through ownerName, and visit items never embed their pet.
getPetDetail
- Assembles the detail of one pet from its own fields, the ownerName of its owner, and its visits
- The visits are sorted by visitDate descending, most recent first
- Throws the provided pet-not-found exception with message "Pet not found with id: " followed by the requested id when no pet matches
getPetsForOwner
- Returns one summary per pet of exactly that owner, and an empty list when the owner has no pets
- Throws the provided owner-not-found exception with message "Owner not found with id: " followed by the requested id when no owner matches, decided before the owner's pets are looked up, so an unknown owner is never reported as an owner with no pets
PetController
Implement the two REST endpoints. Each endpoint must be reachable at exactly the verb and path listed below.
GET /pets/{id}
- Return 200 OK with the pet's detail: its id, name, species, the ownerName of its owner, and its visits
- The detail carries exactly those five keys and nothing else
- visits holds the pet's visit history sorted by visitDate descending, most recent first
- Each visit item carries exactly id, visitDate, reason, and vetName, with the stored values
- Only visits of the requested pet appear, never a visit of another pet
- A pet that has no visits returns 200 OK with an empty visits array
- An id that matches no pet returns 404 Not Found with error message: "Pet not found with id: " followed by the requested id
- Serving this endpoint issues exactly one SQL statement, no matter how many visits the pet has and also when it has none
GET /owners/{id}/pets
- Return 200 OK with the list of that owner's pets, each carrying exactly id, name, and species with the stored values
- Pets of other owners never appear in the list
- An owner that has no pets returns 200 OK with an empty array
- An id that matches no owner returns 404 Not Found with error message: "Owner not found with id: " followed by the requested id
Sample Requests & Responses
Expand to view details on sample requests and responses.
GET /pets/1
Response (200 OK):
{
"id": 1,
"name": "Biscuit",
"species": "Dog",
"ownerName": "Priya Raman",
"visits": [
{
"id": 2,
"visitDate": "2026-06-18",
"reason": "Ear infection",
"vetName": "Dr. Chen"
},
{
"id": 1,
"visitDate": "2026-03-02",
"reason": "Annual checkup",
"vetName": "Dr. Alvarez"
},
{
"id": 3,
"visitDate": "2025-11-27",
"reason": "Vaccination booster",
"vetName": "Dr. Alvarez"
}
]
}
GET /pets/3
Response (200 OK):
{
"id": 3,
"name": "Ziggy",
"species": "Parrot",
"ownerName": "Marcus Webb",
"visits": []
}
GET /pets/9999
Response (404 Not Found):
{
"error": "Pet not found with id: 9999",
"timestamp": "2026-07-30T11:02:54.506576",
"path": "/pets/9999"
}
GET /owners/1/pets
Response (200 OK):
[
{
"id": 1,
"name": "Biscuit",
"species": "Dog"
},
{
"id": 2,
"name": "Clementine",
"species": "Cat"
}
]
GET /owners/9999/pets
Response (404 Not Found):
{
"error": "Owner not found with id: 9999",
"timestamp": "2026-07-30T11:02:54.864612",
"path": "/owners/9999/pets"
}
