Files
pp-tcms/backend/routes/comments/delete.js
2026-02-23 12:26:07 +09:00

39 lines
1.1 KiB
JavaScript

import express from 'express';
const router = express.Router();
import { DataTypes } from 'sequelize';
import defineComment from '../../models/comments.js';
import authMiddleware from '../../middleware/auth.js';
export default function (sequelize) {
const { verifySignedIn } = authMiddleware(sequelize);
const Comment = defineComment(sequelize, DataTypes);
router.delete('/:commentId', verifySignedIn, async (req, res) => {
const commentId = req.params.commentId;
if (!commentId) {
return res.status(400).json({ error: 'commentId is required' });
}
try {
const comment = await Comment.findByPk(commentId);
if (!comment) {
return res.status(404).json({ error: 'Comment not found' });
}
// Verify the user owns the comment
if (comment.userId !== req.userId) {
return res.status(403).json({ error: 'Unauthorized' });
}
await comment.destroy();
res.json({ success: true });
} catch (error) {
console.error(error);
res.status(500).send('Internal Server Error');
}
});
return router;
}