<commit_before>var express = require("express");
var Twitter = require("twitter");

var config = require("../../config");
var Tweet = require("../../model/tweet");

var twitterClient = new Twitter(config.twitter);

var app = module.exports = express();

app.get("/favorites", getFavoriteTweets);
app.post("/favorites", addTweetToFavorites);
app.delete("/favorites/:id", removeTweetFromFavorites);

function getFavoriteTweets(req, res, next) {
  Tweet.find(function(err, favoriteTweets) {
    if (err) {
      return next(err);
    }
    res.send(favoriteTweets);
  });
}

  var tweet = req.body;
function addTweetToFavorites(req, res, next) {

  var tweetModel = new Tweet(tweet);
  tweetModel.save(function(err, favoriteTweets) {
    if (err) {
      return next(err);
    }
    res.send(favoriteTweets);
  });
}

function removeTweetFromFavorites(req, res, next) {
  var id = req.params.id;

  Tweet.findByIdAndRemove(id, function(err, removedTweet) {
    if (err) {
      return next(err);
    }
    res.send(removedTweet);
  });
}<commit_msg>update tweet if it is already exist<commit_after>var express = require("express");
var Twitter = require("twitter");

var config = require("../../config");
var Tweet = require("../../model/tweet");

var twitterClient = new Twitter(config.twitter);

var app = module.exports = express();

app.get("/favorites", getFavoriteTweets);
app.post("/favorites", addTweetToFavorites);
app.delete("/favorites/:id", removeTweetFromFavorites);

function getFavoriteTweets(req, res, next) {
  Tweet.find(function(err, favoriteTweets) {
    if (err) {
      return next(err);
    }
    res.send(favoriteTweets);
  });
}

function addTweetToFavorites(req, res, next) {
  var tweet = new Tweet(req.body).toObject();
  // delete the _id property, otherwise Mongo will return a "Mod on _id not allowed" error
  delete tweet._id;

  // update tweet (at least tweet.createdAt property) in db if it is already exist
  Tweet.findOneAndUpdate({ id: tweet.id }, tweet, { upsert: true }, function(err, favoriteTweets) {
    if (err) {
      return next(err);
    }
    res.send(favoriteTweets);
  });
}

function removeTweetFromFavorites(req, res, next) {
  var id = req.params.id;

  Tweet.findByIdAndRemove(id, function(err, removedTweet) {
    if (err) {
      return next(err);
    }
    res.send(removedTweet);
  });
}
{'fraction_non_alphanumeric': 0.1523687580025608, 'fraction_numerical': 0.0, 'mean_word_length': 3.9451476793248945, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2330696', 'n_tokens_mistral': 814, 'n_tokens_neox': 799, 'n_words': 179}