text: import * as Yup from 'yup';
  import Plan from '../models/Plan';
  
  class PlanController {
    async index(req, res) {
      const plans = await Plan.findAll();
  
      return res.json(plans);
    }
  
    async store(req, res) {
      const schema = Yup.object().shape({
        title: Yup.string()
          .min(4)
          .required(),
        duration: Yup.number()
          .min(1)
          .required(),
        price: Yup.number().required(),
      });
  
      if (!(await schema.isValid(req.body))) {
        return res.status(400).json({ error: 'Validation fails' });
      }
  
      const { title } = req.body;
  
      const plan = await Plan.findOne({ where: { title } });
  
      if (plan) {
        return res.status(400).json({ error: 'Plan already exists.' });
      }
  
      const { id, duration, price } = await Plan.create(req.body);
  
      return res.json({
        id,
        title,
        duration,
        price,
      });
    }
  
    async update(req, res) {
      const schema = Yup.object().shape({
        title: Yup.string().min(4),
        duration: Yup.number().min(1),
        price: Yup.number(),
      });
  
      if (!(await schema.isValid(req.body))) {
        return res.status(400).json({ error: 'Validation fails' });
      }
  
      const { id } = req.params;
  
      const plan = await Plan.findByPk(id);
  
      if (!plan) {
        return res.status(400).json({ error: 'Plan does not exists' });
      }
  
      const updatedPlan = await plan.update(req.body);
  
      return res.json(updatedPlan);
    }
  
    async delete(req, res) {
      const { id } = req.params;
  
      const plan = await Plan.findByPk(id);
  
      if (!plan) {
        return res.status(400).json({ error: 'Plan does not exists' });
      }
  
      return res.json(await plan.destroy());
    }
  }
  
  export default new PlanController();
  