{"size": 3460, "ext": "js", "max_stars_repo_path": "source/server/routes/applications.js", "max_stars_repo_name": "bagabont/hotkey-ninja", "max_stars_repo_stars_event_min_datetime": "2015-02-06T23:07:35.000Z", "max_stars_repo_stars_event_max_datetime": "2015-02-12T21:09:16.000Z", "max_issues_repo_path": "source/server/routes/applications.js", "max_issues_repo_name": "bagabont/hotkey-ninja", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "source/server/routes/applications.js", "max_forks_repo_name": "bagabont/hotkey-ninja", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 29.5726495726, "max_line_length": 91, "alphanum_fraction": 0.4069364162}
var router = require('express').Router(),
    mongoose = require('mongoose'),
    Application = require('../models/application'),
    fs = require('fs');
;

module.exports = function (passport) {

    router.route('/')
        .get(function (req, res, next) {
            Application.find({}, function (err, models) {
                if (err) {
                    return next(err);
                }
                var applications = [];
                for (var i = 0; i < models.length; i++) {
                    var app = {
                        id: models[i].id,
                        name: models[i].name,
                        platform: models[i].platform,
                        shortcuts: models[i].shortcuts
                    };
                    applications.push(app);
                }
                res.send(applications);
            });
        });

    router.route('/create')
        .all(passport.authenticate('basic', {session: false}))
        .post(function (req, res, next) {
            Application.create(req.body, function (err) {
                if (err) {
                    return next(err);
                }
                res.status(201).send();
            });
        });

    router.param('id', function (req, res, next, id) {
        if (!mongoose.Types.ObjectId.isValid(id)) {
            return res.status(400).send();
        }
        var query = {
            _id: id
        };
        Application.findOne(query, function (err, model) {
            if (err) {
                return next(err)
            }
            req.app = model;
            next();
        });
    });

    router.route('/:id')
        .get(function (req, res, next) {
            var app = req.app;
            if (!app) {
                return res.status(404).send();
            }
            res.send({
                name: app.name,
                platform: app.platform,
                shortcuts: app.shortcuts
            });
        })
        .post(passport.authenticate('basic', {session: false}), function (req, res, next) {
            var app = req.app;
            if (!app) {
                return res.status(404).send();
            }
            app.name = req.body.name;
            app.shortcuts = req.body.shortcuts;

            app.save(function (err) {
                if (err) {
                    console.log(err);
                }
                res.send(app);
            });
        });

    router.route('/delete/:id')
        .post(passport.authenticate('basic', {session: false}), function (req, res, next) {
            console.log('looooool');
            var query = {
                _id: req.param("id")
            };
            Application.findOne(query, function (err, model) {
                if (err) {
                    return next(err)
                }
                Application.remove(model, function (err) {
                    if (err) {
                        return next(err);
                    }
                    return res.redirect("/admin");
                });
            });
        }
    );

    router.route('/create')
        .all(passport.authenticate('basic', {session: false}))
        .post(function (req, res, next) {
            Application.create(obj, function (err) {
                if (err) {
                    return next(err);
                }
                res.status(201).send();
            });
        });

    return router;
};
