{"size": 2592, "ext": "js", "max_stars_repo_path": "api/src/app/controllers/RecipientController.js", "max_stars_repo_name": "tenclar/festfeeet", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "api/src/app/controllers/RecipientController.js", "max_issues_repo_name": "tenclar/festfeeet", "max_issues_repo_issues_event_min_datetime": "2021-10-06T13:58:08.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-13T08:37:14.000Z", "max_forks_repo_path": "api/src/app/controllers/RecipientController.js", "max_forks_repo_name": "tenclar/festfeeet", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 2, "max_forks_count": null, "avg_line_length": 24.9230769231, "max_line_length": 75, "alphanum_fraction": 0.5844907407}
import * as Yup from 'yup';

import { Op } from 'sequelize';
import Recipient from '../models/Recipient';

class RecipientController {
  async index(req, res) {
    const { q } = req.query;
    const recipients = q
      ? await Recipient.findAll({
          where: {
            name: {
              [Op.iLike]: `%${q}%`
            }
          }
        })
      : await Recipient.findAll();

    return res.json(recipients);
  }

  async show(req, res) {
    const paramId = req.params.id;
    const recipient = await Recipient.findByPk(paramId);
    return res.json(recipient);
  }

  async store(req, res) {
    const schema = Yup.object().shape({
      name: Yup.string().required(),
      street: Yup.string().required(),
      number: Yup.number().required(),
      complement: Yup.string().required(),
      city: Yup.string().required(),
      state: Yup.string().required(),
      cep: Yup.string().required()
    });
    if (!(await schema.isValid(req.body))) {
      return res.status(400).json({ error: 'Validation fields fails' });
    }

    const recipientExist = await Recipient.findOne({
      where: {
        name: req.body.name
      }
    });

    if (recipientExist) {
      return res.status(400).json({ error: 'Recipient already exist.' });
    }
    const recipient = await Recipient.create(req.body);
    return res.json(recipient);
  }

  async update(req, res) {
    const schema = Yup.object().shape({
      name: Yup.string(),
      street: Yup.string(),
      number: Yup.number(),
      complement: Yup.string(),
      city: Yup.string(),
      state: Yup.string(),
      cep: Yup.string()
    });
    if (!(await schema.isValid(req.body))) {
      return res.status(400).json({ error: 'Validation fields fails' });
    }

    const { name } = req.body;
    const paramId = req.params.id;

    const recipient = await Recipient.findByPk(paramId);

    if (name !== recipient.name) {
      const recipientExist = await Recipient.findOne({
        where: { name }
      });

      if (recipientExist) {
        return res.status(400).json({ error: 'Recipient already exist.' });
      }
    }

    await recipient.update(req.body);

    return res.json(recipient);
  }

  async remove(req, res) {
    const paramId = req.params.id;
    if (!paramId || !paramId.match(/^-{0,1}\d+$/)) {
      return res.status(400).json({ err: 'Recipient id not provided' });
    }

    const recipient = await Recipient.findByPk(paramId);

    await recipient.destroy();

    return res.json({ message: 'Recipient removed' });
  }
}

export default new RecipientController();
