Last active
February 21, 2018 08:50
-
-
Save chathurawidanage/4e1b46b7cd73fcac7bbde98d51bab9ef to your computer and use it in GitHub Desktop.
This file contains hidden or 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
let AWS = require('aws-sdk'); | |
const ddb = new AWS.DynamoDB.DocumentClient(); | |
const validate = require("validate.js"); | |
exports.handler = function (event, context, callback) { | |
//validating email and name | |
var constraints = { | |
email: { | |
presence: true, | |
email: true | |
}, | |
name: { | |
presence: true, | |
length: { | |
minimum: 1 | |
}, | |
} | |
}; | |
let invalid = validate(event, constraints); | |
if (!invalid) { | |
let today = new Date().getTime(); | |
ddb.put({ | |
TableName: 'contact_us', | |
Item: { | |
'name': event.name, | |
'email': event.email, | |
'phone': event.phone, | |
'company': event.company, | |
'comment': event.comment, | |
'date': today | |
} | |
}, function (err, data) { | |
if (err) { | |
callback(err, null); | |
} else { | |
callback(null, "Successfully Saved Entry!"); | |
} | |
}); | |
} else { | |
callback(JSON.stringify(invalid), null); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment