<commit_before>"use strict";

var express = require('express');
var Project = require('../schemas/project.js');

var app = express();

app.get('/projects/:id', function (req, res, next) {
  Project.findById(req.params.id).exec().then(function (project) {
    if(!project){
      res.send(404);
    }
    
    res.json({
      "project": project
    });
  }, next);
});

app.get('/projects', function (req, res, next) {
  // filter by user

  Project.find().exec().then(function (projects) {
    res.json({
      "projects": projects
    });
  }, next);
});

app.put('/projects/:id', function (req, res, next) {
  Project.findById(req.params.id).exec().then(function (project) {
    project.name = req.body.project.name;
    project.save();
    
    res.json({
      "project": project
    });
  }, next);
});

app.post('/projects', function (req, res, next) {
  var project = new Project({
    name: req.body.project.name
  });

  Project.create(project).then(function () {
    res.json({
      "project": project
    });
  }, next);

  // add project_id to user
});

app.delete('/projects/:id', function (req, res, next) {
  Project.findById(req.params.id).exec().then(function (project) {
    project.remove().exec().then(function () {
      res.send('');
    }, next)
  }, next);

  // remove project_id from user
});

module.exports = app;<commit_msg>Make sure users can only access projects that belong to them<commit_after>"use strict";

var express = require('express');
var Project = require('../schemas/project.js');
var User = require('../../schemas/userSchema.js');

var app = express();

app.get('/projects/:id', function (req, res, next) {
  // Check if this user can access this project
  User.findById(req.user._id).where('project_ids').in([req.params.id]).exec().then(function (allowed) {
    if (!allowed) {
      res.send(401);
    } else {
      Project.findById(req.params.id).exec().then(function (project) {
        if (!project) {
          res.send(404);
        }
        res.json({
          "project": project
        });
      }, next);
    }
  }, next);
});

app.get('/projects', function (req, res, next) {
  User.findById(req.user._id).exec().then(function (user) {
    Project.find({
      '_id': {
        $in: user.project_ids
      }
    }).exec().then(function (projects) {
      res.json({
        "projects": projects
      });
    }, next);
  })
});

app.put('/projects/:id', function (req, res, next) {
  Project.findById(req.params.id).exec().then(function (project) {
    project.name = req.body.project.name;
    project.save();

    res.json({
      "project": project
    });
  }, next);
});

app.post('/projects', function (req, res, next) {
  var project = new Project({
    name: req.body.project.name
  });

  Project.create(project).then(function () {
    User.findByIdAndUpdate(req.user._id, {
      $push: {
        project_ids: project._id
      }
    }, {
      safe: true,
      upsert: true
    }).exec().then(function () {
      res.json({
        "project": project
      });
    }, next);
  });
});

app.delete('/projects/:id', function (req, res, next) {
  Project.findById(req.params.id).exec().then(function (project) {
    project.remove().exec().then(function () {
      User.findByIdAndUpdate(req.user._id, {
        $pull: {
          project_ids: project._id
        }
      }).exec().then(function () {
        res.send(200);
      }, next);
    }, next)
  }, next);
});

module.exports = app;
{'fraction_non_alphanumeric': 0.19647500722334585, 'fraction_numerical': 0.0034672060098237503, 'mean_word_length': 2.6596194503171247, 'pattern_counts': {'":': 8, '<': 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': '3495297', 'n_tokens_mistral': 1130, 'n_tokens_neox': 1117, 'n_words': 216}