Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node assignemnts #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.project
.settings/
logs
node_modules
64 changes: 64 additions & 0 deletions dbConnection.js
Original file line number Diff line number Diff line change
@@ -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);
});
}
};





15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
56 changes: 56 additions & 0 deletions route/app.js
Original file line number Diff line number Diff line change
@@ -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;











150 changes: 150 additions & 0 deletions route/asyncOperations.js
Original file line number Diff line number Diff line change
@@ -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;
38 changes: 38 additions & 0 deletions route/auth.js
Original file line number Diff line number Diff line change
@@ -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("<<data");
console.log(data);
dbOpr.fetchDataDb(data,function(Error,Success){
console.log("Callback yaar");
console.log(Error)
if(!Error){
console.log("Yes auth done");
authenticateDataFromDbCB(null,Success);
}else{
console.log("auth not done");
authenticateDataFromDbCB(Error,null);
}

});


};

exports.auth = auth;
4 changes: 4 additions & 0 deletions route/callToFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var destiFile = require('./destination').destination;
var destiObj = new destiFile();

destiObj.writeTxtFile();
Loading