text: const { User, Thought } = require('../models');
  
  const userController = {
      // get all users
      getAllUser(req, res) {
          User.find({})
              .populate({
                  path: 'thoughts',
                  select: '-__v'
              })
              .select('-__v')
              .sort({ _id: -1 })
              .then(dbUserData => res.json(dbUserData))
              .catch(err => {
                  console.log(err);
                  res.sendStatus(400);
          });
      },
  
      // get one user by id
      getUserById({ params }, res) {
          User.findOne({ _id: params.id })
          .populate({
              path: 'thoughts',
              select: '-__v'
          })
          .populate({
              path: 'friends',
              select: '-__v'
          })
          .then(dbUserData => {
              if (!dbUserData) {
                  res.status(404).json({ message: 'User not found.'});
                  return;
              }
              res.json(dbUserData);
          })
          .catch(err => {
              console.log(err);
              res.sendStatus(400).json(err);
          });
      },
  
      // createUser
      createUser({ body }, res) {
          User.create(body)
              .then(dbUserData => res.json(dbUserData))
              .catch(err => res.json(err));
      },
  
      // update user by id
      updateUser({ params, body }, res) {
          User.findOneAndUpdate({ _id: params.id }, body, { new: true, runValidators: true })
          .then(dbUserData => {
              if (!dbUserData) {
                  res.status(404).json({ message: 'No user found with this id!' });
                  return;
              }
              res.json(dbUserData);
          })
          .catch(err => {
              console.log(err);
              res.status(400).json(err);
          });
      },
  
      // delete user
      deleteUser({ params }, res) {
          User.findOneAndDelete({ _id: params.id })
          .then(dbUserData => res.json(dbUserData))
          .catch(err => res.json(err));
      },
  
      // Add a friend to User
      addFriend({ params }, res) {
          User.findOneAndUpdate(
              { _id: params.userId },
              { $push: { friends: params.friendId }},
              { new: true }
          )
          .then((dbUserData) => {
              if (!dbUserData) {
                  res.status(404).json({ message: 'No user found with this ID'});
                  return;
              }
              res.json(dbUserData);
          })
          .catch((err) => res.status(400).json(err));
      },
  
      // Delete a friend fron User
      deleteFriend({ params }, res) {
          User.findOneAndUpdate(
              { _id: params.userId },
              { $pull: { friends: params.friendId }},
              { new: true }
          )
          .then((dbUserData) => {
              if (!dbUserData) {
                  res.status(404).json({ message: 'No user found with this ID'});
                  return;
              }
              res.json(dbUserData);
          })
          .catch((err) => res.status(400).json(err));
      }
  };
  
  module.exports = userController;
  