From 04c4c5cfc7c488e70d438db91b12e459769c09de Mon Sep 17 00:00:00 2001 From: Loza_TM Date: Sun, 7 Jan 2024 22:16:32 +0100 Subject: [PATCH 1/7] initiating express app and logging and handling cors --- src/server.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/server.js b/src/server.js index 30f0ce3..d87ae0e 100644 --- a/src/server.js +++ b/src/server.js @@ -9,5 +9,15 @@ app.use(express.json()) // write your app code here +const contacts = require("../data/contacts.js"); +const meetings = require("../data/meetings.js"); + +// initializing express app +const app = express(); + +// Middleware setup for logging and handling CORS +app.use(morgan("dev")); +app.use(cors()); +app.use(express.json()); module.exports = app From c9f15079ac02f8ec5f774f860adf319cd74b5da8 Mon Sep 17 00:00:00 2001 From: Loza_TM Date: Sun, 7 Jan 2024 22:18:23 +0100 Subject: [PATCH 2/7] intial state setup for contact and meetings --- src/server.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/server.js b/src/server.js index d87ae0e..6d38ae0 100644 --- a/src/server.js +++ b/src/server.js @@ -21,3 +21,12 @@ app.use(cors()); app.use(express.json()); module.exports = app + +// intial state setup for contacts and meetings +const STATE = { + contacts, + nextContactId: 3, + meetings, + nextMeetingId: 4, + }; + From 7b9d0b1704e7f95aedd929806647b029bbd16c10 Mon Sep 17 00:00:00 2001 From: Loza_TM Date: Sun, 7 Jan 2024 22:19:33 +0100 Subject: [PATCH 3/7] function to get the next contact and meeting ID --- src/server.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/server.js b/src/server.js index 6d38ae0..4fd7c4b 100644 --- a/src/server.js +++ b/src/server.js @@ -29,4 +29,14 @@ const STATE = { meetings, nextMeetingId: 4, }; + +// Function to get the next contact ID and meeting ID +const getNextContactId = () => { + return STATE.nextContactId++; + }; + + const getNextMeetingId = () => { + return STATE.nextMeetingId++; + }; + From add3ff260574535b4e73ad5f2543c2b6bd52e832 Mon Sep 17 00:00:00 2001 From: Loza_TM Date: Sun, 7 Jan 2024 22:21:04 +0100 Subject: [PATCH 4/7] function to find index until route to update a contact ID --- src/server.js | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/src/server.js b/src/server.js index 4fd7c4b..e68ee1b 100644 --- a/src/server.js +++ b/src/server.js @@ -38,5 +38,59 @@ const getNextContactId = () => { const getNextMeetingId = () => { return STATE.nextMeetingId++; }; + +/** + * Function to find the index of an item in an array based on the ID in the request parameters. + * @param {array} array - The array to search in. + * @param {import('express').Request} req - The Express request object. + * @returns {number} - Index of the item in the array. + */ +const findStateIndex = (array, req) => { + const { id } = req.params; + const foundIndex = array.findIndex((contact) => contact.id === Number(id)); + return foundIndex; + }; + + // Route to get all contacts + app.get("/contacts", (req, res) => { + res.json({ contacts: STATE.contacts }); + }); + + // Route to add a new contact + app.post("/contacts", (req, res) => { + const count = STATE.contacts.push(req.body); + const newContact = { contact: STATE.contacts[count - 1] }; + newContact.contact.id = getNextContactId(); + res.status(201).json(newContact); + }); + + // Route to get a specific contact by ID + app.get("/contacts/:id", (req, res) => { + const foundIndex = findStateIndex(STATE.contacts, req); + res.json({ contact: STATE.contacts[foundIndex] }); + }); + + // Route to delete a contact by ID + app.delete("/contacts/:id", (req, res) => { + const foundContactIndex = findStateIndex(STATE.contacts, req); + const [removedContact] = STATE.contacts.splice(foundContactIndex, 1); + + // Remove associated meetings for the deleted contact + STATE.meetings = STATE.meetings.filter( + (meeting) => meeting.contactId !== Number(req.params.id) + ); + + res.json({ contact: removedContact }); + }); + + // Route to update a contact by ID + app.put("/contacts/:id", (req, res) => { + const foundIndex = findStateIndex(STATE.contacts, req); + const foundContact = (STATE.contacts[foundIndex] = { + ...req.body, + id: Number(req.params.id), + }); + res.json({ contact: foundContact }); + }); From 27c72380a3ef754f6bc9c1228dcdd1cbcb4d8ab8 Mon Sep 17 00:00:00 2001 From: Loza_TM Date: Sun, 7 Jan 2024 22:22:59 +0100 Subject: [PATCH 5/7] test done using npm test --- src/server.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/server.js b/src/server.js index e68ee1b..bc3c262 100644 --- a/src/server.js +++ b/src/server.js @@ -94,3 +94,5 @@ const findStateIndex = (array, req) => { res.json({ contact: foundContact }); }); + + module.exports = app; From c301d395c07bdbd2d9aed4d2000717945190a309 Mon Sep 17 00:00:00 2001 From: Loza_TM Date: Sun, 7 Jan 2024 22:26:44 +0100 Subject: [PATCH 6/7] handling HTTP GET, Delete , Put , Post requests --- src/server.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/server.js b/src/server.js index bc3c262..cf8f818 100644 --- a/src/server.js +++ b/src/server.js @@ -93,6 +93,51 @@ const findStateIndex = (array, req) => { res.json({ contact: foundContact }); }); + + + // Code for the meeting part + + // Codes --- route for handling HTTP GET, Delete , Put , Post requests to the "/meetings by ID" endpoint. + +app.get("/meetings", (req, res) => { + res.json({ meetings: STATE.meetings }); + }); + + app.get("/meetings/:id", (req, res) => { + const foundIndex = findStateIndex(STATE.meetings, req); + res.json({ meeting: STATE.meetings[foundIndex] }); + }); + + app.delete("/meetings/:id", (req, res) => { + const foundIndex = findStateIndex(STATE.meetings, req); + const [removedMeeting] = STATE.meetings.splice(foundIndex, 1); + res.json({ meeting: removedMeeting }); + }); + + app.put("/meetings/:id", (req, res) => { + const foundMeetingIndex = findStateIndex(STATE.meetings, req); + const foundMeeting = STATE.meetings[foundMeetingIndex]; + foundMeeting.name = req.body.name; + res.json({ meeting: foundMeeting }); + }); + + app.get("/contacts/:id/meetings", (req, res) => { + const foundContactIndex = findStateIndex(STATE.contacts, req); + const foundMeetings = STATE.meetings.filter( + (meeting) => meeting.contactId === Number(req.params.id) + ); + res.json({ meetings: foundMeetings }); + }); + + app.post("/contacts/:id/meetings", (req, res) => { + const count = STATE.meetings.push(req.body); + const newMeeting = { meeting: STATE.meetings[count - 1] }; + + newMeeting.meeting.id = getNextMeetingId(); + newMeeting.meeting.contactId = Number(req.params.id); + + res.status(201).json(newMeeting); + }); module.exports = app; From b7519f654ba5de97e6ac03cb3c6086f6b9fdda39 Mon Sep 17 00:00:00 2001 From: Loza_TM Date: Sun, 7 Jan 2024 22:31:12 +0100 Subject: [PATCH 7/7] error fixed --- src/server.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/server.js b/src/server.js index cf8f818..24a40ec 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") +// initializing express app const app = express() app.use(morgan("dev")) @@ -12,8 +13,7 @@ app.use(express.json()) const contacts = require("../data/contacts.js"); const meetings = require("../data/meetings.js"); -// initializing express app -const app = express(); + // Middleware setup for logging and handling CORS app.use(morgan("dev"));