import { csrf } from 'src/lib/csrf';
import { Category, sequelize } from './model';

async function destroyCategory(req) {
  try {
    await sequelize.authenticate();
    let { cid } = req.query;

    let category = await Category.findOne({ where: { cid } });
    await category.destroy();

    return { status: 'OK' };
  } catch (error) {
    console.error(error);
  }
}

async function handler(req, res) {
  try {
    let result = await destroyCategory(req);

    return res.status(200).send(result);
  } catch (err) {
    console.log(err);
    res.status(200).send({ status: 'error', message: 'helloworld error' });
  }
}

export default csrf(handler);
