diff --git a/New_APIs/College_Lectures_API/package-lock.json b/New_APIs/College_Lectures_API/package-lock.json index 9d058b2..aa26337 100644 --- a/New_APIs/College_Lectures_API/package-lock.json +++ b/New_APIs/College_Lectures_API/package-lock.json @@ -12,7 +12,30 @@ "all": "^0.0.0", "body-parser": "^1.20.2", "cors": "^2.8.5", - "express": "^4.19.2" + "dotenv": "^16.4.5", + "express": "^4.19.2", + "mongoose": "^8.3.4" + } + }, + "node_modules/@mongodb-js/saslprep": { + "version": "1.1.7", + "resolved": "https://registry.npmjs.org/@mongodb-js/saslprep/-/saslprep-1.1.7.tgz", + "integrity": "sha512-dCHW/oEX0KJ4NjDULBo3JiOaK5+6axtpBbS+ao2ZInoAL9/YRQLhXzSNAFz7hP4nzLkIqsfYAK/PDE3+XHny0Q==", + "dependencies": { + "sparse-bitfield": "^3.0.3" + } + }, + "node_modules/@types/webidl-conversions": { + "version": "7.0.3", + "resolved": "https://registry.npmjs.org/@types/webidl-conversions/-/webidl-conversions-7.0.3.tgz", + "integrity": "sha512-CiJJvcRtIgzadHCYXw7dqEnMNRjhGZlYK05Mj9OyktqV8uVT8fD2BFOB7S1uwBE3Kj2Z+4UyPmFw/Ixgw/LAlA==" + }, + "node_modules/@types/whatwg-url": { + "version": "11.0.4", + "resolved": "https://registry.npmjs.org/@types/whatwg-url/-/whatwg-url-11.0.4.tgz", + "integrity": "sha512-lXCmTWSHJvf0TRSO58nm978b8HJ/EdsSsEKLd3ODHFjo+3VGAyyTp4v50nWvwtzBxSMQrVOK7tcuN0zGPLICMw==", + "dependencies": { + "@types/webidl-conversions": "*" } }, "node_modules/accepts": { @@ -60,6 +83,14 @@ "npm": "1.2.8000 || >= 1.4.16" } }, + "node_modules/bson": { + "version": "6.7.0", + "resolved": "https://registry.npmjs.org/bson/-/bson-6.7.0.tgz", + "integrity": "sha512-w2IquM5mYzYZv6rs3uN2DZTOBe2a0zXLj53TGDqwF4l6Sz/XsISrisXOJihArF9+BZ6Cq/GjVht7Sjfmri7ytQ==", + "engines": { + "node": ">=16.20.1" + } + }, "node_modules/bytes": { "version": "3.1.2", "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.2.tgz", @@ -171,6 +202,17 @@ "npm": "1.2.8000 || >= 1.4.16" } }, + "node_modules/dotenv": { + "version": "16.4.5", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.4.5.tgz", + "integrity": "sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg==", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://dotenvx.com" + } + }, "node_modules/ee-first": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", @@ -410,6 +452,14 @@ "node": ">= 0.10" } }, + "node_modules/kareem": { + "version": "2.6.3", + "resolved": "https://registry.npmjs.org/kareem/-/kareem-2.6.3.tgz", + "integrity": "sha512-C3iHfuGUXK2u8/ipq9LfjFfXFxAZMQJJq7vLS45r3D9Y2xQ/m4S8zaR4zMLFWh9AsNPXmcFfUDhTEO8UIC/V6Q==", + "engines": { + "node": ">=12.0.0" + } + }, "node_modules/media-typer": { "version": "0.3.0", "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", @@ -418,6 +468,11 @@ "node": ">= 0.6" } }, + "node_modules/memory-pager": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/memory-pager/-/memory-pager-1.5.0.tgz", + "integrity": "sha512-ZS4Bp4r/Zoeq6+NLJpP+0Zzm0pR8whtGPf1XExKLJBAczGMnSi3It14OiNCStjQjM6NU1okjQGSxgEZN8eBYKg==" + }, "node_modules/merge-descriptors": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.1.tgz", @@ -461,6 +516,126 @@ "node": ">= 0.6" } }, + "node_modules/mongodb": { + "version": "6.5.0", + "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-6.5.0.tgz", + "integrity": "sha512-Fozq68InT+JKABGLqctgtb8P56pRrJFkbhW0ux+x1mdHeyinor8oNzJqwLjV/t5X5nJGfTlluxfyMnOXNggIUA==", + "dependencies": { + "@mongodb-js/saslprep": "^1.1.5", + "bson": "^6.4.0", + "mongodb-connection-string-url": "^3.0.0" + }, + "engines": { + "node": ">=16.20.1" + }, + "peerDependencies": { + "@aws-sdk/credential-providers": "^3.188.0", + "@mongodb-js/zstd": "^1.1.0", + "gcp-metadata": "^5.2.0", + "kerberos": "^2.0.1", + "mongodb-client-encryption": ">=6.0.0 <7", + "snappy": "^7.2.2", + "socks": "^2.7.1" + }, + "peerDependenciesMeta": { + "@aws-sdk/credential-providers": { + "optional": true + }, + "@mongodb-js/zstd": { + "optional": true + }, + "gcp-metadata": { + "optional": true + }, + "kerberos": { + "optional": true + }, + "mongodb-client-encryption": { + "optional": true + }, + "snappy": { + "optional": true + }, + "socks": { + "optional": true + } + } + }, + "node_modules/mongodb-connection-string-url": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/mongodb-connection-string-url/-/mongodb-connection-string-url-3.0.1.tgz", + "integrity": "sha512-XqMGwRX0Lgn05TDB4PyG2h2kKO/FfWJyCzYQbIhXUxz7ETt0I/FqHjUeqj37irJ+Dl1ZtU82uYyj14u2XsZKfg==", + "dependencies": { + "@types/whatwg-url": "^11.0.2", + "whatwg-url": "^13.0.0" + } + }, + "node_modules/mongoose": { + "version": "8.3.4", + "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-8.3.4.tgz", + "integrity": "sha512-ckBaBzKgtWgCalW/LPkcBsR3wKCOYEJ9jLFPmYCYV7TLStpETY757ELx8/1stL11+6HxLLVffawBffXzd0Y7YA==", + "dependencies": { + "bson": "^6.5.0", + "kareem": "2.6.3", + "mongodb": "6.5.0", + "mpath": "0.9.0", + "mquery": "5.0.0", + "ms": "2.1.3", + "sift": "17.1.3" + }, + "engines": { + "node": ">=16.20.1" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/mongoose" + } + }, + "node_modules/mongoose/node_modules/ms": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==" + }, + "node_modules/mpath": { + "version": "0.9.0", + "resolved": "https://registry.npmjs.org/mpath/-/mpath-0.9.0.tgz", + "integrity": "sha512-ikJRQTk8hw5DEoFVxHG1Gn9T/xcjtdnOKIU1JTmGjZZlg9LST2mBLmcX3/ICIbgJydT2GOc15RnNy5mHmzfSew==", + "engines": { + "node": ">=4.0.0" + } + }, + "node_modules/mquery": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/mquery/-/mquery-5.0.0.tgz", + "integrity": "sha512-iQMncpmEK8R8ncT8HJGsGc9Dsp8xcgYMVSbs5jgnm1lFHTZqMJTUWTDx1LBO8+mK3tPNZWFLBghQEIOULSTHZg==", + "dependencies": { + "debug": "4.x" + }, + "engines": { + "node": ">=14.0.0" + } + }, + "node_modules/mquery/node_modules/debug": { + "version": "4.3.4", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", + "integrity": "sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==", + "dependencies": { + "ms": "2.1.2" + }, + "engines": { + "node": ">=6.0" + }, + "peerDependenciesMeta": { + "supports-color": { + "optional": true + } + } + }, + "node_modules/mquery/node_modules/ms": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" + }, "node_modules/ms": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", @@ -526,6 +701,14 @@ "node": ">= 0.10" } }, + "node_modules/punycode": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz", + "integrity": "sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg==", + "engines": { + "node": ">=6" + } + }, "node_modules/qs": { "version": "6.11.0", "resolved": "https://registry.npmjs.org/qs/-/qs-6.11.0.tgz", @@ -666,6 +849,19 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/sift": { + "version": "17.1.3", + "resolved": "https://registry.npmjs.org/sift/-/sift-17.1.3.tgz", + "integrity": "sha512-Rtlj66/b0ICeFzYTuNvX/EF1igRbbnGSvEyT79McoZa/DeGhMyC5pWKOEsZKnpkqtSeovd5FL/bjHWC3CIIvCQ==" + }, + "node_modules/sparse-bitfield": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/sparse-bitfield/-/sparse-bitfield-3.0.3.tgz", + "integrity": "sha512-kvzhi7vqKTfkh0PZU+2D2PIllw2ymqJKujUcyPMd9Y75Nv4nPbGJZXNhxsgdQab2BmlDct1YnfQCguEvHr7VsQ==", + "dependencies": { + "memory-pager": "^1.0.2" + } + }, "node_modules/statuses": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/statuses/-/statuses-2.0.1.tgz", @@ -682,6 +878,17 @@ "node": ">=0.6" } }, + "node_modules/tr46": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/tr46/-/tr46-4.1.1.tgz", + "integrity": "sha512-2lv/66T7e5yNyhAAC4NaKe5nVavzuGJQVVtRYLyQ2OI8tsJ61PMLlelehb0wi2Hx6+hT/OJUWZcw8MjlSRnxvw==", + "dependencies": { + "punycode": "^2.3.0" + }, + "engines": { + "node": ">=14" + } + }, "node_modules/type-is": { "version": "1.6.18", "resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz", @@ -717,6 +924,26 @@ "engines": { "node": ">= 0.8" } + }, + "node_modules/webidl-conversions": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-7.0.0.tgz", + "integrity": "sha512-VwddBukDzu71offAQR975unBIGqfKZpM+8ZX6ySk8nYhVoo5CYaZyzt3YBvYtRtO+aoGlqxPg/B87NGVZ/fu6g==", + "engines": { + "node": ">=12" + } + }, + "node_modules/whatwg-url": { + "version": "13.0.0", + "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-13.0.0.tgz", + "integrity": "sha512-9WWbymnqj57+XEuqADHrCJ2eSXzn8WXIW/YSGaZtb2WKAInQ6CHfaUUcTyyver0p8BDg5StLQq8h1vtZuwmOig==", + "dependencies": { + "tr46": "^4.1.1", + "webidl-conversions": "^7.0.0" + }, + "engines": { + "node": ">=16" + } } } } diff --git a/New_APIs/College_Lectures_API/package.json b/New_APIs/College_Lectures_API/package.json index ff84b4d..f335f4e 100644 --- a/New_APIs/College_Lectures_API/package.json +++ b/New_APIs/College_Lectures_API/package.json @@ -12,6 +12,8 @@ "all": "^0.0.0", "body-parser": "^1.20.2", "cors": "^2.8.5", - "express": "^4.19.2" + "dotenv": "^16.4.5", + "express": "^4.19.2", + "mongoose": "^8.3.4" } } diff --git a/New_APIs/College_Lectures_API/src/index.js b/New_APIs/College_Lectures_API/src/index.js index 45ec13f..1123de5 100644 --- a/New_APIs/College_Lectures_API/src/index.js +++ b/New_APIs/College_Lectures_API/src/index.js @@ -2,69 +2,131 @@ const express = require('express'); const bodyParser = require('body-parser'); const path = require('path'); const cors = require('cors'); +const mongoose = require('mongoose'); +const dotenv = require('dotenv'); const app = express(); +dotenv.config(); app.use(cors()); const PORT = process.env.PORT || 3000; -// Sample data for demonstration -let lectures = [ - { id: 1, title: 'Introduction to Computer Science', instructor: 'John Doe', time: '9:00 AM', room: 'Room 101' }, - { id: 2, title: 'Advanced Mathematics', instructor: 'Jane Smith', time: '11:00 AM', room: 'Room 201' }, - { id: 3, title: 'Operating System', instructor: 'Alice Johnson', time: '10:00 AM', room: 'Room 102' }, - { id: 4, title: 'Software Engineering', instructor: 'Michael Brown', time: '1:00 PM', room: 'Room 301' } +// Connect to MongoDB +mongoose.connect(process.env.MONGO_URL).then(() => { + app.listen(PORT, () => console.log(`Server Port: ${PORT}`)); +}).catch((error) => console.log(`${error} did not connect!`)); + +// Lecture Schema +const lectureSchema = new mongoose.Schema({ + title: String, + instructor: String, + time: String, + room: String +}); + +const Lecture = mongoose.model('Lecture', lectureSchema); + +// Data to save +const lectures = [ + { title: 'Introduction to Computer Science', instructor: 'John Doe', time: '9:00 AM', room: 'Room 101' }, + { title: 'Advanced Mathematics', instructor: 'Jane Smith', time: '11:00 AM', room: 'Room 201' }, + { title: 'Operating System', instructor: 'Alice Johnson', time: '10:00 AM', room: 'Room 102' }, + { title: 'Software Engineering', instructor: 'Michael Brown', time: '1:00 PM', room: 'Room 301' } ]; +// Function to save lectures to the database +async function saveLectures() { + try { + for (const lectureData of lectures) { + const lecture = new Lecture(lectureData); + await lecture.save(); + } + console.log('Lectures saved successfully'); + } catch (error) { + console.error('Error saving lectures:', error); + } finally { + mongoose.disconnect(); + } +} + +// Call the function to save lectures for one time +{/*saveLectures();*/} + // Middleware app.use(bodyParser.json()); app.use(express.static(path.join(__dirname, 'public'))); // Routes // Get all lectures -app.get('/lectures', (req, res) => { - res.json(lectures); +app.get('/lectures', async (req, res) => { + try { + const lectures = await Lecture.find(); + res.json(lectures); + } catch (err) { + res.status(500).json({ message: err.message }); + } }); // Get a specific lecture by ID -app.get('/lectures/:id', (req, res) => { - const lectureId = parseInt(req.params.id); - const lecture = lectures.find(lecture => lecture.id === lectureId); - if (!lecture) { - return res.status(404).json({ message: 'Lecture not found' }); - } - res.json(lecture); +app.get('/lectures/:id', getLecture, (req, res) => { + res.json(res.lecture); }); // Create a new lecture -app.post('/lectures', (req, res) => { - const { title, instructor, time, room } = req.body; - const id = lectures.length + 1; - const newLecture = { id, title, instructor, time, room }; - lectures.push(newLecture); - res.status(201).json(newLecture); +app.post('/lectures', async (req, res) => { + const lecture = new Lecture({ + title: req.body.title, + instructor: req.body.instructor, + time: req.body.time, + room: req.body.room + }); + + try { + const newLecture = await lecture.save(); + res.status(201).json(newLecture); + } catch (err) { + res.status(400).json({ message: err.message }); + } }); +// Middleware function to get a lecture by ID +async function getLecture(req, res, next) { + let lecture; + try { + lecture = await Lecture.findById(req.params.id); + if (lecture == null) { + return res.status(404).json({ message: 'Lecture not found' }); + } + } catch (err) { + return res.status(500).json({ message: err.message }); + } + + res.lecture = lecture; + next(); +} + // Update a lecture -app.put('/lectures/:id', (req, res) => { - const lectureId = parseInt(req.params.id); +app.put('/lectures/:id', async (req, res) => { const { title, instructor, time, room } = req.body; - const lectureIndex = lectures.findIndex(lecture => lecture.id === lectureId); - if (lectureIndex === -1) { - return res.status(404).json({ message: 'Lecture not found' }); + try { + const updatedLecture = await Lecture.findByIdAndUpdate(req.params.id, { title, instructor, time, room }, { new: true }); + if (!updatedLecture) { + return res.status(404).json({ message: 'Lecture not found' }); + } + res.json(updatedLecture); + } catch (err) { + res.status(400).json({ message: err.message }); } - const updatedLecture = { id: lectureId, title, instructor, time, room }; - lectures[lectureIndex] = updatedLecture; - res.json(updatedLecture); }); // Delete a lecture -app.delete('/lectures/:id', (req, res) => { - const lectureId = parseInt(req.params.id); - lectures = lectures.filter(lecture => lecture.id !== lectureId); - res.status(204).end(); +app.delete('/lectures/:id', async (req, res) => { + try { + const deletedLecture = await Lecture.findByIdAndDelete(req.params.id); + if (!deletedLecture) { + return res.status(404).json({ message: 'Lecture not found' }); + } + res.status(204).end(); + } catch (err) { + res.status(500).json({ message: err.message }); + } }); - -// Start the server -app.listen(PORT, () => { - console.log(`Server is running on port ${PORT}`); -}); \ No newline at end of file diff --git a/New_APIs/College_Lectures_API/src/lect.js b/New_APIs/College_Lectures_API/src/lect.js index e44fc16..eb78b6f 100644 --- a/New_APIs/College_Lectures_API/src/lect.js +++ b/New_APIs/College_Lectures_API/src/lect.js @@ -5,29 +5,29 @@ function searchLecture() { return; } fetch(`http://localhost:3000/lectures?title=${lectureName}`) - .then(response => { - if (!response.ok) { - throw new Error('Lecture not found'); - } - return response.json(); - }) - .then(lectures => { - const foundLecture = lectures.find(lecture => lecture.title.toLowerCase() === lectureName.toLowerCase()); - if (!foundLecture) { - throw new Error('Lecture not found'); - } - const lectureContent = ` -

Lecture Information

-

Title: ${foundLecture.title}

-

Instructor: ${foundLecture.instructor}

-

Time: ${foundLecture.time}

-

Room: ${foundLecture.room}

- `; - document.getElementById('lectureContent').innerHTML = lectureContent; - }) - .catch(error => { - document.getElementById('lectureContent').innerHTML = ` -

${error.message}

- `; - }); + .then(response => { + if (!response.ok) { + throw new Error('Lecture not found'); + } + return response.json(); + }) + .then(lectures => { + const foundLecture = lectures.find(lecture => lecture.title.toLowerCase() === lectureName.toLowerCase()); + if (!foundLecture) { + throw new Error('Lecture not found'); + } + const lectureContent = ` +

Lecture Information

+

Title: ${foundLecture.title}

+

Instructor: ${foundLecture.instructor}

+

Time: ${foundLecture.time}

+

Room: ${foundLecture.room}

+ `; + document.getElementById('lectureContent').innerHTML = lectureContent; + }) + .catch(error => { + document.getElementById('lectureContent').innerHTML = ` +

${error.message}

+ `; + }); } \ No newline at end of file