2025-04-02 17:47:30 -05:00
|
|
|
import { createDraft } from '@/db/models/draftModels';
|
|
|
|
import { getServerSession } from 'next-auth/next';
|
|
|
|
import { authOptions } from '@/pages/api/auth/[...nextauth]';
|
2024-03-25 13:39:32 -05:00
|
|
|
|
|
|
|
export default async function handler(req, res) {
|
2025-04-02 17:47:30 -05:00
|
|
|
const session = await getServerSession(req, res, authOptions);
|
2024-10-02 17:27:38 -05:00
|
|
|
|
|
|
|
if (!session) {
|
|
|
|
return res.status(401).json({ error: 'Unauthorized' });
|
|
|
|
}
|
|
|
|
|
2024-03-25 13:39:32 -05:00
|
|
|
if (req.method === 'POST') {
|
|
|
|
try {
|
|
|
|
const draft = await createDraft(req.body);
|
|
|
|
if (draft) {
|
|
|
|
res.status(201).json(draft);
|
|
|
|
} else {
|
|
|
|
res.status(400).json({ error: 'Draft not created' });
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
res.status(400).json({ error: error.message });
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Handle any other HTTP method
|
|
|
|
res.setHeader('Allow', ['GET', 'POST']);
|
|
|
|
res.status(405).end(`Method ${req.method} Not Allowed`);
|
|
|
|
}
|
|
|
|
}
|