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/sequence diagram.png b/sequence diagram.png new file mode 100644 index 0000000..c2c997b Binary files /dev/null and b/sequence diagram.png differ diff --git a/src/server.js b/src/server.js index 30f0ce3..2722a1b 100644 --- a/src/server.js +++ b/src/server.js @@ -7,7 +7,154 @@ app.use(morgan("dev")) app.use(cors()) app.use(express.json()) -// write your app code here +let contacts = require("../data/contacts.js") +let meetings = require("../data/meetings.js") +let idCounter = 3 +let meetingIdCounter = 4 + +app.get('/contacts', (req, res) => { + res.json({ + contacts + }) +}) + +app.post('/contacts', (req, res) => { + const contact = req.body + + if(!verifyContactProperties(contact)) { + return res.status(400).json({message: 'invalid body'}) + } + + contact.id = idCounter + contacts.push(contact) + + idCounter++ + + res.status(201).json({contact}) +}) + +app.get('/contacts/:id', (req, res) => { + const contactID = Number(req.params.id) + const foundContact = contacts.find((contact) => contact.id === contactID) + + res.json({contact: foundContact}) +}) + +app.delete('/contacts/:id', (req, res) => { + const contactID = Number(req.params.id) + const foundContact = contacts.find((contact) => contact.id === contactID) + + contacts = contacts.filter((contact) => contact.id !== contactID) + + meetings = meetings.filter((meeting) => meeting.contactId !== contactID) + + res.json({contact: foundContact}) +}) + +app.put('/contacts/:id', (req, res) => { + const newContactInfo = req.body + const contactID = Number(req.params.id) + + if(!verifyContactProperties(newContactInfo)) { + return res.status(400).json({message: 'invalid body'}) + } + + const foundContact = contacts.find((contact) => contact.id === contactID) + const foundContactIndex = contacts.indexOf(foundContact) + + newContactInfo.id = contactID + + contacts.splice(foundContactIndex, 1, newContactInfo) + + res.json({contact: newContactInfo}) +}) + +app.get('/meetings', (req, res) => { + res.json({meetings}) +}) + +app.get('/meetings/:id', (req, res) => { + const meetingID = Number(req.params.id) + const foundMeeting = meetings.find((meeting) => meeting.id === meetingID) + + res.json({meeting: foundMeeting}) +}) + +app.delete('/meetings/:id', (req, res) => { + const meetingID = Number(req.params.id) + const foundMeeting = meetings.find((meeting) => meeting.id === meetingID) + + meetings = meetings.filter((meeting) => meeting.id !== meetingID) + + res.json({meeting: foundMeeting}) +}) + +app.put('/meetings/:id', (req, res) => { + const newMeetingInfo = req.body + const contactID = Number(req.params.id) + + if(!verifyMeetingProperties(newMeetingInfo)) { + return res.status(400).json({message: 'invalid body'}) + } + + const foundMeeting = meetings.find((meeting) => meeting.contactId === contactID) + const foundMeetingIndex = meetings.indexOf(foundMeeting) + + newMeetingInfo.id = foundMeeting.id + newMeetingInfo.contactId = contactID + + meetings.splice(foundMeetingIndex, 1, newMeetingInfo) + + res.json({meeting: newMeetingInfo}) +}) + +app.get('/contacts/:id/meetings', (req, res) => { + const contactID = Number(req.params.id) + + const filteredMeetings = meetings.filter((meeting) => meeting.contactId === contactID) + + res.json({meetings: filteredMeetings}) +}) + +app.post('/contacts/:id/meetings', (req, res) => { + const contactID = Number(req.params.id) + const meetingInfo = req.body + + if(!verifyMeetingProperties(meetingInfo)) { + return res.status(400).json({message: 'invalid body'}) + } + + meetingInfo.contactId = contactID + meetingInfo.id = meetingIdCounter + + meetings.push(meetingInfo) + + meetingIdCounter++ + + res.status(201).json({meeting: meetingInfo}) +}) + +function verifyContactProperties(object) { + const neededProperties = ['firstName', 'lastName', 'street', 'city', 'type', 'email', 'linkedin', 'twitter'] + + for (const item of neededProperties) { + if (object[item] === undefined) { + return false + } + } + + return true +} + +function verifyMeetingProperties(object) { + const neededProperties = 'name' + + if (object[neededProperties] === undefined) { + return false + } + + return true +} module.exports = app