const Order = require('../../../models/order');
import dbConnect from '../../../utils/helpers/dbHelpers';

async function handler(req, res) {
  const { method } = req;

  await dbConnect();

  switch (method) {
    case 'POST': {
      try {
        const order = await Order.create(
          req.body
        ); /* create a new model in the database */
        res
          .status(201)
          .json({ message: 'Your order was submitted successfully!', order });
      } catch (error) {
        res.status(400).json({ success: false });
      }
      break;
    }
    default:
      res.status(405).json({ message: 'Method not allowed' });
      break;
  }
}

export default handler;
