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..2ef45a7 100644 --- a/src/server.js +++ b/src/server.js @@ -9,5 +9,65 @@ app.use(express.json()) // write your app code here +let contacts = [ + {id: 1, firstName: 'John', lastName: 'Carmack'}, + {id: 2, firstName: 'Grace', lastName: 'Hopper'}, + +] +let nextId = 3 + +//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) + if (contact) { + respond.status(200).json({ contact }) + } else { + //respond.status(404).send('Contact not found') + respond.status(404).json({ message: 'contact not found' }) + } +}) + +//create a new contact +app.post('/contacts', (request, respond) => { + const newContact = request.body + newContact.id = nextId++ + contacts.push(newContact) + respond.status(201).json({ contact: newContact }) + +}) + +// 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} + const updatedContact = contacts[index] + respond.status(200).json({contact: updatedContact}) + } else { + 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) + if (index !== -1) { + const deletedContact = contacts.splice(index, 1) + respond.status(200).json ({ contact: deletedContact[0] }) + } else { + respond.status(404).json('Message: contacts not found') + } +}) + module.exports = app