const List = require("../../models/list");
const config = require("../../config");

exports.addlist = async (req, res) => {
  try {
    const addlistData = req.body;
    await List.create({
      name: addlistData.name,
      price: addlistData.price,
      time: addlistData.time,
      shop: addlistData.shop,
      userId: addlistData.userId
    });
    res.send("create");
  } catch (err) {
    console.log(err);
    res.sendStatus(401);
  }
};

exports.getList = async (req, res) => {
  try {
    let list = await List.findAll();

    res.status(200).send(list);
  } catch (err) {
    console.log(err);
    res.sendStatus(401);
  }
};

exports.deletelist = async (req, res) => {
  try {
    const listId = req.params.listId;
    await List.destroy({
      where: {
        id: listId
      }
    });
    res.status(200).send("success");
  } catch (err) {
    console.log(err);
    res.sendStatus(401);
  }
};

exports.getListuser = async (req, res) => {
  try {
    const listuser = req.params.userId;
    let list = await List.findAll({
      where: {
        userId: listuser
      }
    });
    console.log('list', list)
    res.status(200).send(list);
  } catch (err) {
    // console.log(err);
    // res.sendStatus(401);
    res.send({
      error: err.message
    });
  }
};
exports.getListshop = async (req, res) => {
  try {
    const listshop = req.params.shop;
    let list = await List.findAll({
      where: {
        shop: listshop
      }
    });
    console.log('list', list)
    res.status(200).send(list);
  } catch (err) {
    // console.log(err);
    // res.sendStatus(401);
    res.send({
      error: err.message
    });
  }
};

exports.updateList = async (req, res) => {
  try {
    const listData = req.body;
    const id = req.params.id;
    await List.update(listData, { where: { id: id } });
    res.status(200).send({ status: "done" });
  } catch (err) {
    console.log(err);
    res.sendStatus(401);
  }
};
