From 4347732154c31cb7e4916923195eb589dab7b924 Mon Sep 17 00:00:00 2001 From: Oluwagbemi Abiodun Date: Fri, 21 Jun 2024 13:39:11 +0100 Subject: [PATCH 1/6] test run works --- package-lock.json | 21 +++++++++++++++++++++ package.json | 1 + src/server.js | 4 ++++ 3 files changed, 26 insertions(+) diff --git a/package-lock.json b/package-lock.json index 3fb12fd..edfab90 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "cors": "^2.8.5", "express": "^4.18.2", "morgan": "^1.10.0", + "node": "^20.15.0", "nodemon": "^3.0.1" }, "devDependencies": { @@ -3563,6 +3564,26 @@ "node": ">= 0.6" } }, + "node_modules/node": { + "version": "20.15.0", + "resolved": "https://registry.npmjs.org/node/-/node-20.15.0.tgz", + "integrity": "sha512-zsJ9aOJAKhnR9rJ4BBfGmgiBmG3ZlLXLZ4jBjshPib+S8qKSY+ggBvphceT5gHF1X7KB4UP1ImU/A4EYr31Y2g==", + "hasInstallScript": true, + "dependencies": { + "node-bin-setup": "^1.0.0" + }, + "bin": { + "node": "bin/node" + }, + "engines": { + "npm": ">=5.0.0" + } + }, + "node_modules/node-bin-setup": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/node-bin-setup/-/node-bin-setup-1.1.3.tgz", + "integrity": "sha512-opgw9iSCAzT2+6wJOETCpeRYAQxSopqQ2z+N6BXwIMsQQ7Zj5M8MaafQY8JMlolRR6R1UXg2WmhKp0p9lSOivg==" + }, "node_modules/node-int64": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/node-int64/-/node-int64-0.4.0.tgz", diff --git a/package.json b/package.json index ec6510a..2d49cf5 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "cors": "^2.8.5", "express": "^4.18.2", "morgan": "^1.10.0", + "node": "^20.15.0", "nodemon": "^3.0.1" }, "devDependencies": { diff --git a/src/server.js b/src/server.js index 30f0ce3..a45ed60 100644 --- a/src/server.js +++ b/src/server.js @@ -9,5 +9,9 @@ app.use(express.json()) // write your app code here +app.get('/', (request, respond) => { + respond.send('Hello world') +}) + module.exports = app From 4258362967e9cc60a01b1db9ba9c38b3699298fd Mon Sep 17 00:00:00 2001 From: Oluwagbemi Abiodun Date: Fri, 21 Jun 2024 14:22:52 +0100 Subject: [PATCH 2/6] oo --- src/server.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/server.js b/src/server.js index a45ed60..e3f6b97 100644 --- a/src/server.js +++ b/src/server.js @@ -9,9 +9,25 @@ app.use(express.json()) // write your app code here -app.get('/', (request, respond) => { - respond.send('Hello world') +let contacts = [ + {id: 1, firstName: 'Abiodun', lastName: 'Oluwagbemi'}, + {id: 2, firstName: 'Nathan', lastName: 'King'}, + {id: 3, firstName: 'Lewis', lastName: 'Capaldi'} +] + +app.get('/contacts', (request, respond) => { + respond.status(200).json({ contacts }) }) +app.get('/contacts/:id', (request, respond) => { + const id = parseInt(request.params.id) + const contact = contacts.find(c => c.id === id) + if (contact) { + respond.status(200).json({ contact }) + } else { + respond.status(404).send('Contact not found') + } +}) + module.exports = app From ec4bfbc372f325a7e6fbe17cdc90f979ef956dd6 Mon Sep 17 00:00:00 2001 From: Oluwagbemi Abiodun Date: Fri, 21 Jun 2024 15:49:52 +0100 Subject: [PATCH 3/6] core done --- src/server.js | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/server.js b/src/server.js index e3f6b97..55f8f4f 100644 --- a/src/server.js +++ b/src/server.js @@ -14,6 +14,7 @@ let contacts = [ {id: 2, firstName: 'Nathan', lastName: 'King'}, {id: 3, firstName: 'Lewis', lastName: 'Capaldi'} ] +let nextId = 4 app.get('/contacts', (request, respond) => { respond.status(200).json({ contacts }) @@ -25,9 +26,41 @@ app.get('/contacts/:id', (request, respond) => { if (contact) { respond.status(200).json({ contact }) } else { - respond.status(404).send('Contact not found') + //respond.status(404).send('Contact not found') + respond.status(404).json({ contact }) } }) +app.post('/contacts', (request, respond) => { + const newContact = request.body + newContact.id = nextId++ + contacts.push(newContact) + respond.status(201).json({ contact: newContact }) + +}) + +app.put('/contacts/:id', (request, respond) => { + const id = parseInt(request.params.id) + const index = contacts.findIndex (c => c.id === id) + if (index !== -1) { + contacts[index] = {...contacts[index], ...request.body} + respond.status(200).json({contacts: contacts[index]}) + } else { + respond.status(404).json('Contacts not found') + } + +}) + +app.delete('/contacts/:id', (request, respond) => { + const id = parseInt(request.params.id) + const index = contacts.findIndex(c => c.id === id) + if (index !== -1) { + const deleteContact = contacts.splice(index, 1) + respond.status(200).json ({ contact: deleteContact[0] }) + } else { + respond.status(404).json('contacts not found') + } +}) + module.exports = app From d810d10393d155fed8e404f88dbacd8f613ceaee Mon Sep 17 00:00:00 2001 From: Oluwagbemi Abiodun Date: Fri, 21 Jun 2024 15:58:05 +0100 Subject: [PATCH 4/6] core done --- src/server.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/server.js b/src/server.js index 55f8f4f..e88dc94 100644 --- a/src/server.js +++ b/src/server.js @@ -16,10 +16,12 @@ let contacts = [ ] let nextId = 4 +//gets all contacts app.get('/contacts', (request, respond) => { respond.status(200).json({ contacts }) }) +// gets a contact by Id app.get('/contacts/:id', (request, respond) => { const id = parseInt(request.params.id) const contact = contacts.find(c => c.id === id) @@ -31,6 +33,7 @@ app.get('/contacts/:id', (request, respond) => { } }) +//create a new contact app.post('/contacts', (request, respond) => { const newContact = request.body newContact.id = nextId++ @@ -39,18 +42,22 @@ app.post('/contacts', (request, respond) => { }) +// edit and update a contact app.put('/contacts/:id', (request, respond) => { const id = parseInt(request.params.id) const index = contacts.findIndex (c => c.id === id) if (index !== -1) { contacts[index] = {...contacts[index], ...request.body} - respond.status(200).json({contacts: contacts[index]}) + const updatedContact = contacts[index] + respond.status(200).json({contact: updatedContact}) } else { - respond.status(404).json('Contacts not found') + respond.status(404).json('Message: Contacts not found') } }) + +//then delete a contact app.delete('/contacts/:id', (request, respond) => { const id = parseInt(request.params.id) const index = contacts.findIndex(c => c.id === id) From 0848453496f7a6d9967c6b350ea612b6c45e9590 Mon Sep 17 00:00:00 2001 From: Oluwagbemi Abiodun Date: Fri, 21 Jun 2024 16:02:30 +0100 Subject: [PATCH 5/6] core done --- src/server.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/server.js b/src/server.js index e88dc94..1f3c76b 100644 --- a/src/server.js +++ b/src/server.js @@ -62,10 +62,10 @@ app.delete('/contacts/:id', (request, respond) => { const id = parseInt(request.params.id) const index = contacts.findIndex(c => c.id === id) if (index !== -1) { - const deleteContact = contacts.splice(index, 1) - respond.status(200).json ({ contact: deleteContact[0] }) + const deletedContact = contacts.splice(index, 1) + respond.status(200).json ({ contact: deletedContact[0] }) } else { - respond.status(404).json('contacts not found') + respond.status(404).json('Message: contacts not found') } }) From 45b58251327e80c34a762c46676cdfb84502aebb Mon Sep 17 00:00:00 2001 From: Oluwagbemi Abiodun Date: Fri, 21 Jun 2024 16:21:48 +0100 Subject: [PATCH 6/6] core done --- src/server.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/server.js b/src/server.js index 1f3c76b..2ef45a7 100644 --- a/src/server.js +++ b/src/server.js @@ -10,11 +10,11 @@ app.use(express.json()) // write your app code here let contacts = [ - {id: 1, firstName: 'Abiodun', lastName: 'Oluwagbemi'}, - {id: 2, firstName: 'Nathan', lastName: 'King'}, - {id: 3, firstName: 'Lewis', lastName: 'Capaldi'} + {id: 1, firstName: 'John', lastName: 'Carmack'}, + {id: 2, firstName: 'Grace', lastName: 'Hopper'}, + ] -let nextId = 4 +let nextId = 3 //gets all contacts app.get('/contacts', (request, respond) => { @@ -29,7 +29,7 @@ app.get('/contacts/:id', (request, respond) => { respond.status(200).json({ contact }) } else { //respond.status(404).send('Contact not found') - respond.status(404).json({ contact }) + respond.status(404).json({ message: 'contact not found' }) } })