text: // Imports user model
  const { User } = require("../models");
  // Imports sign token function from auth
  const { signToken } = require("../utils/auth");
  
  module.exports = {
    // Get a single user by either their id or their username
    async getSingleUser({ user = null, params }, res) {
      const foundUser = await User.findOne({
        $or: [
          { _id: user ? user._id : params.id },
          { username: params.username },
        ],
      });
  
      if (!foundUser) {
        return res
          .status(400)
          .json({ message: "Sorry, cannot find a user with this id!" });
      }
  
      res.json(foundUser);
    },
    // Creates a user, sign a token, and send it back (to client/src/components/SignUpForm.js)
    async createUser({ body }, res) {
      const user = await User.create(body);
  
      if (!user) {
        return res.status(400).json({ message: "Something is wrong!" });
      }
      const token = signToken(user);
      res.json({ token, user });
    },
    // Login a user, sign a token, and send it back (to client/src/components/LoginForm.js)
    async login({ body }, res) {
      const user = await User.findOne({
        $or: [{ username: body.username }, { email: body.email }],
      });
      if (!user) {
        return res.status(400).json({ message: "Sorry, Cannot find this user" });
      }
  
      const correctPw = await user.isCorrectPassword(body.password);
  
      if (!correctPw) {
        return res.status(400).json({ message: "Wrong password!" });
      }
      const token = signToken(user);
      res.json({ token, user });
    },
    // Save a book to a user's `savedBooks` field by adding it to the set (to prevent duplicates)
    async saveBook({ user, body }, res) {
      console.log(user);
      try {
        const updatedUser = await User.findOneAndUpdate(
          { _id: user._id },
          { $addToSet: { savedBooks: body } },
          { new: true, runValidators: true }
        );
        return res.json(updatedUser);
      } catch (err) {
        console.log(err);
        return res.status(400).json(err);
      }
    },
    // Remove a book from `savedBooks`
    async deleteBook({ user, params }, res) {
      const updatedUser = await User.findOneAndUpdate(
        { _id: user._id },
        { $pull: { savedBooks: { bookId: params.bookId } } },
        { new: true }
      );
      if (!updatedUser) {
        return res
          .status(404)
          .json({ message: "Sorry, cannot find a user with this id!" });
      }
      return res.json(updatedUser);
    },
  };
  