From a796488eb7b2a87a2cd9860ee565dd584ccf86a4 Mon Sep 17 00:00:00 2001 From: JDC-horizons Date: Tue, 30 Jul 2024 17:25:34 +0100 Subject: [PATCH 1/3] most of core --- package-lock.json | 34 ++++++++++++++++---------------- package.json | 9 +++++++-- src/server.js | 49 +++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 65 insertions(+), 27 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3fb12fd..fe39c38 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,7 @@ "license": "ISC", "dependencies": { "cors": "^2.8.5", - "express": "^4.18.2", + "express": "^4.19.2", "morgan": "^1.10.0", "nodemon": "^3.0.1" }, @@ -1410,12 +1410,12 @@ } }, "node_modules/body-parser": { - "version": "1.20.1", - "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz", - "integrity": "sha512-jWi7abTbYwajOytWCQc37VulmWiRae5RyTpaCyDcS5/lMdtwSz5lOpDE67srw/HYe35f1z3fDQw+3txg7gNtWw==", + "version": "1.20.2", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.2.tgz", + "integrity": "sha512-ml9pReCu3M61kGlqoTm2umSXTlRTuGTx0bfYj+uIUKKYycG5NtSbeetV3faSU6R7ajOPw0g/J1PvK4qNy7s5bA==", "dependencies": { "bytes": "3.1.2", - "content-type": "~1.0.4", + "content-type": "~1.0.5", "debug": "2.6.9", "depd": "2.0.0", "destroy": "1.2.0", @@ -1423,7 +1423,7 @@ "iconv-lite": "0.4.24", "on-finished": "2.4.1", "qs": "6.11.0", - "raw-body": "2.5.1", + "raw-body": "2.5.2", "type-is": "~1.6.18", "unpipe": "1.0.0" }, @@ -1730,9 +1730,9 @@ "dev": true }, "node_modules/cookie": { - "version": "0.5.0", - "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.5.0.tgz", - "integrity": "sha512-YZ3GUyn/o8gfKJlnlX7g7xq4gyO6OSuhGPKaaGssGB2qgDUS0gPgtTvoyZLTt9Ab6dC4hfc9dV5arkvc/OCmrw==", + "version": "0.6.0", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.6.0.tgz", + "integrity": "sha512-U71cyTamuh1CRNCfpGY6to28lxvNwPG4Guz/EVjgf3Jmzv0vlDp1atT9eS5dDjMYHucpHbWns6Lwf3BKz6svdw==", "engines": { "node": ">= 0.6" } @@ -2032,16 +2032,16 @@ } }, "node_modules/express": { - "version": "4.18.2", - "resolved": "https://registry.npmjs.org/express/-/express-4.18.2.tgz", - "integrity": "sha512-5/PsL6iGPdfQ/lKM1UuielYgv3BUoJfz1aUwU9vHZ+J7gyvwdQXFEBIEIaxeGf0GIcreATNyBExtalisDbuMqQ==", + "version": "4.19.2", + "resolved": "https://registry.npmjs.org/express/-/express-4.19.2.tgz", + "integrity": "sha512-5T6nhjsT+EOMzuck8JjBHARTHfMht0POzlA60WV2pMD3gyXw2LZnZ+ueGdNxG+0calOJcWKbpFcuzLZ91YWq9Q==", "dependencies": { "accepts": "~1.3.8", "array-flatten": "1.1.1", - "body-parser": "1.20.1", + "body-parser": "1.20.2", "content-disposition": "0.5.4", "content-type": "~1.0.4", - "cookie": "0.5.0", + "cookie": "0.6.0", "cookie-signature": "1.0.6", "debug": "2.6.9", "depd": "2.0.0", @@ -4013,9 +4013,9 @@ } }, "node_modules/raw-body": { - "version": "2.5.1", - "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.1.tgz", - "integrity": "sha512-qqJBtEyVgS0ZmPGdCFPWJ3FreoqvG4MVQln/kCgF7Olq95IbOp0/BWyMwbdtn4VTvkM8Y7khCQ2Xgk/tcrCXig==", + "version": "2.5.2", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.2.tgz", + "integrity": "sha512-8zGqypfENjCIqGhgXToC8aB2r7YrBX+AQAfIPs/Mlk+BtPTztOvTS01NRW/3Eh60J+a48lt8qsCzirQ6loCVfA==", "dependencies": { "bytes": "3.1.2", "http-errors": "2.0.0", diff --git a/package.json b/package.json index ec6510a..73abb42 100644 --- a/package.json +++ b/package.json @@ -20,12 +20,17 @@ "homepage": "https://github.com/boolean-uk/api-address-book#readme", "dependencies": { "cors": "^2.8.5", - "express": "^4.18.2", + "express": "^4.19.2", "morgan": "^1.10.0", "nodemon": "^3.0.1" }, "devDependencies": { "jest": "^29.7.0", "supertest": "^6.2.3" - } + }, + "directories": { + "doc": "docs", + "test": "test" + }, + "keywords": [] } diff --git a/src/server.js b/src/server.js index 30f0ce3..e81f012 100644 --- a/src/server.js +++ b/src/server.js @@ -1,13 +1,46 @@ -const express = require("express") -const morgan = require("morgan") -const cors = require("cors") -const app = express() +const express = require("express"); +const morgan = require("morgan"); +const cors = require("cors"); +const app = express(); +let contacts = require("../data/contacts.js"); +let meetings = require("../data/meetings.js"); -app.use(morgan("dev")) -app.use(cors()) -app.use(express.json()) +app.use(morgan("dev")); +app.use(cors()); +app.use(express.json()); // write your app code here +app.get("/contacts", function (req, res) { + res.status(200).json({ contacts: contacts }); +}); -module.exports = app +app.post("/contacts", function (req, res) { + const postContact = req.body; + const currentHighId = contacts.reduce((max, obj) => { + return obj.id > max ? obj.id : max; + }, 0); + postContact.id = currentHighId + 1; + contacts.push(postContact); + res.status(200).json({ contacts: contacts }); +}); + +app.get("/contacts/:id", function (req, res) { + const toFind = parseInt(req.params.id, 10); + const index = contacts.findIndex((obj) => obj.id === toFind); + const foundContact = contacts[index]; + if (foundContact) { + res.send({ contact: foundContact }); + } else { + res.sendStatus(404); + } +}); + +app.delete("/contacts/:id", function (req, res) { + const toRemove = parseInt(req.params.id, 10); + updated = contacts.filter((obj) => obj.id !== toRemove); + contacts = updated; + res.status(200).json({ contacts: contacts }); +}); + +module.exports = app; From 93e21f9d6170277a0e3b1ebdc9bb1e27aa8cc107 Mon Sep 17 00:00:00 2001 From: JDC-horizons Date: Wed, 31 Jul 2024 15:13:35 +0100 Subject: [PATCH 2/3] core complete --- src/server.js | 46 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/src/server.js b/src/server.js index e81f012..c2155ba 100644 --- a/src/server.js +++ b/src/server.js @@ -16,13 +16,26 @@ app.get("/contacts", function (req, res) { }); app.post("/contacts", function (req, res) { - const postContact = req.body; + const { firstName, lastName, street, city, type, email, linkedin, twitter } = + req.body; + if ( + !firstName || + !lastName || + !street || + !city || + !type || + !email || + !linkedin || + !twitter + ) { + return res.status(400); + } const currentHighId = contacts.reduce((max, obj) => { return obj.id > max ? obj.id : max; }, 0); - postContact.id = currentHighId + 1; - contacts.push(postContact); - res.status(200).json({ contacts: contacts }); + req.body.id = currentHighId + 1; + contacts.push(req.body); + res.status(201).json({ contacts: contacts }); }); app.get("/contacts/:id", function (req, res) { @@ -43,4 +56,29 @@ app.delete("/contacts/:id", function (req, res) { res.status(200).json({ contacts: contacts }); }); +app.put("/contacts/:id", function (req, res) { + const id = parseInt(req.params.id, 10); + const contactIndex = contacts.findIndex((contact) => contact.id === id); + if (contactIndex === -1) { + return res.status(404).json({ error: "Contact not found" }); + } + + const { firstName, lastName, street, city, type, email, linkedin, twitter } = + req.body; + const updatedContact = { + ...contacts[contactIndex], + firstName, + lastName, + street, + city, + type, + email, + linkedin, + twitter, + }; + contacts[contactIndex] = updatedContact; + + res.status(200).json({ updatedContact }); +}); + module.exports = app; From cde05a8f6cc30e9734ecd3de334083f39e057ddf Mon Sep 17 00:00:00 2001 From: JDC-horizons Date: Wed, 31 Jul 2024 15:26:49 +0100 Subject: [PATCH 3/3] fixed some things, now passing core tests --- src/server.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/server.js b/src/server.js index c2155ba..bfd02f0 100644 --- a/src/server.js +++ b/src/server.js @@ -35,7 +35,7 @@ app.post("/contacts", function (req, res) { }, 0); req.body.id = currentHighId + 1; contacts.push(req.body); - res.status(201).json({ contacts: contacts }); + res.status(201).json({ contact: req.body }); }); app.get("/contacts/:id", function (req, res) { @@ -51,9 +51,15 @@ app.get("/contacts/:id", function (req, res) { app.delete("/contacts/:id", function (req, res) { const toRemove = parseInt(req.params.id, 10); - updated = contacts.filter((obj) => obj.id !== toRemove); - contacts = updated; - res.status(200).json({ contacts: contacts }); + const contactIndex = contacts.findIndex((obj) => obj.id === toRemove); + if (contactIndex === -1) { + return res.status(404).json({ error: "Contact not found" }); + } + + const deletedContact = contacts[contactIndex]; + contacts = contacts.filter((obj) => obj.id !== toRemove); + + res.status(200).json({ contact: deletedContact }); }); app.put("/contacts/:id", function (req, res) { @@ -78,7 +84,7 @@ app.put("/contacts/:id", function (req, res) { }; contacts[contactIndex] = updatedContact; - res.status(200).json({ updatedContact }); + res.status(200).json({ contact: updatedContact }); }); module.exports = app;