-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -141,4 +141,44 @@ export class HeroController { | |||||||||||||||||||||||||||||||||||
next(error); | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||
* Delete a hero by ID | ||||||||||||||||||||||||||||||||||||
* @route DELETE /api/heroes/:id | ||||||||||||||||||||||||||||||||||||
* @param {number} id - Hero ID | ||||||||||||||||||||||||||||||||||||
* @query {boolean} confirm - Confirmation flag to ensure deletion is intentional | ||||||||||||||||||||||||||||||||||||
* @returns {Object} Success message or error message | ||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||
async deleteHero(req: Request, res: Response, next: NextFunction): Promise<void> { | ||||||||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||||||||
const id = Number(req.params.id); | ||||||||||||||||||||||||||||||||||||
const confirm = req.query.confirm === 'true'; | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
if (isNaN(id)) { | ||||||||||||||||||||||||||||||||||||
res.status(400).json({ error: 'Invalid hero ID. ID must be a number.' }); | ||||||||||||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
const result = await this.heroService.deleteHero(id, confirm); | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
if (!result.success) { | ||||||||||||||||||||||||||||||||||||
// 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 }); | ||||||||||||||||||||||||||||||||||||
Comment on lines
+165
to
+171
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
res.status(200).json({ | ||||||||||||||||||||||||||||||||||||
success: true, | ||||||||||||||||||||||||||||||||||||
message: result.message, | ||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||
} catch (error) { | ||||||||||||||||||||||||||||||||||||
next(error); | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -26,6 +26,13 @@ export interface CreateHeroResult { | |||||
error?: string; | ||||||
} | ||||||
|
||||||
// Define interface for hero deletion result | ||||||
export interface DeleteHeroResult { | ||||||
success: boolean; | ||||||
message?: string; | ||||||
error?: string; | ||||||
} | ||||||
|
||||||
export class HeroService { | ||||||
/** | ||||||
* Get heroes from the database with filtering and pagination | ||||||
|
@@ -146,4 +153,48 @@ export class HeroService { | |||||
}; | ||||||
} | ||||||
} | ||||||
|
||||||
/** | ||||||
* Delete a hero from the database | ||||||
* @param id Hero id to delete | ||||||
* @param confirm Confirmation flag to ensure deletion is intentional | ||||||
* @returns Promise resolving to delete result with success flag and message or error | ||||||
*/ | ||||||
async deleteHero(id: number, confirm: boolean): Promise<DeleteHeroResult> { | ||||||
try { | ||||||
// Check if confirmation is provided | ||||||
if (!confirm) { | ||||||
return { | ||||||
success: false, | ||||||
error: 'Confirmation is required to delete a hero', | ||||||
}; | ||||||
} | ||||||
|
||||||
// Check if hero exists | ||||||
const hero = await HeroModel.findOne({ id }); | ||||||
if (!hero) { | ||||||
return { | ||||||
success: false, | ||||||
error: `Hero with ID ${id} not found`, | ||||||
}; | ||||||
} | ||||||
|
||||||
// Delete the hero | ||||||
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 commentThe 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. Positive FeedbackNegative Feedback |
||||||
|
||||||
return { | ||||||
success: true, | ||||||
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 commentThe 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.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
return { | ||||||
success: false, | ||||||
error: 'An unexpected error occurred while deleting the hero', | ||||||
}; | ||||||
} | ||||||
} | ||||||
} |
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.