<commit_before>'use strict';

var AdminModel = require('../models/admin');

var User = require('../models/user');
var easySession = require('easy-session');

module.exports = function (router) {

    var model = new AdminModel();

    router.get('/:id*?', easySession.isLoggedIn(), easySession.checkRole('admin'), function (req, res) {
        
		if(!req.params.id){
			
			User.find({_id: req.session.userID}, function(err, admin){
				if(err){
					console.error(err); //Handle better later...
				}
			
				var model = {admins: admin}; 
			
				res.render('admin', model);
				return;
			
			});
			
		}else{
			
			var params = {
				adminID: req.session.userID,
				ownerId: req.params.id //Owner of the object?	
			};
			
			req.session.can('view:dashboard', params, function(err, has){
				if(err || !has){
					res.sendStatus(403);
					return;
				}
				
				User.find({_id: params.adminID}, function(err, admin){
					if(err){
						console.error(err); //Handle better later...
					}
			
			/*admin[0]._id*/
			
			
			
					var model = {admins: admin}; 
			
					res.render('admin', model);
					return;
			
				});
				
			});
			
		}

    });
	
	
	//Route handlers for posting/editing/delete news on the site.
	router.get('/news', function(req, res){
		res.render('news');
	}).post('post_news', function(req, res){
		res.render('news');
	}).put('edit_news', function(req, res){
		res.render('news');
	});
	
	//Route handlers for posting/editing/deleting events on the site.
	router.get('/events', function(req, res){
		res.render('events');
	}).post('post_event', function(req, res){
		res.render('event');
	}).put('edit_event', function(req, res){
		res.render('event');
	}).delete('delete_event', function(req, res){
		res.render('events');
	});
	
	//Route handlers for posting/editing/deleting donations on the site.
	router.get('/donations', function(req, res){
		res.render('donations');<commit_msg>Add events modelt to route handler.<commit_after>'use strict';

var easySession = require('easy-session'),
	fs = require('fs');

//Models
var User = require('../models/user');
var Events = require('../models/events');

var AdminModel = require('../models/admin');


module.exports = function (router) {

    var model = new AdminModel();

    router.get('/:id*?', easySession.isLoggedIn(), easySession.checkRole('admin'), function (req, res) {
        
		if(!req.params.id){
			
			User.find({_id: req.session.userID}, function(err, admin){
				if(err){
					console.error(err); //Handle better later...
				}
			
				var model = {admins: admin}; 
			
				res.render('admin', model);
				return;
			
			});
			
		}else{
			
			var params = {
				adminID: req.session.userID,
				ownerId: req.params.id //Owner of the object?	
			};
			
			req.session.can('view:dashboard', params, function(err, has){
				if(err || !has){
					res.sendStatus(403);
					return;
				}
				
				User.find({_id: params.adminID}, function(err, admin){
					if(err){
						console.error(err); //Handle better later...
					}
			
			/*admin[0]._id*/
			
			
			
					var model = {admins: admin}; 
			
					res.render('admin', model);
					return;
			
				});
				
			});
			
		}

    });
	
	
	//Route handlers for posting/editing/delete news on the site.
	router.get('/news', function(req, res){
		res.render('news');
	}).post('post_news', function(req, res){
		res.render('news');
	}).put('edit_news', function(req, res){
		res.render('news');
	});
	
	//Route handlers for posting/editing/deleting events on the site.
	router.get('/events', easySession.isLoggedIn(), easySession.checkRole('admin'), function(req, res){
		
		Events.find({}, null, {sort : {date : 1}}, function(err, events){
			if(err){
				console.error(err);
			}
			
			var model = {events: events};
			res.render('events', model);
			
		});
		
	}).post('post_event', function(req, res){
		
		var newEvent = new Events();
		
		newEvent.eventTitle.majorTitle = req.body.majorTitle;
		newEvent.eventTitle.subTitle = req.body.subTitle;
		

//		newEvent.imgLink = "data" +  req.files.eventImg.type + ";base64" + fs.readFileSync(req.files.productImg.path).toString('base64');
//		newEvent.eventDescription = new Buffer(req.body.eventDescription, 'utf8').toString();
		
		newEvent.phoneNumber1 = req.body.phoneNum1;
		newEvent.phoneNumber2 = req.body.phoneNum2;
		newEvent.eMail1 = req.body.email1;
		newEvent.eMail2 = req.body.email2;
		newEvent.date = req.body.eventDate;
		
		newEvent.location.description = req.body.eventLocationDescription;
		newEvent.location.address = req.body.eventAddress;
		newEvent.location.city = req.body.eventCity;
		//newEvent.
		
		newEvent.creator = req.session.userID;
		
		
		newEvent.save(function(err){
			if(err){
				console.error(err);
			}
			
			res.redirect('/admin/events');
			
		});
		
		
	}).put('edit_event', function(req, res){
		res.render('event');
	}).delete('delete_event', function(req, res){
		res.render('events');
	});
	
	//Route handlers for posting/editing/deleting donations on the site.
	router.get('/donations', function(req, res){
		res.render('donations');
{'fraction_non_alphanumeric': 0.17916912778105926, 'fraction_numerical': 0.0043315613309706635, 'mean_word_length': 3.0381558028616853, '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': '5256834', 'n_tokens_mistral': 2074, 'n_tokens_neox': 1665, 'n_words': 331}