var express = require('express');
var router = express.Router({mergeParams: true});
var Key = require('../models/keys');
var middleware = require('../middleware');

router.get('/', function(req, res) {
    Key.find({}, function(err, allKeys){
        if(err){
            console.log(err);
        } else {
            res.render('keys/index', {
                keys: allKeys
            });
        }
    });
});

router.get("/search", function(req, res) {
    "use strict";

    var query = req.query.query ? req.query.query : "";

    var searchItems = function(query, callback) {

        var keys = [];

        var numItems = 0;
        
        var cursor2 = Key.aggregate([
            { $match: {$text: { $search: query } } }, 
            { $group: {_id: null, num: {$sum: 1}}}
        ], function (err, result) {

            if (err) {
                next(err);
            } else {

                if (result.length > 0) {
                    numItems = result[0].num                    
                } else {
                    numItems = 0
                }

                var cursor = Key
                .find({ $text: { $search: query } })
                .sort({"title" : 1 }).stream()
                .on('data', function(doc){
                    keys.push(doc);
                })
                .on('error', function(err){
                    assert.equal(err, null);
                })
                .on('end', function(){
                    callback(res.render('search', { 
                    query: query,
                    keys: keys,
                    numItems: numItems }));
                });
            }
        });
    }

    searchItems(query, function() {
        
    });

});

router.get('/:id', function(req, res) {
    Key.findById(req.params.id, function(err, foundKey) {
        res.render('keys/show', {keys: foundKey});
    });
});

router.get('/:id/edit', middleware.isLoggedIn, function(req, res) {
    Key.findById(req.params.id, function(err, foundKey) {
        res.render('keys/edit', {key: foundKey});
    });
});

router.put('/:id', middleware.isLoggedIn, function(req, res){
    Key.findByIdAndUpdate(req.params.id, function(err, foundKey){
        if(err){
            res.redirect('/keys');
        } else {
            res.redirect('/keys/' + req.params.id);
        }
    });
});

router.delete('/:id', middleware.isLoggedIn, function(req, res){
    Key.findByIdAndRemove(req.params.id, function(err){
        if (err){
            res.redirect('/keys');
        } else {
            res.redirect('/keys');
        }
    });
});



module.exports = router;
@@ -186,32 +186,632 @@
 tion(req, res) %7B
+%0A%0A    var allTags = %5B%5D;%0A%0A    Key.aggregate(%5B%7B $project: %7B tags : 1%7D%7D, %0A        %0A        %7B$group: %7B_id: null, tags:  %7B %22$addToSet%22 : %22$tags%22 %7D %7D %7D, %0A        %7B$unwind: %22$tags%22%7D, %0A        %7B$unwind: %22$tags%22%7D, %0A        %7B$project: %7B%22_id%22: 0, %22tags%22: %22$tags%22%7D%7D, %0A        %7B$group: %7B_id: null, allTags: %7B$addToSet: %22$tags%22%7D%7D%7D %0A%0A    %5D, function (err, result) %7B%0A        if (err) %7B%0A            next(err);%0A        %7D else %7B%0A            allTags = result%5B0%5D.allTags  %0A            // console.log(allTags);  %0A            // res.render('index', %7B%0A            //     allTags: allTags%0A            // %7D);%0A        %7D%0A    %7D);
 %0A    Key.find(%7B%7D

add tags method