diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..58692c1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.project +.settings/ +logs +node_modules \ No newline at end of file diff --git a/dbConnection.js b/dbConnection.js new file mode 100644 index 0000000..730cdbf --- /dev/null +++ b/dbConnection.js @@ -0,0 +1,64 @@ +/*var express = require('express'); +var app = express(); +var bodyParser = require('body-parser');*/ + +var MongoClient = require('mongodb').MongoClient; + + + + +var state = exports.state = { + dbSub :{ + + } +}; + +exports.connect = function(url,done){ + + if(state.db) return done(); + //create connection with mongoDb + MongoClient.connect(url,function(err,db){ + if(!err){ + console.log("Connected to server sucessfully"); + // app.listen(3000); + state.dbSub = db; + done(db); + // db.close(); + } + }); +}; + + +exports.get = function(done){ + //return state.db; + done(state.dbSub); +}; + +exports.close = function(mongoObj,done){ + if(state.dbSub){ + state.dbSub.close(function(err,result){ + state.dbSub = null; + state.mode = null; + done(err,result); + + }); + }else{ + mongoObj.close(function(err,result){ + if(!err){ + console.log("Sucessful"); + console.log(result); + }else{ + console.log("Error while closing connection"); + console.log(err); + } + + + done(err,result); + }); + } +}; + + + + + diff --git a/package.json b/package.json new file mode 100644 index 0000000..229e67e --- /dev/null +++ b/package.json @@ -0,0 +1,15 @@ +{ + "name": "y", + "version": "1.0.0", + "description": "", + "main": "destination.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "fs": "0.0.2", + "mongodb": "^2.1.19" + } +} diff --git a/route/app.js b/route/app.js new file mode 100644 index 0000000..b1afcbd --- /dev/null +++ b/route/app.js @@ -0,0 +1,56 @@ +var asyncFileObj = require('./asyncOperations').asyncOpertion; +var _asyncFile = new asyncFileObj(); + +function app(){ + +} + +app.prototype ={ + constructor : app +}; + +/** + * @author apandit + * @purpose : To call doSeries fun of asyncOpeartions : For async series assignment + */ +app.prototype.doSeriesOpr= function(){ + //console.log("In doSeriesOpr"); + _asyncFile.doSeries(function(){ + console.log("Succesfully write file"); + }); +}; + +/** + * @author apandit + * @purpose : To call doSeries fun of doParallelOpr : For async parallel processing assignment + */ + +app.prototype.doParallelOpr= function(){ + + _asyncFile.doParallel(function(){ + console.log("Succesfully write file"); + }); +}; + + +var _appObj = new app(); + +//call to series operation +_appObj.doSeriesOpr(); + +//call to parallel operation +//_appObj.doParallelOpr(); + + +exports.app = app; + + + + + + + + + + + diff --git a/route/asyncOperations.js b/route/asyncOperations.js new file mode 100644 index 0000000..d3fbe02 --- /dev/null +++ b/route/asyncOperations.js @@ -0,0 +1,150 @@ +var async = require('async'); +var fs = require('fs'); +var js2xmlparser = require('js2xmlparser'); +var logger = require('./logger').Logger(); +var destiFile = require('./destination').destination; +var destiObj = new destiFile(); + + +function asyncOpertion(){ + +} + +asyncOpertion.prototype ={ + constructor : asyncOpertion +}; + +/** + * @author apandit + * @purpose : write text file + * @param writeTxtFileCB + */ + +asyncOpertion.prototype.writeTxtFile = function(writeTxtFileCB){ + try{ + destiObj.sortRecords(function(SortedJsonObj){ + fs.stat('../destination.txt', function(err, stat) { + if(!err){ + logger.error("destination.txt file already exists"); + }else{ + //create text file + var data = null; + var _exeRec = function(iReadObj,iIndex){ + + console.log("In _exeRec of destination.js"); + if(iIndex == 0){ + data = 'Id|First Name|Last Name|Score' +"\n"+ iReadObj[iIndex].id + '|' +iReadObj[iIndex].fname + '|' +iReadObj[iIndex].IName + '|' + iReadObj[iIndex].score; + }else{ + data = "\n" + iReadObj[iIndex].id + '|' +iReadObj[iIndex].fname + '|' +iReadObj[iIndex].IName + '|' + iReadObj[iIndex].score; + } + fs.appendFile("../destination.txt", data ,function(err){ + if(!err){ + iIndex++; + if(iIndex < (iReadObj.length)){ + _exeRec(iReadObj,iIndex); + }else{ + //console.log("Doneeeeeeeeeeeeeeee"); + return writeTxtFileCB(); + } + + + }else{ + logger.error("Unable to write in destination.txt"); + } + + }); + + + }; + if(SortedJsonObj.length > 0){ + _exeRec(SortedJsonObj,0); + } + + + } + + }); + }); + }catch(e){ + logger.error("Exception catched in writeTxtFile"+e); + } + +}; +/** + * @author apandit + * @purpose : Write xml file + * @param writeXmlFile + */ +asyncOpertion.prototype.writeXmlFile= function(writeXmlFile){ + //console.log("In asyncOpertion writeXmlFile"); + destiObj.sortRecords(function(SortedJsonObj){ + var _parserObj = (js2xmlparser("student",SortedJsonObj[0] )) + (js2xmlparser("student",SortedJsonObj[1] )) + (js2xmlparser("student",SortedJsonObj[2] )); + destiObj.writeXmlFile(_parserObj,function(){ + return writeXmlFile(); + }); + }); +}; + +/** + * @author apandit + * @purpose : Async series operation call + * @param doSeriesCB + */ +asyncOpertion.prototype.doSeries = function(doSeriesCB){ + var _that =this; + //Async Series operation call + async.series([ + //Call writeTxt file fun + function (next) { + console.log("In 1st fun"); + _that.writeTxtFile(function(){ + next(); + + }); + }, + //Call writeXml file fun + function (next) { + console.log("In 2nd fun"); + _that.writeXmlFile(function(){ + next(); + }); + } + ],function(error,results){ + return doSeriesCB(); + }); + +}; + +/** + * @author apandit + * @purpose : Async parallel operation call + * @param doSeriesCB + */ + +asyncOpertion.prototype.doParallel = function(doParallelCB){ + var _that = this; + async.parallel([ + //Call writeTxt file fun + function (next) { + console.log("In 1st fun"); + _that.writeTxtFile(function(){ + next(); + + }); + }, + //Call writeXml file fun + function (next) { + console.log("In 2nd fun"); + _that.writeXmlFile(function(){ + next(); + }); + } + + ],function(error,results){ + return doParallelCB(); + }); + +}; + + +exports.asyncOpertion = asyncOpertion; diff --git a/route/auth.js b/route/auth.js new file mode 100644 index 0000000..4059b95 --- /dev/null +++ b/route/auth.js @@ -0,0 +1,38 @@ +var dbOperationObj = require('./dbOpeartion').dbOperation; +var dbOpr = new dbOperationObj(); + + + +function auth(){ + +} + +auth.prototype={ + construtor : auth +}; + + +auth.prototype.authenticateDataFromDb = function(param,authenticateDataFromDbCB){ + var data = { + name : param.UserName, + pwd : param.Password + }; + console.log("< 0){ + db.close(iDbConn,function(){ + if(fetchDataDbCB){ + return fetchDataDbCB(null,result); + } + + }); + }else{ + var _errorMsg = 'Data is not authenticated'; + db.close(iDbConn,function(){ + if(fetchDataDbCB){ + return fetchDataDbCB(_errorMsg,null); + } + }); + } + + }); + + } + + }); +}; + +/** + * @author apandit + * @Purpose : To update records from collections + * @param updateDbCB + */ + +dbOperation.prototype.updateDb = function(updateDbCB){ + + console.log("In updateDb of dbOperation"); + db.connect('mongodb://localhost:27017/studentDb',function(iDbConn){ + if(iDbConn){ + var collection = iDbConn.collection('studentData'); + collection.updateOne({id : 3 , name : 'Samhita' , pwd : 'samhita'} + , { $set: {id : 3 , name : 'Amruta' , pwd : 'amruta'} }, function(err1, result1) { + if(!err1){ + db.close(iDbConn,function(){ + updateDbCB(); + }); + }else{ + db.close(iDbConn,function(){ + updateDbCB(); + }); + } + + }); + } + + }); +}; + +/** + * @author apandit + * @purpose : To delete record from collection + * @param deleteDbCB + */ + +dbOperation.prototype.deleteDb = function(deleteDbCB){ + db.connect('mongodb://localhost:27017/studentDb',function(iDbConn){ + + if(iDbConn){ + var collection = iDbConn.collection('studentData'); + collection.deleteOne({ id : 3 ,name : 'Amruta'}, function(err2, result2) { + if(!err2){ + db.close(iDbConn,function(){ + deleteDbCB(); + }); + } + + }); + } + }); +}; + + + +//Call to CRUD operation +/* +var _dbOprnObj = new dbOperation(); +_dbOprnObj.deleteDb(function(){ + +});*/ + + +exports.dbOperation= dbOperation; diff --git a/route/destination.js b/route/destination.js new file mode 100644 index 0000000..62b7b4e --- /dev/null +++ b/route/destination.js @@ -0,0 +1,179 @@ +var fs = require('fs'); +var js2xmlparser = require('js2xmlparser'); +var logger = require('./logger').Logger(); +var db = require('../dbConnection'); + +function destination(){ + +} + +destination.prototype ={ + constructor : destination +}; + + +/** + * @purpose : ReadFile source.json file + * @param readJsonFileCB + */ +destination.prototype.readJsonFile = function(readJsonFileCB){ + logger.debug("In readJsonFile of Destination.js"); + var _jsonObj =null,_jsonObjFlag =null; + fs.stat('source.json', function(err, stat) { + if(!err){ + logger.debug("Source.json file found!!"); + //Read source.json file + fs.readFile('source.json', 'utf8', function (error, data) { + if(!error){ + try{ + _jsonObj = JSON.parse(data); + + }catch(e){ + logger.error("Source.json file does not contain vallid json object"); + } + // var _jsonObjFlag = null; + + //To check each object has vallid score property + for(var a = 0 ; a < _jsonObj.students.length ; a++){ + if(_jsonObj.students[a].hasOwnProperty('score') == true){ + _jsonObjFlag = true; + }else{ + _jsonObjFlag = false; + logger.error("Json object don't have score property"); + } + } + + if(_jsonObjFlag == true && readJsonFileCB){ + console.log("In this",_jsonObj); + return readJsonFileCB(_jsonObj); + } + + + }else{ + logger.error("Error while reading source.json file."); + } + + }); + }else{ + logger.error("Source.json file not found!!"); + } + }); + +}; +/** + * @purpose : Sort object records using scores in text file + * @param readJsonFileCB + */ +destination.prototype.sortRecords = function(sortRecordsCB){ + var _that = this; + _that.readJsonFile(function(JsonObj){ + var _jsonObjArr = JsonObj.students; + //Sort data according to score + _jsonObjArr.sort(function(a,b){ + return b.score - a.score; + }); + + if(sortRecordsCB){ + return sortRecordsCB(_jsonObjArr); + } + }); + +}; +/** + * @purpose :Write records in destination file + * @param writeFileCB + */ + +destination.prototype.writeTxtFile = function(writeFileCB){ + var _that = this; + + _that.sortRecords(function(SortedJsonObj){ + //Check destination.txt file already exists + fs.stat('../destination.txt', function(err, stat) { + if(!err){ + logger.error("destination.txt file already exists"); + }else{ + //Insert data in mongodb + db.connect('mongodb://localhost:27017/mydatabase',function(dbObj){ + var collection = dbObj.collection('records'); + collection.insertMany(SortedJsonObj, function(err, result) { + + }); + }); + + + //create text file + var data = null; + var _exeRec = function(iReadObj,iIndex){ + + console.log("In _exeRec of destination.js"); + var _parserObj = (js2xmlparser("student",iReadObj[iIndex] )); + if(iIndex == 0){ + data = 'Id|First Name|Last Name|Score' +"\n"+ iReadObj[iIndex].id + '|' +iReadObj[iIndex].fname + '|' +iReadObj[iIndex].IName + '|' + iReadObj[iIndex].score; + }else{ + data = "\n" + iReadObj[iIndex].id + '|' +iReadObj[iIndex].fname + '|' +iReadObj[iIndex].IName + '|' + iReadObj[iIndex].score; + } + fs.appendFile("../destination.txt", data ,function(err){ + if(!err){ + _that.writeXmlFile(_parserObj,function(){ + iIndex++; + if(iIndex < (iReadObj.length)){ + _exeRec(iReadObj,iIndex); + }else{ + console.log("Doneeeeeeeeeeeeeeee!!!"); + } + }); + + }else{ + logger.error("Unable to write in destination.txt"); + } + + }); + + + }; + if(SortedJsonObj.length > 0){ + _exeRec(SortedJsonObj,0); + } + + + } + + }); + + }); + + +}; +/** + * @purpose : Sort object records using scores in xml file + * @param readJsonFileCB + */ + +destination.prototype.writeXmlFile = function(parserObj,writeXmlFileCB){ + //var _parserObj = (js2xmlparser("student",JsonObj)); + fs.appendFile('../destination.xml',parserObj,function(error){ + if(!error){ + if(writeXmlFileCB){ + return writeXmlFileCB(); + } + + }else{ + if(writeXmlFileCB){ + return writeXmlFileCB(); + } + } + + }); +}; + + + + +/* +var _newDestiObj = new destination(); +_newDestiObj.writeTxtFile();*/ + + +exports.destination = destination; + diff --git a/route/first.js b/route/first.js new file mode 100644 index 0000000..0177e34 --- /dev/null +++ b/route/first.js @@ -0,0 +1,10 @@ + + +function first(){ + +} + + + + +module.exports = first; \ No newline at end of file diff --git a/route/logger.js b/route/logger.js new file mode 100644 index 0000000..86ae7e9 --- /dev/null +++ b/route/logger.js @@ -0,0 +1,54 @@ +var winston = require('winston'); + +function Logger(){ + + var logger = new (winston.Logger)({ + exitOnError : false, + transports : [ + new (winston.transports.File)({ + filename : './logs/error.log' , + name : 'file.error', + level : 'error', + maxsize : 1024*1024*10, + json : false, + handleExceptions : true + + }), + new (winston.transports.File)({ + filename : '../logs/info.log' , + name : 'file.info', + level : 'info', + maxsize : 1024*1024*10, + json : false, + handleExceptions : true + }), + new (winston.transports.File)({ + filename : '../logs/debug.log' , + name : 'file.debug', + level : 'debug', + maxsize : 1024*1024*10, + json : false, + handleExceptions : true + + }), + new winston.transports.Console({ + handleExceptions: true, + + }), + new (winston.transports.File)({ + filename : './logs/warn.log' , + name : 'file.warn', + level : 'warn', + maxsize : 1024*1024*10, + json : false, + handleExceptions : true + + }), + + ]}); + + return logger; +} + +exports.Logger = Logger; + diff --git a/route/source.json b/route/source.json new file mode 100644 index 0000000..f47b705 --- /dev/null +++ b/route/source.json @@ -0,0 +1,22 @@ +{ + "students" : [{ + "id" : 123, + "fname" : "John", + "IName" : "Doe", + "score" : 234 + },{ + "id" : 124, + "fname" : "Jane", + "IName" : "Doe", + "score" : 543 + },{ + "id" : 125, + "fname" : "Jackie", + "IName" : "Doe", + "score" : 453 + } + + + + ] +} \ No newline at end of file diff --git a/serverFile.js b/serverFile.js new file mode 100644 index 0000000..f1b2d63 --- /dev/null +++ b/serverFile.js @@ -0,0 +1,93 @@ +var express = require('express'); +var app = express(); +var bodyParser = require('body-parser'); +var authObj = require('./route/auth').auth; +var authFile = new authObj(); +var redis = require("redis"); +var session = require('express-session'); +var redisStore = require('connect-redis')(session); +var client = redis.createClient(); + +//For serving static files +app.use(express.static('views')); + +app.set('views', __dirname + '/views'); +app.engine('html', require('ejs').renderFile); + + +app.listen(3000,function () { + console.log('Example app listening on port 3000!'); +}); + + +app.use(session({ + secret: 'ssshhhhh', + // create new redis store. + store: new redisStore({ host: 'localhost', port: 6379, client: client}), + saveUninitialized: false, + resave: false, + +})); + + +app.use(bodyParser.json()); +app.use(bodyParser.urlencoded({ extended: true })); + +/** + * @author apandit + * @purpsoe : To authenticate user and set session + */ +app.post('/loginForm', function(req, res) { + + if (req.session.UserName) { + if(req.session.UserName != 'admin'){ + res.redirect('/user.html'); + }else{ + res.redirect('/admin.html'); + } + }else{ + var _vallidateObj = { + UserName : req.body.UserName, + Password : req.body.Password + }; + + authFile.authenticateDataFromDb(_vallidateObj,function(Error,AuthSuccess){ + if(!Error){ + + req.session.UserName = req.body.UserName; + if(AuthSuccess[0].user){ + res.redirect('/admin.html'); + }else{ + res.redirect('/user.html'); + } + + }else{ + res.redirect(401, '/login.html'); + } + + }); + } + + +}); + +/** + * @author apandit + * @purpose : To destroy express session + */ + + +app.get('/logout',function(req,res){ + req.session.destroy(function(err){ + if(err){ + res.send(301); + }else{ + // res.clearCookie('connect.sid'); + res.send(301); + } + }); + +}); + + + diff --git a/views/admin.html b/views/admin.html new file mode 100644 index 0000000..d8b7338 --- /dev/null +++ b/views/admin.html @@ -0,0 +1,68 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
idnamepwd
1Anujaanuja
2Ashash
3Samhitasamhita
+ + + + + + + + + + + + + + + diff --git a/views/index.css b/views/index.css new file mode 100644 index 0000000..b5fe857 --- /dev/null +++ b/views/index.css @@ -0,0 +1,7 @@ +body{ + background-color : lightgrey; +} + +h1{ + color : blue; +} \ No newline at end of file diff --git a/views/index.html b/views/index.html new file mode 100644 index 0000000..2f7e7c0 --- /dev/null +++ b/views/index.html @@ -0,0 +1,10 @@ + + + + + +

Index File not login file

+ + + + \ No newline at end of file diff --git a/views/index.js b/views/index.js new file mode 100644 index 0000000..6ca1102 --- /dev/null +++ b/views/index.js @@ -0,0 +1,3 @@ +function alertFun(){ + alert("Now Its In Javascript File"); +} \ No newline at end of file diff --git a/views/login.html b/views/login.html new file mode 100644 index 0000000..395f3c0 --- /dev/null +++ b/views/login.html @@ -0,0 +1,14 @@ + + +Login page + + +
+ + + + + +
+ + \ No newline at end of file diff --git a/views/user.html b/views/user.html new file mode 100644 index 0000000..c7bd57e --- /dev/null +++ b/views/user.html @@ -0,0 +1,59 @@ + + + + + + + + + + + + + + + + + + + + + +
idnamepwd
1Anujaanuja
+ + + + + + + + + + +