Skip to content

feat: add GET, PUT and DELETE endpoints to PetController#150

Merged
lindaeskilsson merged 2 commits intomainfrom
feat/add-pet-controller-endpoints
Apr 7, 2026
Merged

feat: add GET, PUT and DELETE endpoints to PetController#150
lindaeskilsson merged 2 commits intomainfrom
feat/add-pet-controller-endpoints

Conversation

@lindaeskilsson
Copy link
Copy Markdown
Contributor

@lindaeskilsson lindaeskilsson commented Apr 7, 2026

Closes #146

Vad är gjort

Lagt till saknade endpoints i PetController:

  • GET /pets/{petId} – hämta ett specifikt djur
  • GET /pets/owner/{ownerId} – hämta alla djur för en ägare
  • PUT /pets/{petId} – uppdatera ett djur
  • DELETE /pets/{petId} – radera ett djur

Ändringar

  • PetController.java – nya endpoints tillagda
  • UserService injiceras för att lösa upp currentUserId till User-entitet
  • Privat toResponse()-hjälpmetod extraherad för att undvika kodupprepning

Summary by CodeRabbit

  • New Features

    • Added endpoints to retrieve individual pets, list pets by owner, update pet details, and delete pets.
  • Bug Fixes

    • Improved error responses and handling for permission issues, missing resources, invalid requests, and delete constraints; delete now returns 204 No Content on success.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 7, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 9b90a837-55ca-4b1d-951d-05d31f781fb7

📥 Commits

Reviewing files that changed from the base of the PR and between 0af7b84 and 35610a1.

📒 Files selected for processing (1)
  • src/main/java/org/example/vet1177/services/PetService.java

📝 Walkthrough

Walkthrough

PetController was expanded with full CRUD endpoints (GET by ID, GET by owner, PUT, DELETE), now depends on UserService, and uses a new private toResponse(Pet) helper for mapping; PetService internals now throw domain-specific exceptions (ForbiddenException, BusinessRuleException, ResourceNotFoundException) in place of generic RuntimeException.

Changes

Cohort / File(s) Summary
PetController
src/main/java/org/example/vet1177/controller/PetController.java
Constructor now injects UserService; added endpoints: GET /pets/{petId}, GET /pets/owner/{ownerId}, PUT /pets/{petId}, DELETE /pets/{petId}; createPet refactored to use private toResponse(Pet) helper used across endpoints; endpoints resolve current user via UserService and delegate to PetService.
PetService (exceptions)
src/main/java/org/example/vet1177/services/PetService.java
Replaced generic RuntimeException throws with domain/HTTP-aligned exceptions: ForbiddenException, BusinessRuleException, and ResourceNotFoundException at existing decision points (create/get/update/delete flows).

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Controller as PetController
    participant UserSvc as UserService
    participant PetSvc as PetService
    participant DB as Database

    Client->>Controller: GET /pets/{petId} (currentUserId, petId)
    Controller->>UserSvc: getUserEntityById(currentUserId)
    UserSvc-->>Controller: User entity
    Controller->>PetSvc: getPetById(petId, currentUser)
    PetSvc->>DB: query pet by id & authorization checks
    DB-->>PetSvc: Pet entity
    PetSvc-->>Controller: Pet entity
    Controller->>Client: 200 OK (PetResponse)
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested labels

enhancement

Suggested reviewers

  • johanbriger
  • annikaholmqvist94
  • TatjanaTrajkovic

Poem

🐰 I hopped to add endpoints, one through four,
GET, PUT, DELETE — now there's more,
With UserService I sniffed each paw,
PetResponse tidy, neat and raw,
Hooray — the controller hops and soars! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately and concisely describes the main change: adding GET, PUT, and DELETE endpoints to PetController.
Linked Issues check ✅ Passed All four endpoints required by issue #146 are implemented: GET /pets/{petId}, GET /pets/owner/{ownerId}, PUT /pets/{petId}, and DELETE /pets/{petId}, all properly delegating to PetService.
Out of Scope Changes check ✅ Passed PetService exception handling improvements (replacing RuntimeException with typed exceptions) are a supporting change that enhances the endpoint implementation scope.
Docstring Coverage ✅ Passed Docstring coverage is 85.71% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/add-pet-controller-endpoints

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/main/java/org/example/vet1177/controller/PetController.java (1)

30-37: Pick one boundary for resolving the current user.

createPet, getPetsByOwner, and updatePet pass currentUserId into PetService, while getPetById and deletePet resolve a User in the controller first. That splits not-found/authorization behavior across layers and makes the controller harder to keep consistent. I’d standardize on one approach for all five endpoints.

Also applies to: 42-47, 53-57, 66-72, 77-82

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/main/java/org/example/vet1177/controller/PetController.java` around lines
30 - 37, The controller mixes two patterns for resolving the current user: some
endpoints (createPet, getPetsByOwner, updatePet) forward a currentUserId to
PetService while others (getPetById, deletePet) resolve a User in the
controller; pick one boundary and apply it to all endpoints to centralize
not-found/auth behavior. Either (A) resolve and validate the User in the
controller for all endpoints and pass the User (or its ID known-valid) into
PetService, or (B) accept only the raw currentUserId in controllers and move all
User lookup/authorization into PetService; then update createPet,
getPetsByOwner, updatePet, getPetById, and deletePet accordingly so they all
follow the chosen pattern and remove duplicated lookup/authorization code
between controller and PetService.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/main/java/org/example/vet1177/controller/PetController.java`:
- Around line 42-48: PetService methods (notably getPetById and deletePet) are
currently throwing raw RuntimeException; change those throws to the appropriate
typed exceptions so the GlobalExceptionHandler maps to correct HTTP statuses —
throw ResourceNotFoundException when the pet or related resource is missing,
throw ForbiddenException when the current user is not authorized to access or
delete the pet, and throw BusinessRuleException for validation/business-rule
violations; update the throw sites in PetService (replace RuntimeException
occurrences around the reported locations) so callers like PetController.receive
the typed exceptions.

---

Nitpick comments:
In `@src/main/java/org/example/vet1177/controller/PetController.java`:
- Around line 30-37: The controller mixes two patterns for resolving the current
user: some endpoints (createPet, getPetsByOwner, updatePet) forward a
currentUserId to PetService while others (getPetById, deletePet) resolve a User
in the controller; pick one boundary and apply it to all endpoints to centralize
not-found/auth behavior. Either (A) resolve and validate the User in the
controller for all endpoints and pass the User (or its ID known-valid) into
PetService, or (B) accept only the raw currentUserId in controllers and move all
User lookup/authorization into PetService; then update createPet,
getPetsByOwner, updatePet, getPetById, and deletePet accordingly so they all
follow the chosen pattern and remove duplicated lookup/authorization code
between controller and PetService.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 62bcd0ce-246d-4d34-950b-c0b72e76f59c

📥 Commits

Reviewing files that changed from the base of the PR and between 1cf45a8 and 0af7b84.

📒 Files selected for processing (1)
  • src/main/java/org/example/vet1177/controller/PetController.java

@TatjanaTrajkovic TatjanaTrajkovic self-requested a review April 7, 2026 19:05
Copy link
Copy Markdown
Contributor

@TatjanaTrajkovic TatjanaTrajkovic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bra jobbat!

@lindaeskilsson lindaeskilsson merged commit f675903 into main Apr 7, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: PetController saknar GET, PUT och DELETE endpoints

2 participants