diff --git a/index.js b/index.js index 8b13789..22018a9 100644 --- a/index.js +++ b/index.js @@ -1 +1,162 @@ +var http = require('http'); +var users = require("./state.js").users; +var products = require("./state.js").products; + + +var server = http.createServer(messageReceived); + +server.listen(8080); + + + +function messageReceived(req, res) { + let toDisplay = "Complete"; + + if (req.method === "GET") { + toDisplay = getRecordsToDisplayOnGetRequest(req.url); + res.writeHead(200, {'Content-Type': 'application/json'}); + res.write(toDisplay); + res.end(); + } + else if (req.method == "PUT") { + toDisplay = markElementAsModifiedById(req.url); + let httpVerb = 400; + + if (toDisplay) { + httpVerb = 204; + } + res.writeHead(httpVerb, {'Content-Type': 'application/json'}); + res.write(JSON.stringify(toDisplay)); + res.end(); + } + else if (req.method == "POST") { + let body = []; + + if (req.url == "/users" || req.url == "/products") { + req.on('data', (chunk) => { + body.push(chunk); + }).on('end', () => { + body = Buffer.concat(body).toString(); + + let element = JSON.parse(body); + + if (req.url == "/users") { + element._id = users.length + 1; + users.push(element); + } + else { + element._id = products.length + 1; + products.push(element); + } + + toDisplay = JSON.stringify(element); + + res.writeHead(200, {'Content-Type': 'application/json'}); + res.write(toDisplay); + res.end(); + }); + } + else { + toDisplay = JSON.stringify("ERROR: Not a valid element"); + + res.writeHead(400, {'Content-Type': 'text/plain'}); + res.write(toDisplay); + res.end(); + } + } + else if (req.method == "DELETE") { + toDisplay = markElementAsDeletedById(req.url); + let httpVerb = 400; + + if (toDisplay) { + httpVerb = 200; + toDisplay = "deleted"; + } + res.writeHead(httpVerb, {'Content-Type': 'application/json'}); + res.write(JSON.stringify(toDisplay)); + res.end(); + } + else { + toDisplay = "METHOD NOT ALLOWED"; + res.writeHead(400, {'Content-Type': 'text/plain'}); + res.write(toDisplay); + res.end(); + } +} + + +function markElementAsDeletedById(url) { + let success = true; + let elements = getElementsFromRequest(url); + + //A single element got returned + if (elements && elements.length == null) { + elements.isActive = false; + } + //Either no element or all elements got returned + else { + success = false; + } + return success; +} + + +function markElementAsModifiedById(url) { + let success = true; + let elements = getElementsFromRequest(url); + + //A single element got returned + if (elements && elements.length == null) { + elements._modified = true; + } + //Either no element or all elements got returned + else { + success = false; + } + return success; +} + +function getElementsFromRequest(url) { + let idToFetchRegEx = /\d+/g; + let idToFetch = idToFetchRegEx.exec(url); + + let toReturn; + + if (url.toLowerCase().includes("/users")) { + + if (idToFetch && idToFetch.length > 0) { + toReturn = getRecordById(idToFetch.join(), users); + } + else { + toReturn = users; + } + } + else if (url.toLowerCase().includes("/products")) { + + if (idToFetch && idToFetch.length > 0) { + toReturn = getRecordById(idToFetch.join(), products); + } + else { + toReturn = products; + } + } + else { + toReturn = null; + } + return toReturn; +} + + +function getRecordsToDisplayOnGetRequest(url) { + return JSON.stringify(getElementsFromRequest(url)); +} + +function getRecordById(id, myArray) { + let result = myArray.find( element => element._id == id ); + + if (result) + return result; + else + return null; +} diff --git a/state.js b/state.js index ebab277..d202e76 100644 --- a/state.js +++ b/state.js @@ -33,7 +33,7 @@ exports.users = [ exports.products = [ { - "id": 1, + "_id": 1, "name": "Body Luxuries Sweet Lavender Hand Sanitizer", "description": "Makes your hands clean", "rating": 2, @@ -84,7 +84,7 @@ exports.products = [ ] }, { - "id": 2, + "_id": 2, "name": "Topiramate", "description": "A wonderful medicine that makes everything all better", "rating": 2, @@ -115,7 +115,7 @@ exports.products = [ ] }, { - "id": 3, + "_id": 3, "name": "Almond", "description": "A great treat that tastes great", "rating": 5, @@ -138,7 +138,7 @@ exports.products = [ ] }, { - "id": 4, + "_id": 4, "name": "VYTORIN", "description": "Orchard as the place of occurrence of the external cause", "rating": 3, @@ -161,7 +161,7 @@ exports.products = [ ] }, { - "id": 5, + "_id": 5, "name": "Decolorized Iodine", "description": "Kills germs on contact", "rating": 1, @@ -184,7 +184,7 @@ exports.products = [ ] }, { - "id": 6, + "_id": 6, "name": "Fresh Sugar Honey Tinted Lip Treatment SPF15", "description": "Fix those chapped lips. ", "rating": 3, @@ -207,7 +207,7 @@ exports.products = [ ] }, { - "id": 7, + "_id": 7, "name": "LBel", "description": "2-Propanol", "rating": 3, @@ -230,7 +230,7 @@ exports.products = [ ] }, { - "id": 8, + "_id": 8, "name": "Cholestyramine", "description": "Help reduce cholesteral in the system", "rating": 3, @@ -253,7 +253,7 @@ exports.products = [ ] }, { - "id": 9, + "_id": 9, "name": "Risperidone", "description": "cephalospor/oth beta-lactm antibiot, undet, sequela", "rating": 1, @@ -276,7 +276,7 @@ exports.products = [ ] }, { - "id": 10, + "_id": 10, "name": "MAC", "description": "Other Gram-negative sepsis", "rating": 2,