Archived
updated handler
This commit is contained in:
+40
-18
@@ -1,32 +1,54 @@
|
|||||||
import { NextApiRequest, NextApiResponse } from 'next';
|
import { NextApiRequest, NextApiResponse } from 'next';
|
||||||
import { MongoClient } from 'mongodb';
|
import { MongoClient } from 'mongodb';
|
||||||
|
|
||||||
const uri = process.env.MONGODB_URI || 'dontstealmyuri';
|
const uri = process.env.MONGODB_URI || 'your-mongodb-uri';
|
||||||
const dbName = 'comments';
|
const dbName = 'confessions-of-grace';
|
||||||
|
|
||||||
const client = new MongoClient(uri);
|
const client = new MongoClient(uri);
|
||||||
|
|
||||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
if (req.method !== 'POST') {
|
if (req.method === 'POST') {
|
||||||
return res.status(405).json({ message: 'Method not allowed' });
|
const { name, email, comment, postId } = req.body;
|
||||||
}
|
|
||||||
|
|
||||||
const { name, email, comment, postId } = req.body;
|
if (!name || !email || !comment || !postId) {
|
||||||
|
return res.status(400).json({ message: 'All fields are required' });
|
||||||
|
}
|
||||||
|
|
||||||
if (!name || !email || !comment || !postId) {
|
try {
|
||||||
return res.status(400).json({ message: 'All fields are required' });
|
if (!client.db(dbName)) await client.connect();
|
||||||
}
|
const db = client.db(dbName);
|
||||||
|
const collection = db.collection('comments');
|
||||||
|
|
||||||
try {
|
await collection.insertOne({ name, email, comment, postId, createdAt: new Date() });
|
||||||
if (!client.db(dbName)) await client.connect();
|
|
||||||
const db = client.db(dbName);
|
|
||||||
const collection = db.collection('comments');
|
|
||||||
|
|
||||||
await collection.insertOne({ name, email, comment, postId, createdAt: new Date() });
|
res.status(201).json({ message: 'Comment submitted successfully' });
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
res.status(500).json({ message: 'Internal server error' });
|
||||||
|
}
|
||||||
|
} else if (req.method === 'GET') {
|
||||||
|
const { postId } = req.query;
|
||||||
|
|
||||||
res.status(201).json({ message: 'Comment submitted successfully' });
|
if (!postId) {
|
||||||
} catch (error) {
|
return res.status(400).json({ message: 'Post ID is required' });
|
||||||
console.error(error);
|
}
|
||||||
res.status(500).json({ message: 'Internal server error' });
|
|
||||||
|
try {
|
||||||
|
if (!client.db(dbName)) await client.connect();
|
||||||
|
const db = client.db(dbName);
|
||||||
|
const collection = db.collection('comments');
|
||||||
|
|
||||||
|
const comments = await collection
|
||||||
|
.find({ postId })
|
||||||
|
.sort({ createdAt: -1 })
|
||||||
|
.toArray();
|
||||||
|
|
||||||
|
res.status(200).json(comments);
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
res.status(500).json({ message: 'Internal server error' });
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
res.status(405).json({ message: 'Method not allowed' });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user