<commit_before>var express = require("express");
var router = express.Router();
var Profile = require("../models/profile")
var Stamp = require("../models/stamp")
var User = require("../models/user")

function error(response, message){
  response.status(500);
  response.json({error: message})
}

var profilesController = {
  getProfiles: function(req,res){
    if (req.params.format){
      Profile.find({}).populate("stamps").then(function(profiles){
        res.json(profiles);//response renders json of profiles
      });
    }else{
      Profile.find({}).populate("stamps").then(function(profiles){
        res.render("index")
      })
    }
  },
  getProfile: function(req,res){
    Profile.findById(req.params.id).populate("stamps").then(function(profile){
      res.json(profile);
    });
  },
  addProfile:function(req,res){
    // Profile.findOne({'username': req.body}, function(err, profile){
    //   if(err) return err;
    //   // If the profile already exists, just return that user.
    //   if(profile){
    //     return profile;
    //   } else {
    //     var newProfile = new Profile(req.body)
    //     newProfile.save(function(err){
    //       if(!err){
    //         return profile;
    //       }
    //     })
    //   }
    // }).then(function(profile){
    //   User.findById(req.user._id,function(err,docs){
    //     docs.follows.push(profile._id);
    //     docs.save(function(err){
    //       if(!err){
    //         res.json(profile)
    //       }
    //     })
    //   })
    // })
    new Profile(req.body).save().then(function(profile){
      User.findById(req.user._id, function(err,docs){
        docs.follows.push(profile._id);
        docs.save(function(err){
          console.log(profile.username+ " added to the db")
          if(!err){
            res.json(profile)
          }
        });
      });
    });
  },
  updateProfile:function(req,res){
    Profile.findByIdAndUpdate(req.params.id, {$set: req.body}, {new: true}).then(function(profile){
      res.json(profile);
    });
  },
  deleteProfile:function(req,res){
    Profile.findByIdAndRemove(req.params.id).then(function(profile){
      console.log("removing "+profile.username+" from the db...")
      res.json({success: true});
    });
  },
  getStamps:function(req,res){
    Profile.findById(req.params.id).populate("stamps").then(function(profile){
      res.json(profile.stamps);
    });
  },
  addStamp:function(req,res){
    var token = req.user.github.token;
    Profile.findById(req.params.id,function(err,docs){
      var stamp = new Stamp(req.body);
      var git = stamp.setUp(token);
      stamp.getMsgs(docs.username, git);
      docs.stamps.push(stamp);
      docs.save(function(err){
        if(!err){
          res.json(stamp);
        }
      });
    });
  }
}

module.exports = profilesController;
<commit_msg>create profile if it doesn't exist<commit_after>var express = require("express");
var router = express.Router();
var Profile = require("../models/profile")
var Stamp = require("../models/stamp")
var User = require("../models/user")

function error(response, message){
  response.status(500);
  response.json({error: message})
}

function followProfile(userId,profile,res){
  User.findById(userId, function(err,docs){
    if(err) return err;
    docs.follows.push(profile._id);
    docs.save(function(error){
      console.log(profile.username+ " followed by "+docs.github.username)
      if(!error){
        res.json(profile)
      }
    });
  });
}

var profilesController = {
  getProfiles: function(req,res){
    if (req.params.format){
      Profile.find({}).populate("stamps").then(function(profiles){
        res.json(profiles);//response renders json of profiles
      });
    }else{
      Profile.find({}).populate("stamps").then(function(profiles){
        res.render("index")
      })
    }
  },
  getProfile: function(req,res){
    Profile.findById(req.params.id).populate("stamps").then(function(profile){
      res.json(profile);
    });
  },
  addProfile:function(req,res){
    Profile.findOne({'username':req.body.username}, function(err, profile){
      if(err) return err;
      // If the profile already exists, just return that profile.
      if (profile){
        console.log(req.body.username + " already in the database")
        followProfile(req.user._id,profile,res)
      }else{
        var newProfile = new Profile(req.body).save(function(err){
          if(!err){
            console.log(req.body.username + " saved in the database")
            return newProfile
          }
        }).then(function(newProfile){followProfile(req.user._id,newProfile,res)});
      }
    })
    //   if(profile){
    //     User.findById(req.user._id,function(err,docs){
    //       docs.follows.push(profile._id);
    //       docs.save(function(err){
    //         if(!err){
    //           res.json(profile)
    //         }
    //       })
    //     })
    //   } else {
    //     new Profile(req.body).save().then(function(profile){
    //       User.findById(req.user._id, function(err,docs){
    //         docs.follows.push(profile._id);
    //         docs.save(function(err){
    //           console.log(profile.username+ " added to the db")
    //           if(!err){
    //             res.json(profile)
    //           }
    //         });
    //       });
    //     });
    //   }
    // })
  },
  updateProfile:function(req,res){
    Profile.findByIdAndUpdate(req.params.id, {$set: req.body}, {new: true}).then(function(profile){
      res.json(profile);
    });
  },
  deleteProfile:function(req,res){
    Profile.findByIdAndRemove(req.params.id).then(function(profile){
      console.log("removing "+profile.username+" from the db...")
      res.json({success: true});
    });
  },
  unfollowProfile:function(req,res){

  },
  getStamps:function(req,res){
    Profile.findById(req.params.id).populate("stamps").then(function(profile){
      res.json(profile.stamps);
    });
  },
  addStamp:function(req,res){
    var token = req.user.github.token;
    Profile.findById(req.params.id,function(err,docs){
      var stamp = new Stamp(req.body);
      var git = stamp.setUp(token);
      stamp.getMsgs(docs.username, git);
      docs.stamps.push(stamp);
      docs.save(function(err){
        if(!err){
          res.json(stamp);
        }
      });
    });
  }
}

module.exports = profilesController;

{'fraction_non_alphanumeric': 0.1747922871923499, 'fraction_numerical': 0.0009405862987929142, 'mean_word_length': 2.4375, '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': '648225', 'n_tokens_mistral': 2035, 'n_tokens_neox': 2016, 'n_words': 285}