From 743cbde1971921f578eb37397c2a45188a952e69 Mon Sep 17 00:00:00 2001 From: homonoviscoding Date: Fri, 21 Jun 2024 15:23:06 +0100 Subject: [PATCH 1/3] first two passing tests --- package-lock.json | 48 +++++++++++++++++++++++------------------------ package.json | 2 +- src/server.js | 25 ++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 25 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3fb12fd..089713c 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" }, @@ -1442,11 +1442,11 @@ } }, "node_modules/braces": { - "version": "3.0.2", - "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.2.tgz", - "integrity": "sha512-b8um+L1RzM3WDSzvhm6gIz1yfTbBt6YTlcEKAvsmqCZZFw46z626lVj9j1yEPW33H5H+lBQpZMP1k8l+78Ha0A==", + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.3.tgz", + "integrity": "sha512-yQbXgO/OSZVD2IsiLlro+7Hf6Q18EJrKSEsdoMzKePKXct3gvD8oLcOQdIzGupr5Fj+EDe8gO/lxc1BzfMpxvA==", "dependencies": { - "fill-range": "^7.0.1" + "fill-range": "^7.1.1" }, "engines": { "node": ">=8" @@ -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", @@ -2094,9 +2094,9 @@ } }, "node_modules/fill-range": { - "version": "7.0.1", - "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz", - "integrity": "sha512-qOo9F+dMUmC2Lcb4BbVvnKJxTPjCm+RRpe4gDuGrzkL7mEVl/djYSu2OdQ2Pa302N4oqkSg9ir6jaLWJ2USVpQ==", + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.1.1.tgz", + "integrity": "sha512-YsGpe3WHLK8ZYi4tWDg2Jy3ebRz2rXowDxnld4bkQB00cc/1Zw9AWnC0i9ztDJitivtQvaI9KaLyKrc+hBW0yg==", "dependencies": { "to-regex-range": "^5.0.1" }, @@ -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..d8aab1f 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "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" }, diff --git a/src/server.js b/src/server.js index 30f0ce3..c9a6692 100644 --- a/src/server.js +++ b/src/server.js @@ -1,6 +1,7 @@ const express = require("express") const morgan = require("morgan") const cors = require("cors") +const contacts = require('../data/contacts') const app = express() app.use(morgan("dev")) @@ -9,5 +10,29 @@ app.use(express.json()) // write your app code here +app.get('/contacts', (req, res) => { + contacts + res.status(200).json({ + contacts + }) +}) + +app.get('/contacts/:id', (req, res) => { + const id = Number(req.params.id) + + const foundContact = contacts.find(i => i.id === 2) + + if ( id != 2 && !foundContact) { + return res.status(404).json({ + }) + } + res.status(200).json({ + contact: foundContact + }) +}) + +app.post('/contacts', (req, res) => { + req.body +}) module.exports = app From 7d9265cbf7288aa973e5c629f5a80f9fd127ff9a Mon Sep 17 00:00:00 2001 From: homonoviscoding Date: Fri, 21 Jun 2024 16:56:47 +0100 Subject: [PATCH 2/3] core assignment done --- src/server.js | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/server.js b/src/server.js index c9a6692..7494016 100644 --- a/src/server.js +++ b/src/server.js @@ -31,8 +31,31 @@ app.get('/contacts/:id', (req, res) => { }) }) +let id = contacts.length + 1 app.post('/contacts', (req, res) => { - req.body + const contact = req.body + contact.id = id + id++ + contacts.push(contact) + + res.status(201).json({ contact }) +}) + +app.delete('/contacts/:id', (req, res) => { + const id = Number(req.params.id) + + const contactFound = contacts.find((c) => c.id === id) + const contactIndex = contacts.indexOf(contactFound) + contacts.splice(contactIndex, 1) + return res.status(200).json({ contactFound }) +}) + +app.put('/contacts/:id', (req, res) => { + const contactId = Number(req.params.id) + const updatedContact = req.body + updatedContact.id = contactId + contacts.splice(contactId - 1, 1, updatedContact) + res.status(200).json({ 'contact': updatedContact }) }) module.exports = app From 55be444ed2820d1c9fa7a804fdc96d8bb975af23 Mon Sep 17 00:00:00 2001 From: homonoviscoding Date: Fri, 21 Jun 2024 17:02:49 +0100 Subject: [PATCH 3/3] standard tests passing --- src/server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/server.js b/src/server.js index 7494016..3121ec4 100644 --- a/src/server.js +++ b/src/server.js @@ -47,7 +47,7 @@ app.delete('/contacts/:id', (req, res) => { const contactFound = contacts.find((c) => c.id === id) const contactIndex = contacts.indexOf(contactFound) contacts.splice(contactIndex, 1) - return res.status(200).json({ contactFound }) + return res.status(200).json({ contact: contactFound }) }) app.put('/contacts/:id', (req, res) => {