Skip to content

Instantly share code, notes, and snippets.

@markheath
Last active February 12, 2017 22:09
Show Gist options
  • Save markheath/eb81096202b3a2f1acc8d26e821d5755 to your computer and use it in GitHub Desktop.
Save markheath/eb81096202b3a2f1acc8d26e821d5755 to your computer and use it in GitHub Desktop.
Azure Functions simple in-memory CRUD web API
{
"bindings": [
{
"type": "httpTrigger",
"direction": "in",
"webHookTypeX": "genericJson",
"name": "req",
"methods": [
"get",
"post",
"put",
"patch",
"delete"
],
"authLevel": "anonymous"
},
{
"type": "http",
"direction": "out",
"name": "res"
}
],
"disabled": false
}
var tasks = [
{ text: "watch football", isDone: true },
{ text: "play football", isDone: false },
{ text: "eat crisps", isDone: false },
];
var inMemoryCrud = require('./inMemoryCrud');
inMemoryCrud.seedData(tasks);
module.exports = function (context, req) {
var requiredFields = ["text", "isDone"];
inMemoryCrud.handleRequest(context, req, requiredFields);
context.done();
}
var data = [];
var getAll = function() {
return data;
}
var getById = function(id) {
return data.find(p => p.id === id );
}
var seedData = function(newItems) {
newItems.forEach(add);
}
var add = function(newItem) {
newItem.id = data.reduce((a,b) => a.id > b.id ? a.id : b.id, 0) + 1;
data.push(newItem);
return newItem;
}
var put = function(id, updatedItem) {
var index = data.findIndex(d => d.id === id);
if (index >= 0) {
updatedItem.id = id;
data[index] = updatedItem;
return updatedItem;
}
return null;
}
var deleteById = function(id) {
var index = data.findIndex(d => d.id === id);
if (index >= 0) {
data.splice(index, 1);
return true;
}
return false;
}
var patch = function(id, updatedItem) {
var item = getById(id);
if (item) {
updatedItem.id = id;
Object.assign(item, updatedItem);
}
return item;
}
var successResponse = function(code, body) {
return { status: code, body: body };
}
var errorResponse = function(code, message) {
return { status: code, body: { error: message } };
}
var handleUpdate = function(req, updateFunc, requiredFields) {
if (req.query.id) {
var missingFields = findMissingFields(req.body, requiredFields);
if (missingFields === "") {
var id = parseInt(req.query.id);
var updated = updateFunc(id, req.body);
if (updated) {
return successResponse(200, updated);
}
return errorResponse(404, 'Item ' + req.query.id + ' not found');
}
return errorResponse(400, "Missing required fields: " + missingFields);
}
return errorResponse(400, "Please supply item id");
}
var handleDelete = function(req) {
if (req.query.id) {
var id = parseInt(req.query.id);
if (deleteById(id)) {
return successResponse(200, "deleted");
}
return errorResponse(404, 'Item ' + req.query.id + ' not found');
}
return errorResponse(400, "Please supply item id");
}
var handleGet = function(req) {
if (req.query.id) {
var item = getById(parseInt(req.query.id));
if (item) {
return successResponse(200, item);
}
return errorResponse(404, 'Item ' + req.query.id + ' not found');
}
return successResponse(200, data);
}
var findMissingFields =function(body, requiredFields) {
return requiredFields.filter(f => !(f in body)).join(',');
}
var handlePost = function(req, requiredFields) {
if(typeof req.body === 'object') {
var missingFields = findMissingFields(req.body, requiredFields);
if (missingFields === "") {
var newItem = add(req.body);
return successResponse(201, newItem);
}
return errorResponse(400, "Missing required fields: " + missingFields);
}
return errorResponse(400, "must be an object");
}
var handleRequest = function(context, req, requiredFields) {
if (req.method === 'GET') {
context.res = handleGet(req);
}
else if (req.method === 'POST') {
context.res = handlePost(req, requiredFields || []);
}
else if (req.method === 'PUT') {
context.res = handleUpdate(req, put, requiredFields || []);
}
else if (req.method === 'PATCH') {
context.res = handleUpdate(req, patch, []);
}
else if (req.method === 'DELETE') {
context.res = handleDelete(req);
}
}
module.exports =
{"handleRequest":handleRequest,
"seedData": seedData,
"ver": '0.9' };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment