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

async function update(req) {
  try {
    await sequelize.authenticate();
    let { name, description, cid } = req.body;

    let category = await Category.update(
      {
        name,
        description,
      },
      { where: { cid } },
    );

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

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

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

export default csrf(handler);
