|
|
|
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' }; |