<commit_before>import models from '../models';

const upvote = models.Upvote;
const downvote = models.Downvote;
const recipe = models.Recipe;

/**
 * @exports upvoteRecipe
 * @param  {obj} req request object
 * @param  {obj} res result object
 * @return {obj}  newUser object
 */
export const upvoteRecipe = (req, res) => {
  const userId = req.userId;
  const recipeId = req.params.recipeId;

  downvote
    .findOne({
      attributes: ['id'],
      where: {
        $and: [
          { userId },
          { recipeId }
        ]
      }
    })
    .then((voteFound) => {
      if (voteFound) {
        downvote
          .destroy({
            where: {
              $and: [
                { userId },
                { recipeId }
              ]
            }
          })
          .then(() => {
            recipe
              .findOne({
                where: {
                  id: recipeId
                }
              }).then((option) => {
                option.decrement('downvotes');
              });
          });
      }
    });

  const newUpvote = upvote
    .findOrCreate({ where: { userId, recipeId } })
    .spread((createdVote, created) => {
      if (created) {
        recipe
          .findOne({
            where: {
              id: recipeId
            }
          }).then((option) => {
            option.increment('upvotes');
          });
        return res.status(201).send({ message: 'Recipe Upvoted!' });
      }

      return res.status(201).send({ message: 'Already Upvoted!' });
    })
    .catch(() => res.status(401).send({ error: 'Error Upvoting Review' }));

  return newUpvote;
};

/**
 * @exports downvoteRecipe
 * @param  {obj} req request object
 * @param  {obj} res result object
 * @return {obj}  newUser object
 */
export const downvoteRecipe = (req, res) => {
  const userId = req.userId;
  const recipeId = req.params.recipeId;

  upvote
    .findOne({
      attributes: ['id'],
      where: {
        $and: [
          { userId },
          { recipeId }
        ]
      }
    })
    .then((voteFound) => {
      if (voteFound) {
        upvote
          .destroy({
            where: {
              $and: [
                { userId },
                { recipeId }
              ]
            }
          })
          .then(() => {
            recipe
              .findOne({
                where: {
                  id: recipeId
                }
              }).then((option) => {
                option.decrement('upvotes');
              });
          });
      }
    });

  const newDownvote = downvote
    .findOrCreate({ where: { userId, recipeId } })
    .spread((createdVote, created) => {
      if (created) {
        recipe
          .findOne({
            where: {
              id: recipeId
            }
          }).then((option) => {
            option.increment('downvotes');
          });

        return res.status(201).send({ message: 'Recipe Downvoted!' });
      }

      return res.status(201).send({ message: 'Already Downvoted!' });
    })
    .catch(() => res.status(401).send({ error: 'Error Downvoting Review' }));

  return newDownvote;
};

/**
 * @exports getUserUpvotes
 * @param  {obj} req request object
 * @param  {obj} res result object
 * @return {obj}  newUser object
 */
export const getUserUpvotes = (req, res) => {
  const recipeId = req.params.recipeId;
  const upvotes = upvote
    .findAll({
      attributes: ['recipId'],
      where: { recipeId },
      include: [
        { model: models.User, attributes: ['name'] }
      ]
    })
    .then((foundVotes) => {
      if (!foundVotes) {
        return res.status(201).send({
          message: 'No User Upvoted this Recipe!',
        });
      }

      return res.status(201).send(foundVotes);
    })
    .catch(() => res.status(401).send('Unable to get user upvotes'));

  return upvotes;
};

/**
 * @exports getUserDownvotes
 * @param  {obj} req request object
 * @param  {obj} res result object
 * @return {obj}  newUser object
 */
export const getUserDownvotes = (req, res) => {
  const recipeId = req.params.recipeId;
  const downvotes = downvote
    .findAll({
      attributes: ['recipId'],
      where: { recipeId },
      include: [
        { model: models.User, attributes: ['name'] }
      ]
    })
    .then((foundVotes) => {
      if (!foundVotes) {
        return res.status(201).send({
          message: 'No User Downvoted this Recipe!',
        });
      }

      return res.status(201).send(foundVotes);
    })
    .catch(() => res.status(401).send('Unable to get user downvotes'));

  return downvotes;
};
<commit_msg>Add success status to votes controller<commit_after>import models from '../models';

const upvote = models.Upvote;
const downvote = models.Downvote;
const recipe = models.Recipe;

/**
 * @exports upvoteRecipe
 * @param  {obj} req request object
 * @param  {obj} res result object
 * @return {obj}  newUser object
 */
