-
Notifications
You must be signed in to change notification settings - Fork 1
Implement hero deletion endpoint with confirmation requirement #61
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
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: 0GiS0 <175379+0GiS0@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Adds a new DELETE endpoint to safely remove heroes by requiring explicit confirmation, checking existence, logging actions, and returning clear status codes and messages.
- Implements
deleteHero
inHeroService
with confirmation, existence check, deletion, and audit logging. - Exposes
DELETE /api/heroes/:id
inHeroController
with ID validation, confirmation processing, and proper HTTP responses. - Updates routing, HTTP example scripts, and documentation to cover deletion use cases.
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.
Show a summary per file
File | Description |
---|---|
src/services/hero.service.ts | Defined DeleteHeroResult and added deleteHero method with safety checks |
src/controllers/hero.controller.ts | Introduced deleteHero action handling confirmation and status codes |
src/routes/hero.routes.ts | Added DELETE route for hero deletion |
heroes.http | Added HTTP examples for deletion scenarios |
docs/basic-usage.md | Documented delete endpoint usage, parameters, and responses |
Comments suppressed due to low confidence (1)
src/services/hero.service.ts:163
- No unit tests cover the new deleteHero logic; consider adding tests for confirmation missing, hero not found, successful deletion, and error handling.
async deleteHero(id: number, confirm: boolean): Promise<DeleteHeroResult> {
await HeroModel.deleteOne({ id }); | ||
|
||
// Log the deletion for audit purposes | ||
console.log(`Hero deleted: ${hero.name} (ID: ${id}) at ${new Date().toISOString()}`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider using the project's structured logger instead of console.log for audit logging to maintain consistency and enable log level control.
Copilot uses AI. Check for mistakes.
message: `Hero ${hero.name} has been deleted`, | ||
}; | ||
} catch (error) { | ||
console.error(`Error deleting hero with id ${id}:`, error); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider using the project's structured logger instead of console.error for error logging to maintain consistency and facilitate log aggregation.
console.error(`Error deleting hero with id ${id}:`, error); | |
logger.error(`Error deleting hero with id ${id}:`, { id, error }); |
Copilot uses AI. Check for mistakes.
// Determine appropriate status code based on error | ||
if (result.error === 'Confirmation is required to delete a hero') { | ||
res.status(400).json({ error: result.error }); | ||
} else if (result.error?.includes('not found')) { | ||
res.status(404).json({ error: result.error }); | ||
} else { | ||
res.status(500).json({ error: result.error }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Matching on exact error message content is brittle; consider extending DeleteHeroResult with a status code or error code enum to drive HTTP status decisions.
// Determine appropriate status code based on error | |
if (result.error === 'Confirmation is required to delete a hero') { | |
res.status(400).json({ error: result.error }); | |
} else if (result.error?.includes('not found')) { | |
res.status(404).json({ error: result.error }); | |
} else { | |
res.status(500).json({ error: result.error }); | |
// Determine appropriate status code based on errorCode | |
switch (result.errorCode) { | |
case 'INVALID_CONFIRMATION': | |
res.status(400).json({ error: 'Confirmation is required to delete a hero' }); | |
break; | |
case 'HERO_NOT_FOUND': | |
res.status(404).json({ error: 'Hero not found' }); | |
break; | |
default: | |
res.status(500).json({ error: 'An unknown error occurred' }); |
Copilot uses AI. Check for mistakes.
|
||
### Delete hero without confirmation (will fail) | ||
DELETE {{baseUrl}}/heroes/1 HTTP/1.1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test request path is missing the /api
prefix; it should be {{baseUrl}}/api/heroes/1
to match the configured route.
Copilot uses AI. Check for mistakes.
This PR implements the hero deletion functionality for the Tour of Heroes API with several key safety features:
Implementation Details
Added a
deleteHero
method toHeroService
that:Added a controller method in
HeroController
that:Updated routes to include the new DELETE endpoint
Added comprehensive documentation:
basic-usage.md
heroes.http
for testingExample Usage
Safety Features
confirm=true
query parameter to prevent accidental deletionsFixes #40.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.