<repo_name>khizaear/payapp<file_sep>/README.md
SandGlass
=========
<file_sep>/routes/index.js
var express = require('express');
var router = express.Router();
var httpreq = require('httpreq');
var services=require('./services');
var MongoClient = require('mongodb').MongoClient;
var process=require('process')
/* GET home page. */
router.get('/', function(req, res, next) {
if (process.pid) {
  console.log('This process is your pid ' + process.pid);
  console.log('This platform is ' + process.platform);

         // [ 27, 30, 46, 1000 ]
}
	var output=services.getcomponents();	
	res.render('default', { data:output,  user:services.getuser()});
	//console.log(output);
});

 router.get('/profile/search/:id',function(req, res, next) {
	MongoClient.connect('mongodb://127.0.0.1:27017/sandglass', function(err, db) {	
		db.open(function(err,db){
			var profiles=req.params.id
			db.collection("logs").find({'name':profiles},{'name':true}).toArray(function(err, results) {
				res.send({"datas":results});			
			});
		});
	});
});
 


module.exports = router;
<file_sep>/routes/admin.js
var express = require('express');
var router = express.Router();
var httpreq = require('httpreq');
var services=require('./services');
var MongoClient = require('mongodb').MongoClient;
var ObjectID = require('mongodb').ObjectID;
var util = require('util');


router.get('/', function(req, res, next) {
	MongoClient.connect("mongodb://localhost:27017/sandglass", function(err, db) {
		db.open(function(err,db){
			db.collection('logs', function(err, collection) {
				collection.find().toArray(function(err, items) {
					res.render('admin', {data:services.getcomponents(), profiles:items,  user:services.getuser()});
				});
			});
		});
	});
	
});

router.post('/createprofile', function(req, res, next) {
		MongoClient.connect("mongodb://localhost:27017/sandglass", function(err, db) {
		if (err) throw err;
		var profile=JSON.parse(req.body.datas);
		var profilename=profile.profile.name;
		var profiletype="profiles";
		var params=profile.profile;
		var desc=profile.desc;
		var documents=profile.documents;
		var created=services.getdate();
		var createdby=services.getuser();
		var document ={"name":profilename,"params":params,"type":"profiles","documents":document,"created_at":created,"created_by":createdby}		
		db.collection('logs').insert(document, function(err, records) {
			console.log(document);
		});
	}); 	
	res.end(JSON.stringify(req.body.datas));
});

router.get('/stopserve', function(req, res, next) {
	//var stopser=services.stopServ("feelserv","STAGE2C8998");
		httpreq.get("http://psm.corp.ebay.com/stopserv/?servname=feelserv&stage=STAGE2C8998", function (err, data){
			//output=JSON.parse(data.body);
			console.log(err);
			console.log(data);
			res.end(data);
		});

});

router.get('/viewprofile/:id', function(req, res, next) {
	MongoClient.connect('mongodb://127.0.0.1:27017/sandglass', function(err, db) {	
		db.open(function(err,db){
			db.collection('logs', function(err, collection) {
				var o_id = new ObjectID(req.params.id);
				collection.find({'_id':o_id}).toArray(function(err, items) {
					res.render('blank', {data:items});
					console.log(items);
				});
			});
		});
	});
});

router.get('/editprofile/:id', function(req, res, next) {
	MongoClient.connect('mongodb://127.0.0.1:27017/sandglass', function(err, db) {	
		db.open(function(err,db){
			db.collection('logs', function(err, collection) {
				var o_id = new ObjectID(req.params.id);
				collection.find({'_id':o_id}).toArray(function(err, items) {

					res.render('editprofileid', {data:items,services:services.getcomponents()});
					console.log(items);
				});
			});
		});
	});
});


router.post('/createprofilewithid', function(req, res, next) {
	MongoClient.connect('mongodb://127.0.0.1:27017/sandglass', function(err, db) {
		var profile=JSON.parse(req.body.datas);
		var profilename=profile.profile.name;
		var profiletype="profiles";
		var params=profile.profile;
		var desc=profile.desc;
		var documents=profile.documents;
		var created=services.getdate();
		var createdby=services.getuser();
		var document ={"name":profilename,"params":params,"type":"profiles","documents":document,"created_at":created,"created_by":createdby}
		db.open(function(err,db){
			db.collection('logs', function(err, collection) {
				//var o_id = new ObjectID(req.params.id);
				collection.find({"name":profilename},{"_id":1},function(err, items) {
					console.log(items);
					//res.send(items);
				});
				//end collection.find


				collection.remove({"name":profilename});
				db.collection('logs').insert({"name":profilename,"params":params,"type":"profiles","documents":document,"created_at":created,"created_by":createdby}, function(err, records) {
					console.log(document);
					res.end();
				});
				});
			});
		});
	});


router.get('/deleteprofile/:id', function(req, res, next) {
	MongoClient.connect('mongodb://127.0.0.1:27017/sandglass', function(err, db) {
		db.open(function(err,db){
			db.collection('logs', function(err, collection) {
				var o_id = new ObjectID(req.params.id);
				collection.remove({'_id':o_id});

				res.end();
				});
			});
	});
});


module.exports = router;