export const upvoteRecipe = (req, res) => {
  const userId = req.userId;
  const recipeId = req.params.recipeId;

  downvote
    .findOne({
      attributes: ['id'],
      where: {
        $and: [
          { userId },
          { recipeId }
        ]
      }
    })
    .then((voteFound) => {
      if (voteFound) {
        downvote
          .destroy({
            where: {
              $and: [
                { userId },
                { recipeId }
              ]
            }
          })
          .then(() => {
            recipe
              .findOne({
                where: {
                  id: recipeId
                }
              }).then((option) => {
                option.decrement('downvotes');
              });
          });
      }
    });

  const newUpvote = upvote
    .findOrCreate({ where: { userId, recipeId } })
    .spread((createdVote, created) => {
      if (created) {
        recipe
          .findOne({
            where: {
              id: recipeId
            }
          }).then((option) => {
            option.increment('upvotes');
          });
        return res.status(201).send({
          success: true,
          message: 'Recipe Upvoted!' });
      }

      return res.status(201).send({
        success: false,
        message: 'Recipe Already Upvoted!' });
    })
    .catch(() => res.status(401).send({
      success: false,
      message: 'Error Upvoting Review' }));

  return newUpvote;
};

/**
 * @exports downvoteRecipe
 * @param  {obj} req request object
 * @param  {obj} res result object
 * @return {obj}  newUser object
 */
export const downvoteRecipe = (req, res) => {
  const userId = req.userId;
  const recipeId = req.params.recipeId;

  upvote
    .findOne({
      attributes: ['id'],
      where: {
        $and: [
          { userId },
          { recipeId }
        ]
      }
    })
    .then((voteFound) => {
      if (voteFound) {
        upvote
          .destroy({
            where: {
              $and: [
                { userId },
                { recipeId }
              ]
            }
          })
          .then(() => {
            recipe
              .findOne({
                where: {
                  id: recipeId
                }
              }).then((option) => {
                option.decrement('upvotes');
              });
          });
      }
    });

  const newDownvote = downvote
    .findOrCreate({ where: { userId, recipeId } })
    .spread((createdVote, created) => {
      if (created) {
        recipe
          .findOne({
            where: {
              id: recipeId
            }
          }).then((option) => {
            option.increment('downvotes');
          });

        return res.status(201).send({
          success: true,
          message: 'Recipe Downvoted!' });
      }

      return res.status(201).send({
        success: false,
        message: 'Recipe Already Downvoted!' });
    })
    .catch(() => res.status(401).send({
      success: true,
      message: 'Error Downvoting Review' }));

  return newDownvote;
};

/**
 * @exports getUserUpvotes
 * @param  {obj} req request object
 * @param  {obj} res result object
 * @return {obj}  newUser object
 */
export const getUserUpvotes = (req, res) => {
  const recipeId = req.params.recipeId;
  const upvotes = upvote
    .findAll({
      attributes: ['recipId'],
      where: { recipeId },
      include: [
        { model: models.User, attributes: ['name'] }
      ]
    })
    .then((foundVotes) => {
      if (!foundVotes) {
        return res.status(201).send({
          success: true,
          message: 'No User Upvoted this Recipe!'
        });
      }

      foundVotes.success = true;
      return res.status(201).send(foundVotes);
    })
    .catch(() => res.status(401).send({
      success: true,
      message: 'Unable to get user upvotes' }));

  return upvotes;
};

/**
 * @exports getUserDownvotes
 * @param  {obj} req request object
 * @param  {obj} res result object
 * @return {obj}  newUser object
 */
export const getUserDownvotes = (req, res) => {
  const recipeId = req.params.recipeId;
  const downvotes = downvote
    .findAll({
      attributes: ['recipId'],
      where: { recipeId },
      include: [
        { model: models.User, attributes: ['name'] }
      ]
    })
    .then((foundVotes) => {
      if (!foundVotes) {
        return res.status(201).send({
          success: true,
          message: 'No User Downvoted this Recipe!'
        });
      }

      foundVotes.success = true;
      return res.status(201).send(foundVotes);
    })
    .catch(() => res.status(401).send({
      success: false,
      message: 'Unable to get user downvotes' }));

  return downvotes;
};

{'fraction_non_alphanumeric': 0.14507232802580913, 'fraction_numerical': 0.007492975335622854, 'mean_word_length': 1.5791733762748255, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2616285', 'n_tokens_mistral': 2944, 'n_tokens_neox': 2867, 'n_words': 645}