text: const { Comment, Pizza } = require("../models");
  
  const commentController = {
    // add comment to pizza
    // params is the url, body is the json body
    addComment({ params, body }, res) {
      console.log(body);
      Comment.create(body)
        .then(({ _id }) => {
          return Pizza.findOneAndUpdate(
            { _id: params.pizzaId },
            { $push: { comments: _id } }, //使用$push方法将评论添加_id到我们要更新的披萨中
            { new: true }
          );
        })
        .then((dbPizzaData) => {
          if (!dbPizzaData) {
            res.status(404).json({ message: "No pizza found with this id!" });
            return;
          }
          res.json(dbPizzaData);
        })
        .catch((err) => res.json(err));
    },
  
    // add reply to comment
    addReply({ params, body }, res) {
      Comment.findOneAndUpdate(
        { _id: params.commentId },
        // push has duplicates, but $addToSet avoids duplicates
        { $push: { replies: body } },
        { new: true, runValidators: true }
      )
        .then((dbPizzaData) => {
          if (!dbPizzaData) {
            res.status(404).json({ message: "No pizza found with this id!" });
            return;
          }
          res.json(dbPizzaData);
        })
        .catch((err) => res.json(err));
    },
  
    // remove comment
    // 我们不仅需要删除评论，还需要将其从与其关联的披萨中删除。f
    removeComment({ params }, res) {
      //First we'll delete the comment,
      Comment.findOneAndDelete({ _id: params.commentId })
        .then((deletedComment) => {
          if (!deletedComment) {
            return res.status(404).json({ message: "No comment with this id!" });
          }
          //then we'll use its _id to remove it from the pizza.
          return Pizza.findOneAndUpdate(
            { _id: params.pizzaId },
            { $pull: { comments: params.commentId } }, //take the data and use it to identify and remove it from the associated pizza
            { new: true }
          );
        })
        .then((dbPizzaData) => {
          if (!dbPizzaData) {
            res.status(404).json({ message: "No pizza found with this id!" });
            return;
          }
          res.json(dbPizzaData);
        })
        .catch((err) => res.json(err));
    },
  
    // remove reply
    removeReply({ params }, res) {
      Comment.findOneAndUpdate(
        { _id: params.commentId },
        // $pull to remove the specific reply from the replies array
        // where the replyId matches the value of params.replyId passed in from the route.
        { $pull: { replies: { replyId: params.replyId } } },
        // new: true returns the updated document
        { new: true }
      )
        .then((dbPizzaData) => res.json(dbPizzaData))
        .catch((err) => res.json(err));
    },
  };
  
  module.exports = commentController;
  