2025.11.27.17.50
This commit is contained in:
37
src/pages/api/admin/plans/[id].ts
Normal file
37
src/pages/api/admin/plans/[id].ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||
import { MembershipPlan } from '@/models';
|
||||
import withDatabase from '@/lib/withDatabase';
|
||||
|
||||
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
const { id } = req.query;
|
||||
|
||||
if (req.method === 'GET') {
|
||||
try {
|
||||
const plan = await MembershipPlan.findById(id);
|
||||
if (!plan) return res.status(404).json({ message: 'Plan not found' });
|
||||
return res.status(200).json(plan);
|
||||
} catch (error) {
|
||||
return res.status(500).json({ message: 'Failed to fetch plan' });
|
||||
}
|
||||
} else if (req.method === 'PUT') {
|
||||
try {
|
||||
const plan = await MembershipPlan.findByIdAndUpdate(id, req.body, { new: true });
|
||||
if (!plan) return res.status(404).json({ message: 'Plan not found' });
|
||||
return res.status(200).json(plan);
|
||||
} catch (error) {
|
||||
return res.status(500).json({ message: 'Failed to update plan' });
|
||||
}
|
||||
} else if (req.method === 'DELETE') {
|
||||
try {
|
||||
const plan = await MembershipPlan.findByIdAndDelete(id);
|
||||
if (!plan) return res.status(404).json({ message: 'Plan not found' });
|
||||
return res.status(200).json({ message: 'Plan deleted' });
|
||||
} catch (error) {
|
||||
return res.status(500).json({ message: 'Failed to delete plan' });
|
||||
}
|
||||
} else {
|
||||
return res.status(405).json({ message: 'Method not allowed' });
|
||||
}
|
||||
}
|
||||
|
||||
export default withDatabase(handler);
|
||||
Reference in New Issue
Block a user