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..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/index.js b/src/index.js index 8c18618..b7d4462 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,6 @@ -const app = require('./server.js') -const port = 3030 +const app = require('./server'); +const port = process.env.PORT || 3000; app.listen(port, () => { - console.log(`Server is running on http://localhost:${port}/`) -}) + console.log(`Server is running on http://localhost:${port}`); +}); diff --git a/src/server.js b/src/server.js index 30f0ce3..30d4de2 100644 --- a/src/server.js +++ b/src/server.js @@ -1,13 +1,104 @@ -const express = require("express") -const morgan = require("morgan") -const cors = require("cors") -const app = express() +const express = require('express'); +const app = express(); -app.use(morgan("dev")) -app.use(cors()) -app.use(express.json()) +app.use(express.json()); -// write your app code here +// Data +let contacts = [ + { id: 1, firstName: "John", lastName: "Carmack" }, + { id: 2, firstName: "Grace", lastName: "Hopper" } +]; +let meetings = [ + { id: 1, name: "a test meeting about life", contactId: 1 }, + { id: 2, name: "another test meeting for wondering about existence", contactId: 2 }, + { id: 3, name: "a new meeting for the hopeful", contactId: 1 } +]; -module.exports = app +// Routes for contacts +app.get('/contacts', (req, res) => { + res.json({ contacts }); +}); + +app.get('/contacts/:id', (req, res) => { + const contact = contacts.find(c => c.id === parseInt(req.params.id)); + if (!contact) { + return res.status(404).send('Contact not found'); + } + res.json({ contact }); +}); + +app.post('/contacts', (req, res) => { + const newContact = { ...req.body, id: contacts.length + 1 }; + contacts.push(newContact); + res.status(201).json({ contact: newContact }); +}); + +app.put('/contacts/:id', (req, res) => { + const contact = contacts.find(c => c.id === parseInt(req.params.id)); + if (!contact) { + return res.status(404).send('Contact not found'); + } + Object.assign(contact, req.body); + res.json({ contact }); +}); + +app.delete('/contacts/:id', (req, res) => { + const contactIndex = contacts.findIndex(c => c.id === parseInt(req.params.id)); + if (contactIndex === -1) { + return res.status(404).send('Contact not found'); + } + const [deletedContact] = contacts.splice(contactIndex, 1); + meetings = meetings.filter(m => m.contactId !== deletedContact.id); + res.json({ contact: deletedContact }); +}); + +// Routes for meetings +app.get('/meetings', (req, res) => { + res.json({ meetings }); +}); + +app.get('/meetings/:id', (req, res) => { + const meeting = meetings.find(m => m.id === parseInt(req.params.id)); + if (!meeting) { + return res.status(404).send('Meeting not found'); + } + res.json({ meeting }); +}); + +app.post('/contacts/:id/meetings', (req, res) => { + const contactId = parseInt(req.params.id); + const contact = contacts.find(c => c.id === contactId); + if (!contact) { + return res.status(404).send('Contact not found'); + } + const newMeeting = { ...req.body, id: meetings.length + 1, contactId }; + meetings.push(newMeeting); + res.status(201).json({ meeting: newMeeting }); +}); + +app.put('/meetings/:id', (req, res) => { + const meeting = meetings.find(m => m.id === parseInt(req.params.id)); + if (!meeting) { + return res.status(404).send('Meeting not found'); + } + Object.assign(meeting, req.body); + res.json({ meeting }); +}); + +app.delete('/meetings/:id', (req, res) => { + const meetingIndex = meetings.findIndex(m => m.id === parseInt(req.params.id)); + if (meetingIndex === -1) { + return res.status(404).send('Meeting not found'); + } + const [deletedMeeting] = meetings.splice(meetingIndex, 1); + res.json({ meeting: deletedMeeting }); +}); + +app.get('/contacts/:id/meetings', (req, res) => { + const contactId = parseInt(req.params.id); + const contactMeetings = meetings.filter(m => m.contactId === contactId); + res.json({ meetings: contactMeetings }); +}); + +module.exports = app;