<commit_before>var Keg = require('../models/keg');

module.exports = function(app, passport) {
    var express = require('express');
    var router = express.Router();

    router.post('/add', passport.checkAuth('admin'), function (req, res) {
        var newKeg = new Keg();

        newKeg.brewery_name = req.body.brewery_name;
        newKeg.beer_name = req.body.beer_name;
        newKeg.pint_price = req.body.pint_price;
        newKeg.total_price = req.body.total_price;
        newKeg.volume_oz = req.body.volume_oz;

        newKeg.save(function(err) {
            if (err) {
                res.apiRes(false,'Error Saving Keg',err);
            } else {
                res.apiRes(true,'Successfully Saved Keg', newKeg);
            }
        });
    });

    router.put('/update', passport.checkAuth('admin'), function(req, res) {
        Keg.findOne({'_id' :  req.body._id }, function(err, keg) {
            if(keg) {
                keg.brewery_name = req.body.brewery_name;
                keg.beer_name = req.body.beer_name;
                keg.pint_price = req.body.pint_price;
                keg.total_price = req.body.total_price;
                keg.volume_oz = req.body.volume_oz;

                keg.save(function (err) {
                    if (err) {
                        res.apiRes(false, 'Error Saving keg', err);
                    } else {
                        res.apiRes(true, 'Successfully Saved Keg', keg);
                    }
                });
            } else {
                res.apiRes(false,'Could Not Find Keg',keg);
            }
        });
    });

    router.post('/activate', passport.checkAuth('admin'), function(req, res){
        Keg.update({'is_active' : true},{'is_active' : false},null, function(){
            Keg.update({_id : req.body._id },{'is_active' : true},null, function(err){
                if(err) {
                    res.apiRes(false, 'Error Activating Keg', err);
                } else {
                    res.apiRes(true, 'Successfully Activated Keg', null);
                }
            });
        });
    });

    router.get('/', function (req, res) {
        Keg.findOne({'_id' :  req.query._id }, function(err, keg) {
            if(keg) {
                res.apiRes(true,'Successfully Retrieved Keg',keg);
            } else {
                res.apiRes(false,'Could Not Find Keg',keg);
            }
        });
    });

    router.get('/active', function (req, res) {
        Keg.findOne({'is_active' :  true }, function(err, keg) {
            if(keg) {
                res.apiRes(true,'Successfully Retrieved Active Keg',keg);
            } else {
                res.apiRes(false,'Could Not Find Active Keg',keg);
            }
        });
    });

    router.get('/list', function(req, res) {
        Keg.paginate({}, req.query.page, req.query.limit, function(err, pageCount, paginatedResults, itemCount) {
            if (err) {
                res.apiRes(false, 'Error finding kegs.', err);
            } else {
                res.apiRes(true, 'Successfully fetched kegs.', {
                    page: req.query.page,
                    limit: req.query.limit,
                    page_count: pageCount,
                    item_count: itemCount,
                    results: paginatedResults
                });
            }
        }, {sortBy: {created: -1}});
    });

    return router;
}<commit_msg>Created current keg stats service<commit_after>var Keg = require('../models/keg');
var Purchase = require('../models/purchase');

module.exports = function(app, passport) {
    var express = require('express');
    var router = express.Router();

    router.post('/add', passport.checkAuth('admin'), function (req, res) {
        var newKeg = new Keg();

        newKeg.brewery_name = req.body.brewery_name;
        newKeg.beer_name = req.body.beer_name;
        newKeg.pint_price = req.body.pint_price;
        newKeg.total_price = req.body.total_price;
        newKeg.volume_oz = req.body.volume_oz;

        newKeg.save(function(err) {
            if (err) {
                res.apiRes(false,'Error Saving Keg',err);
            } else {
                res.apiRes(true,'Successfully Saved Keg', newKeg);
            }
        });
    });

    router.put('/update', passport.checkAuth('admin'), function(req, res) {
        Keg.findOne({'_id' :  req.body._id }, function(err, keg) {
            if(keg) {
                keg.brewery_name = req.body.brewery_name;
                keg.beer_name = req.body.beer_name;
                keg.pint_price = req.body.pint_price;
                keg.total_price = req.body.total_price;
                keg.volume_oz = req.body.volume_oz;

                keg.save(function (err) {
                    if (err) {
                        res.apiRes(false, 'Error Saving keg', err);
                    } else {
                        res.apiRes(true, 'Successfully Saved Keg', keg);
                    }
                });
            } else {
                res.apiRes(false,'Could Not Find Keg',keg);
            }
        });
    });

    router.post('/activate', passport.checkAuth('admin'), function(req, res){
        Keg.update({'is_active' : true},{'is_active' : false},null, function(){
            Keg.update({_id : req.body._id },{'is_active' : true},null, function(err){
                if(err) {
                    res.apiRes(false, 'Error Activating Keg', err);
                } else {
                    res.apiRes(true, 'Successfully Activated Keg', null);
                }
            });
        });
    });

    router.get('/', function (req, res) {
        Keg.findOne({'_id' :  req.query._id }, function(err, keg) {
            if(keg) {
                res.apiRes(true,'Successfully Retrieved Keg',keg);
            } else {
                res.apiRes(false,'Could Not Find Keg',keg);
            }
        });
    });

    router.get('/active', function (req, res) {
        Keg.findOne({'is_active' :  true }, function(err, keg) {
            if(keg) {
                res.apiRes(true,'Successfully Retrieved Active Keg',keg);
            } else {
                res.apiRes(false,'Could Not Find Active Keg',keg);
            }
        });
    });

    router.get('/active/stats', function(req, res) {
        Keg.findOne({'is_active' : true}, function(err, keg) {
            Purchase.find({'keg' : keg.id,'cancelled' : true}, function(err, purchases) {
                if(purchases) {
                    var kegStats = {};
                    var collected = 0;
                    purchases.forEach(function(purchase) {
                        collected += purchase.price;
                    });

                    kegStats['total'] = keg.total_price;
                    kegStats['collected'] = collected;
                    kegStats['debt'] = keg.total_price - collected;

                    res.apiRes(true, 'Succesfully generated keg stats', kegStats);
                }
            });
        });
    });

    router.get('/list', function(req, res) {
        Keg.paginate({}, req.query.page, req.query.limit, function(err, pageCount, paginatedResults, itemCount) {
            if (err) {
                res.apiRes(false, 'Error finding kegs.', err);
            } else {
                res.apiRes(true, 'Successfully fetched kegs.', {
                    page: req.query.page,
                    limit: req.query.limit,
                    page_count: pageCount,
                    item_count: itemCount,
                    results: paginatedResults
                });
            }
        }, {sortBy: {created: -1}});
    });

    return router;
}
{'fraction_non_alphanumeric': 0.13990585774058578, 'fraction_numerical': 0.0003922594142259414, 'mean_word_length': 1.528595041322314, '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': '1634914', 'n_tokens_mistral': 2355, 'n_tokens_neox': 2243, 'n_words': 399}