Created
March 20, 2021 23:39
-
-
Save loloDawit/34e69bac689504603372b5a3bc0d5934 to your computer and use it in GitHub Desktop.
Handler
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
require('dotenv').config({ path: './.env' }); | |
const isEmpty = require('lodash.isempty'); | |
const validator = require('validator'); | |
const connectToDatabase = require('./db'); | |
const Note = require('./models/Note'); | |
/** | |
* Helper function | |
* @param {*} statusCode | |
* @param {*} message | |
* @returns | |
*/ | |
const createErrorResponse = (statusCode, message) => ({ | |
statusCode: statusCode || 501, | |
headers: { 'Content-Type': 'application/json' }, | |
body: JSON.stringify({ | |
error: message || 'Incorrect id', | |
}), | |
}); | |
/** | |
* Create - Creates a Note using an object | |
* @param {*} event data that's passed to the function upon execution | |
* - an object {title, description, and reminder} | |
* @param {*} context sets callbackWaitsForEmptyEventLoop false | |
* @param {*} callback sends a response success or failure | |
* @returns | |
*/ | |
module.exports.create = async (event, context, callback) => { | |
context.callbackWaitsForEmptyEventLoop = false; | |
if (isEmpty(event.body)) { | |
return callback(null, createErrorResponse(400, 'Missing details')); | |
} | |
const data = JSON.parse(event.body); | |
const note = new Note({ | |
title: data.title, | |
description: data.description, | |
reminder: data?.reminder, | |
}); | |
if (note.validateSync()) { | |
return callback(null, createErrorResponse(400, 'Incorrect note details')); | |
} | |
return connectToDatabase() | |
.then(() => Note.create(JSON.parse(event.body))) | |
.then((note) => | |
callback(null, { | |
statusCode: 200, | |
body: JSON.stringify(note), | |
}) | |
) | |
.catch((err) => | |
callback( | |
null, | |
createErrorResponse(err.statusCode || 500, 'Could not create the note.') | |
) | |
); | |
}; | |
/** | |
* Get one - Fetches a Note using the id | |
* @param {*} event data that's passed to the function upon execution | |
* - Note ID | |
* @param {*} context sets callbackWaitsForEmptyEventLoop false | |
* @param {*} callback sends a response success or failure | |
* @returns | |
*/ | |
module.exports.getOne = async (event, context, callback) => { | |
context.callbackWaitsForEmptyEventLoop = false; | |
if (!validator.isAlphanumeric(event.pathParameters.id)) { | |
callback(null, createErrorResponse(400, 'Incorrect Id.')); | |
return; | |
} | |
return connectToDatabase() | |
.then(() => Note.findById(event.pathParameters.id)) | |
.then((note) => | |
callback(null, { | |
statusCode: 200, | |
body: JSON.stringify(note), | |
}) | |
) | |
.catch((err) => { | |
if (err.name) { | |
const message = `Invalid ${err.path}: ${err.value}`; | |
callback(null, createErrorResponse(400, `Error:: ${message}`)); | |
} else { | |
callback( | |
null, | |
createErrorResponse(err.statusCode || 500, `Error:: ${err.name}`) | |
); | |
} | |
}); | |
}; | |
/** | |
* Get all - Gets all the Notes | |
* @param {*} context sets callbackWaitsForEmptyEventLoop false | |
* @param {*} callback sends a response success or failure | |
*/ | |
module.exports.getAll = (context, callback) => { | |
context.callbackWaitsForEmptyEventLoop = false; | |
connectToDatabase().then(() => { | |
Note.find() | |
.then((notes) => | |
callback(null, { | |
statusCode: 200, | |
body: JSON.stringify(notes), | |
}) | |
) | |
.catch((err) => { | |
if (err.name) { | |
const message = `Invalid ${err.path}: ${err.value}`; | |
callback(null, createErrorResponse(400, `Error:: ${message}`)); | |
} else { | |
callback( | |
null, | |
createErrorResponse(err.statusCode || 500, `Error:: ${err.name}`) | |
); | |
} | |
}); | |
}); | |
}; | |
/** | |
* Update - Updates a Note using the id | |
* @param {*} event data that's passed to the function upon execution | |
* - Note ID | |
* @param {*} context sets callbackWaitsForEmptyEventLoop false | |
* @param {*} callback sends a response success or failure | |
* @returns | |
*/ | |
module.exports.update = (event, context, callback) => { | |
context.callbackWaitsForEmptyEventLoop = false; | |
if (!validator.isAlphanumeric(event.pathParameters.id)) { | |
callback(null, createErrorResponse(400, 'Incorrect Id.')); | |
return; | |
} | |
if (isEmpty(event.body)) { | |
return callback(null, createErrorResponse(400, 'Missing details')); | |
} | |
const data = JSON.parse(event.body); | |
const note = new Note({ | |
title: data.title, | |
description: data.description, | |
reminder: data.reminder, | |
}); | |
console.log(note); | |
if (note.validateSync()) { | |
callback(null, createErrorResponse(400, 'Incorrect note details')); | |
return; | |
} | |
connectToDatabase().then(() => { | |
Note.findByIdAndUpdate(event.pathParameters.id, JSON.parse(event.body), { | |
new: true, | |
}) | |
.then((note) => | |
callback(null, { | |
statusCode: 200, | |
body: JSON.stringify(note), | |
}) | |
) | |
.catch((err) => { | |
if (err.name) { | |
const message = `Invalid ${err.path}: ${err.value}`; | |
callback(null, createErrorResponse(400, `Error:: ${message}`)); | |
} else { | |
callback( | |
null, | |
createErrorResponse(err.statusCode || 500, `Error:: ${err.name}`) | |
); | |
} | |
}); | |
}); | |
}; | |
/** | |
* Delete - Deletes a Note using the id | |
* @param {*} event data that's passed to the function upon execution | |
* - Note ID | |
* @param {*} context sets callbackWaitsForEmptyEventLoop false | |
* @param {*} callback sends a response success or failure | |
* @returns | |
*/ | |
module.exports.delete = (event, context, callback) => { | |
context.callbackWaitsForEmptyEventLoop = false; | |
if (!validator.isAlphanumeric(event.pathParameters.id)) { | |
callback(null, createErrorResponse(400, 'Incorrect Id.')); | |
return; | |
} | |
connectToDatabase().then(() => { | |
Note.findByIdAndRemove(event.pathParameters.id) | |
.then((note) => | |
callback(null, { | |
statusCode: 200, | |
body: JSON.stringify({ | |
message: `Removed note with id: ${note._id}`, | |
note, | |
}), | |
}) | |
) | |
.catch((err) => { | |
if (err.name) { | |
const message = `Invalid ${err.path}: ${err.value}`; | |
callback(null, createErrorResponse(400, `Error:: ${message}`)); | |
} else { | |
callback( | |
null, | |
createErrorResponse(err.statusCode || 500, `Error:: ${err.name}`) | |
); | |
} | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment