35 lines
1.2 KiB
TypeScript
35 lines
1.2 KiB
TypeScript
import { NextApiResponse } from 'next';
|
|
import { AuthenticatedRequest, requireAuth } from '../../../../../lib/middleware/authMiddleware';
|
|
import { MaterialService } from '../../../../../backend/services/materialService';
|
|
|
|
export default async function handler(req: AuthenticatedRequest, res: NextApiResponse) {
|
|
if (req.method !== 'POST') {
|
|
return res.status(405).json({ success: false, error: 'Method not allowed' });
|
|
}
|
|
|
|
// Require authentication
|
|
const isAuthenticated = await requireAuth(req, res);
|
|
if (!isAuthenticated) {
|
|
return;
|
|
}
|
|
|
|
const { id } = req.query;
|
|
const { content } = req.body;
|
|
|
|
if (typeof id !== 'string') {
|
|
return res.status(400).json({ success: false, error: 'Invalid material ID' });
|
|
}
|
|
|
|
if (!content || !content.trim()) {
|
|
return res.status(400).json({ success: false, error: 'Comment content is required' });
|
|
}
|
|
|
|
try {
|
|
const comment = await MaterialService.addComment(id, req.user!.id, content);
|
|
return res.status(201).json({ success: true, data: comment });
|
|
} catch (error) {
|
|
console.error('Error adding comment:', error);
|
|
return res.status(500).json({ success: false, error: 'Failed to add comment' });
|
|
}
|
|
}
|