ways to remove and delete
All checks were successful
Build Images and Deploy / Update-PROD-Stack (push) Successful in 33s
All checks were successful
Build Images and Deploy / Update-PROD-Stack (push) Successful in 33s
This commit is contained in:
@@ -213,4 +213,32 @@ router.post('/:id/respond', authMiddleware, asyncHandler(async (req, res) => {
|
||||
res.json({ status });
|
||||
}));
|
||||
|
||||
// Delete a challenge (only creator can delete)
|
||||
router.delete('/:id', authMiddleware, asyncHandler(async (req, res) => {
|
||||
const challengeId = req.params.id;
|
||||
|
||||
// Get challenge and verify ownership
|
||||
const challenges = await query(
|
||||
'SELECT * FROM challenges WHERE id = ?',
|
||||
[challengeId]
|
||||
);
|
||||
|
||||
if (challenges.length === 0) {
|
||||
throw new AppError('Challenge not found', 404);
|
||||
}
|
||||
|
||||
const challenge = challenges[0];
|
||||
|
||||
if (challenge.created_by !== req.user.userId) {
|
||||
throw new AppError('Only the creator can delete this challenge', 403);
|
||||
}
|
||||
|
||||
// Delete related data (cascade should handle this, but being explicit)
|
||||
await query('DELETE FROM predictions WHERE challenge_id = ?', [challengeId]);
|
||||
await query('DELETE FROM challenge_participants WHERE challenge_id = ?', [challengeId]);
|
||||
await query('DELETE FROM challenges WHERE id = ?', [challengeId]);
|
||||
|
||||
res.json({ success: true, message: 'Challenge deleted successfully' });
|
||||
}));
|
||||
|
||||
export default router;
|
||||
|
||||
@@ -159,4 +159,26 @@ router.get('/requests', authMiddleware, asyncHandler(async (req, res) => {
|
||||
res.json({ requests });
|
||||
}));
|
||||
|
||||
// Remove a friend
|
||||
router.delete('/:friendId', authMiddleware, asyncHandler(async (req, res) => {
|
||||
const friendId = parseInt(req.params.friendId);
|
||||
|
||||
if (!friendId) {
|
||||
throw new AppError('Friend ID required', 400);
|
||||
}
|
||||
|
||||
// Delete friendship in both directions
|
||||
const result = await query(
|
||||
`DELETE FROM friendships
|
||||
WHERE (user_id = ? AND friend_id = ?) OR (user_id = ? AND friend_id = ?)`,
|
||||
[req.user.userId, friendId, friendId, req.user.userId]
|
||||
);
|
||||
|
||||
if (result.affectedRows === 0) {
|
||||
throw new AppError('Friendship not found', 404);
|
||||
}
|
||||
|
||||
res.json({ success: true, message: 'Friend removed successfully' });
|
||||
}));
|
||||
|
||||
export default router;
|
||||
|
||||
Reference in New Issue
Block a user