-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ab6d66e
commit 104c35d
Showing
6 changed files
with
170 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"foo":"koo"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"firstName":"ritesh","lastName":"singh","phone":"8276079012","tosAgreement":true} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Request handlers | ||
* | ||
*/ | ||
|
||
//Dependecies | ||
var _data = require('./data'); | ||
var helpers = require('./helpers'); | ||
//Define the handler | ||
var handlers = {}; | ||
|
||
//Users | ||
handlers.users = function(data,callback){ | ||
var acceptableMethods = ['post','get','put','delete']; | ||
if(acceptableMethods.indexOf(data.method) > -1){ | ||
handlers._users[data.method](data,callback); | ||
} else { | ||
// returning 405 that method is not available | ||
callback(405); | ||
} | ||
}; | ||
|
||
//Container for user submethods | ||
handlers._users = {}; | ||
|
||
// Users - POST | ||
// Required data firstName,lastName,Phn Number,password,tosAgreement | ||
handlers._users.post = function(data,callback){ | ||
//Check all the required field are filled out | ||
var firstName = typeof(data.payload.firstName) == 'string' && data.payload.firstName.trim().length > 0 ? data.payload.firstName.trim() : false; | ||
var lastName = typeof(data.payload.lastName) == 'string' && data.payload.lastName.trim().length > 0 ? data.payload.lastName.trim() : false; | ||
var phone = typeof(data.payload.phone == 'string') && data.payload.phone.length == 10 ? data.payload.phone : false; | ||
var password = typeof(data.payload.password == 'string') && data.payload.password.length > 0 ? data.payload.password : false; | ||
var tosAgreement = typeof(data.payload.tosAgreement == 'boolean') && data.payload.tosAgreement == true ? true : false; | ||
|
||
if(firstName&& lastName && phone && password && tosAgreement){ | ||
//Make sure that user doesnot exists | ||
_data.read('users',phone,function(err,data){ | ||
if(err) { | ||
// Hash the password | ||
var hashedPassword = helpers.hash(password); | ||
|
||
//sanity check for hashedPassword | ||
if(hashedPassword){ | ||
//Create User Object | ||
var userObject = { | ||
'firstName' : firstName, | ||
'lastName' : lastName, | ||
'phone' : phone, | ||
'tosAgreement' : true, | ||
} | ||
|
||
_data.create('users',phone,userObject,function(err){ | ||
if(!err) { | ||
callback(200); | ||
} else { | ||
|
||
callback(500,{'Error' : 'Couldnot create the new user'}); | ||
} | ||
}); | ||
} else { | ||
|
||
callback(500,{'Error': 'Could not create the hashedPassword'}); | ||
} | ||
|
||
|
||
} else { | ||
callback(400,{'Error' : 'A user with that phone number already exists'}); | ||
} | ||
}); | ||
|
||
} else { | ||
callback(400,{'Error' : 'Missing required fields'}); | ||
} | ||
}; | ||
|
||
//Users - GET | ||
handlers._users.get = function(data,callback){ | ||
|
||
}; | ||
|
||
//Users - PUT | ||
handlers._users.put = function(data,callback){ | ||
|
||
}; | ||
|
||
//User -DELETE | ||
handlers._users.delete = function(data,callback){ | ||
|
||
}; | ||
|
||
//sample handler | ||
handlers.sample = function(data,callback){ | ||
callback(200,{'name':'sample handler'}); | ||
}; | ||
|
||
//Ping handler | ||
handlers.ping=function(data,callback) | ||
{ | ||
callback(200); | ||
}; | ||
|
||
handlers.NotFound = function(data,callback) | ||
{ | ||
callback(404); | ||
}; | ||
|
||
//Export the module | ||
module.exports = handlers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Helpers function for our library | ||
* | ||
*/ | ||
|
||
//Dependencies | ||
var crypto = require('crypto'); | ||
var config = require('./config'); | ||
|
||
// Container for the all the helpers | ||
var helpers = {}; | ||
|
||
// create a SHA256 hash | ||
helpers.hash = function(str) { | ||
if(typeof(str)=='string' && str.length > 0){ | ||
var hash = crypto.createHmac('sha256',config.hashingSecret).update(str).digest('hex'); | ||
return hash; | ||
} else { | ||
return false; | ||
} | ||
}; | ||
|
||
//Parse a JSON string to object in all cases | ||
helpers.parseJsonToObject = function(str){ | ||
console.log("coming here"); | ||
try{ | ||
console.log("coming here"+str); | ||
var obj = JSON.parse(str); | ||
return obj; | ||
} catch(e){ | ||
console.log("coming here.." +e); | ||
return {}; | ||
} | ||
}; | ||
|
||
|
||
// Exports the module | ||
module.exports = helpers; |