<commit_before>// app/routes/project.js

/**
 * Model dependencies
 */
var Project = require('./../models/project');

/**
 * Expose project routes
 */
module.exports = function(app, passport) {

    /**
     * Get all the projects from the database.
     */
    app.get('/api/projects', function (req, res) {
        Project.find(function (err, projects) {
            if (err) {
                return res.send(err);
            }

            res.json(projects);
        });
    });

    /**
     * Create a project based on the data provided in the request 
     * and save it in the database.
     * @see isLoggedInAjax()
     */
    app.post('/api/projects', isLoggedInAjax, function (req, res) {
        Project.create({
            title: req.body.title,
            content: req.body.content,
            grade: req.body.grade,
            students: req.body.students,
            semester: req.body.semester
        }, function (err, project) {
            if (err) {
                res.send(err);
            }

            res.json(project);
        });
    });

    /**
     * Get a project based on the id provided in the request url
     */
    app.get('/api/projects/:_id', function (req, res) {
        Project.findById(req.params._id, function (err, project) {
            if (err) {
                res.send(err);
            }

            res.json(project);
        });
    });

    /**
     * Update a project based on the id provided in the request url and 
     * the data provided in the request.
     * @see isLoggedInAjax()
     */
    app.put('/api/projects/:_id', isLoggedInAjax, function (req, res) {
        Project.findById(req.params._id, function (err, test) {
            if (err) {
                res.send(err);
            }

            test.title = req.body.title;
            test.url = req.body.url;
            test.method = req.body.method;
            test.param = req.body.param;
            test.data = req.body.data;
            test.collectionid = req.body.collectionid;
            
            test.save(function (err) {
                if (err) {
                    res.send(err);
                }

                res.json(test);
            });
        });
    });
    
    /**
     * Delete a project based on the id provided in the request.
     * @see isLoggedInAjax()
     */
    app.delete('/api/projects/:_id', isLoggedInAjax, function (req, res) {
        Project.remove({
            _id: req.params._id
        }, function (err, project) {
            if (err) {
                res.send(err);
            }

            res.json(project);
        });
    });

};

/**
 * Middleware to check if the user is logged in using ajax get request
 * @param  {Object}   req  Request object
 * @param  {Object}   res  Response object
 * @param  {Function} next Next route
 * @return {Boolean} Return json if the user is not logged in else go the next route.
 */
function isLoggedInAjax(req, res, next) {
    if (!req.isAuthenticated()) {<commit_msg>Return projects after deletion<commit_after>// app/routes/project.js

/**
 * Model dependencies
 */
var Project = require('./../models/project');

/**
 * Expose project routes
 */
module.exports = function(app, passport) {

    /**
     * Get all the projects from the database.
     */
    app.get('/api/projects', function (req, res) {
        Project.find(function (err, projects) {
            if (err) {
                return res.send(err);
            }

            res.json(projects);
        });
    });

    /**
     * Create a project based on the data provided in the request 
     * and save it in the database.
     * @see isLoggedInAjax()
     */
    app.post('/api/projects', isLoggedInAjax, function (req, res) {
        Project.create({
            title: req.body.title,
            content: req.body.content,
            grade: req.body.grade,
            students: req.body.students,
            semester: req.body.semester
        }, function (err, project) {
            if (err) {
                res.send(err);
            }

            res.json(project);
        });
    });

    /**
     * Get a project based on the id provided in the request url
     */
    app.get('/api/projects/:_id', function (req, res) {
        Project.findById(req.params._id, function (err, project) {
            if (err) {
                res.send(err);
            }

            res.json(project);
        });
    });

    /**
     * Update a project based on the id provided in the request url and 
     * the data provided in the request.
     * @see isLoggedInAjax()
     */
    app.put('/api/projects/:_id', isLoggedInAjax, function (req, res) {
        Project.findById(req.params._id, function (err, test) {
            if (err) {
                res.send(err);
            }

            test.title = req.body.title;
            test.url = req.body.url;
            test.method = req.body.method;
            test.param = req.body.param;
            test.data = req.body.data;
            test.collectionid = req.body.collectionid;
            
            test.save(function (err) {
                if (err) {
                    res.send(err);
                }

                res.json(test);
            });
        });
    });
    
    /**
     * Delete a project based on the id provided in the request.
     * @see isLoggedInAjax()
     */
    app.delete('/api/projects/:_id', isLoggedInAjax, function (req, res) {
        Project.remove({
            _id: req.params._id
        }, function (err, project) {
            if (err) {
                res.send(err);
            }

            Project.find(function(err, projects) {
                if (err) {
                    res.send(err);
                }

                res.json(projects);
            });
        });
    });

};

/**
 * Middleware to check if the user is logged in using ajax get request
 * @param  {Object}   req  Request object
 * @param  {Object}   res  Response object
 * @param  {Function} next Next route
 * @return {Boolean} Return json if the user is not logged in else go the next route.
 */
function isLoggedInAjax(req, res, next) {
    if (!req.isAuthenticated()) {
{'fraction_non_alphanumeric': 0.12799352750809062, 'fraction_numerical': 0.0, 'mean_word_length': 1.5126016260162602, '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': '4586291', 'n_tokens_mistral': 1812, 'n_tokens_neox': 1761, 'n_words': 462}