From b493a70146c25a86ad6bbcf8b591c0d8a9f82cd6 Mon Sep 17 00:00:00 2001 From: Jahzara Broaster Date: Sun, 28 Sep 2025 22:45:54 -0500 Subject: [PATCH] check --- app.js | 22 ++++ config/db.js | 19 ++++ controllers/messageController.js | 163 ++++++++--------------------- package-lock.json | 171 ++++++++++++++++++++++++------- package.json | 9 +- 5 files changed, 222 insertions(+), 162 deletions(-) create mode 100644 app.js create mode 100644 config/db.js diff --git a/app.js b/app.js new file mode 100644 index 0000000..7f16b1f --- /dev/null +++ b/app.js @@ -0,0 +1,22 @@ +// filepath: /Users/Jahzara/Documents/CSC424 work/backend api/Backend-Example/app.js +const express = require('express'); +const dotenv = require('dotenv'); +const connectDB = require('./config/db'); +const messageRoutes = require('./routes/messageRoutes'); + +dotenv.config(); +connectDB(); + +const app = express(); + +app.use(express.json()); +app.use('/api', messageRoutes); + +app.get('/', (req, res) => { + res.send('API is running'); +}); + +const PORT = process.env.PORT || 3000; +app.listen(PORT, () => { + console.log(`Server running on port ${PORT}`); +}); \ No newline at end of file diff --git a/config/db.js b/config/db.js new file mode 100644 index 0000000..4c51418 --- /dev/null +++ b/config/db.js @@ -0,0 +1,19 @@ +const mongoose = require('mongoose'); +const dotenv = require('dotenv'); + +dotenv.config(); + +const connectDB = async () => { + try { + await mongoose.connect(process.env.MONGODB_URI, { + useNewUrlParser: true, + useUnifiedTopology: true + }); + console.log('MongoDB connected'); + } catch (err) { + console.error('MongoDB connection error:', err.message); + process.exit(1); + } +}; + +module.exports = connectDB; \ No newline at end of file diff --git a/controllers/messageController.js b/controllers/messageController.js index fb85906..9b934a4 100644 --- a/controllers/messageController.js +++ b/controllers/messageController.js @@ -15,144 +15,65 @@ const sendResponse = (res, statusCode, success, data = null, error = null) => { return res.status(statusCode).json(response); }; -// POST /api/messages → Create a new message -const createMessage = async (req, res) => { - try { - // For now, return placeholder response - // TODO: Implement database logic - - const sampleMessage = { - id: "placeholder-id-123", - author: req.body.author || "Sample Author", - text: req.body.text || "Sample message text", - timestamp: new Date().toISOString(), - isRead: false - }; +const Message = require('../models/Message'); - sendResponse(res, 201, true, { - message: "Message created successfully", - messageData: sampleMessage - }); - } catch (error) { - sendResponse(res, 500, false, null, "Failed to create message"); +// Create a new message +exports.createMessage = async (req, res) => { + try { + const { text, author } = req.body; + const message = new Message({ text, author }); + await message.save(); + res.status(201).json(message); + } catch (err) { + res.status(400).json({ error: err.message }); } }; -// GET /api/messages → Get all messages -const getAllMessages = async (req, res) => { +// Get all messages +exports.getAllMessages = async (req, res) => { try { - // For now, return placeholder response - // TODO: Implement database logic - - const sampleMessages = [ - { - id: "msg-001", - author: "John Doe", - text: "Hello, this is the first message!", - timestamp: new Date(Date.now() - 86400000).toISOString(), // 1 day ago - isRead: false - }, - { - id: "msg-002", - author: "Jane Smith", - text: "This is another sample message.", - timestamp: new Date(Date.now() - 43200000).toISOString(), // 12 hours ago - isRead: true - }, - { - id: "msg-003", - author: "Bob Johnson", - text: "Latest message in the system.", - timestamp: new Date().toISOString(), - isRead: false - } - ]; - - sendResponse(res, 200, true, { - messages: sampleMessages, - count: sampleMessages.length - }); - } catch (error) { - sendResponse(res, 500, false, null, "Failed to retrieve messages"); + const messages = await Message.find(); + res.json(messages); + } catch (err) { + res.status(500).json({ error: err.message }); } }; -// GET /api/messages/:id → Get a specific message by ID -const getMessageById = async (req, res) => { +// Get a message by ID +exports.getMessageById = async (req, res) => { try { - const { id } = req.params; - - // For now, return placeholder response - // TODO: Implement database logic - - if (!id) { - return sendResponse(res, 400, false, null, "Message ID is required"); - } - - const sampleMessage = { - id: id, - author: "Sample Author", - text: `This is a sample message with ID: ${id}`, - timestamp: new Date().toISOString(), - isRead: false - }; - - sendResponse(res, 200, true, { - message: sampleMessage - }); - } catch (error) { - sendResponse(res, 500, false, null, "Failed to retrieve message"); + const message = await Message.findById(req.params.id); + if (!message) return res.status(404).json({ error: 'Message not found' }); + res.json(message); + } catch (err) { + res.status(500).json({ error: err.message }); } }; -// PUT /api/messages/:id → Update a message by ID -const updateMessage = async (req, res) => { +// Update a message by ID +exports.updateMessage = async (req, res) => { try { - const { id } = req.params; - const updateData = req.body; - - // For now, return placeholder response - // TODO: Implement database logic - - if (!id) { - return sendResponse(res, 400, false, null, "Message ID is required"); - } - - const updatedMessage = { - id: id, - author: updateData.author || "Updated Author", - text: updateData.text || "Updated message text", - timestamp: new Date().toISOString(), - isRead: updateData.isRead !== undefined ? updateData.isRead : false - }; - - sendResponse(res, 200, true, { - message: "Message updated successfully", - messageData: updatedMessage - }); - } catch (error) { - sendResponse(res, 500, false, null, "Failed to update message"); + const { text, author } = req.body; + const message = await Message.findByIdAndUpdate( + req.params.id, + { text, author }, + { new: true } + ); + if (!message) return res.status(404).json({ error: 'Message not found' }); + res.json(message); + } catch (err) { + res.status(400).json({ error: err.message }); } }; -// DELETE /api/messages/:id → Delete a message by ID -const deleteMessage = async (req, res) => { +// Delete a message by ID +exports.deleteMessage = async (req, res) => { try { - const { id } = req.params; - - // For now, return placeholder response - // TODO: Implement database logic - - if (!id) { - return sendResponse(res, 400, false, null, "Message ID is required"); - } - - sendResponse(res, 200, true, { - message: `Message with ID ${id} deleted successfully`, - deletedId: id - }); - } catch (error) { - sendResponse(res, 500, false, null, "Failed to delete message"); + const message = await Message.findByIdAndDelete(req.params.id); + if (!message) return res.status(404).json({ error: 'Message not found' }); + res.json({ message: 'Message deleted' }); + } catch (err) { + res.status(500).json({ error: err.message }); } }; diff --git a/package-lock.json b/package-lock.json index 2503fc8..cbaf964 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "cors": "^2.8.5", "dotenv": "^16.3.1", "express": "^4.18.2", + "mongodb": "^6.20.0", "mongoose": "^7.5.0" }, "devDependencies": { @@ -23,7 +24,6 @@ "resolved": "https://registry.npmjs.org/@mongodb-js/saslprep/-/saslprep-1.3.0.tgz", "integrity": "sha512-zlayKCsIjYb7/IdfqxorK5+xUMyi4vOKcFy10wKJYc63NSdKI8mNME+uJqfatkPmOSMMUiojrL58IePKBm3gvQ==", "license": "MIT", - "optional": true, "dependencies": { "sparse-bitfield": "^3.0.3" } @@ -44,12 +44,11 @@ "license": "MIT" }, "node_modules/@types/whatwg-url": { - "version": "8.2.2", - "resolved": "https://registry.npmjs.org/@types/whatwg-url/-/whatwg-url-8.2.2.tgz", - "integrity": "sha512-FtQu10RWgn3D9U4aazdwIE2yzphmTJREDqNdODHrbrZmmMqI0vMheC/6NE/J1Yveaj8H+ela+YwWTjq5PGmuhA==", + "version": "11.0.5", + "resolved": "https://registry.npmjs.org/@types/whatwg-url/-/whatwg-url-11.0.5.tgz", + "integrity": "sha512-coYR071JRaHa+xoEvvYqvnIHaVqaYrLPbsufM9BF63HkwI5Lgmy2QR8Q5K/lYDYo5AK82wOvSOS0UsLTpTG7uQ==", "license": "MIT", "dependencies": { - "@types/node": "*", "@types/webidl-conversions": "*" } }, @@ -747,8 +746,7 @@ "version": "1.5.0", "resolved": "https://registry.npmjs.org/memory-pager/-/memory-pager-1.5.0.tgz", "integrity": "sha512-ZS4Bp4r/Zoeq6+NLJpP+0Zzm0pR8whtGPf1XExKLJBAczGMnSi3It14OiNCStjQjM6NU1okjQGSxgEZN8eBYKg==", - "license": "MIT", - "optional": true + "license": "MIT" }, "node_modules/merge-descriptors": { "version": "1.0.3", @@ -815,27 +813,26 @@ } }, "node_modules/mongodb": { - "version": "5.9.2", - "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-5.9.2.tgz", - "integrity": "sha512-H60HecKO4Bc+7dhOv4sJlgvenK4fQNqqUIlXxZYQNbfEWSALGAwGoyJd/0Qwk4TttFXUOHJ2ZJQe/52ScaUwtQ==", + "version": "6.20.0", + "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-6.20.0.tgz", + "integrity": "sha512-Tl6MEIU3K4Rq3TSHd+sZQqRBoGlFsOgNrH5ltAcFBV62Re3Fd+FcaVf8uSEQFOJ51SDowDVttBTONMfoYWrWlQ==", "license": "Apache-2.0", "dependencies": { - "bson": "^5.5.0", - "mongodb-connection-string-url": "^2.6.0", - "socks": "^2.7.1" + "@mongodb-js/saslprep": "^1.3.0", + "bson": "^6.10.4", + "mongodb-connection-string-url": "^3.0.2" }, "engines": { - "node": ">=14.20.1" - }, - "optionalDependencies": { - "@mongodb-js/saslprep": "^1.1.0" + "node": ">=16.20.1" }, "peerDependencies": { "@aws-sdk/credential-providers": "^3.188.0", - "@mongodb-js/zstd": "^1.0.0", - "kerberos": "^1.0.0 || ^2.0.0", - "mongodb-client-encryption": ">=2.3.0 <3", - "snappy": "^7.2.2" + "@mongodb-js/zstd": "^1.1.0 || ^2.0.0", + "gcp-metadata": "^5.2.0", + "kerberos": "^2.0.1", + "mongodb-client-encryption": ">=6.0.0 <7", + "snappy": "^7.3.2", + "socks": "^2.7.1" }, "peerDependenciesMeta": { "@aws-sdk/credential-providers": { @@ -844,6 +841,9 @@ "@mongodb-js/zstd": { "optional": true }, + "gcp-metadata": { + "optional": true + }, "kerberos": { "optional": true }, @@ -852,17 +852,29 @@ }, "snappy": { "optional": true + }, + "socks": { + "optional": true } } }, "node_modules/mongodb-connection-string-url": { - "version": "2.6.0", - "resolved": "https://registry.npmjs.org/mongodb-connection-string-url/-/mongodb-connection-string-url-2.6.0.tgz", - "integrity": "sha512-WvTZlI9ab0QYtTYnuMLgobULWhokRjtC7db9LtcVfJ+Hsnyr5eo6ZtNAt3Ly24XZScGMelOcGtm7lSn0332tPQ==", + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/mongodb-connection-string-url/-/mongodb-connection-string-url-3.0.2.tgz", + "integrity": "sha512-rMO7CGo/9BFwyZABcKAWL8UJwH/Kc2x0g72uhDWzG48URRax5TCIcJ7Rc3RZqffZzO/Gwff/jyKwCU9TN8gehA==", "license": "Apache-2.0", "dependencies": { - "@types/whatwg-url": "^8.2.1", - "whatwg-url": "^11.0.0" + "@types/whatwg-url": "^11.0.2", + "whatwg-url": "^14.1.0 || ^13.0.0" + } + }, + "node_modules/mongodb/node_modules/bson": { + "version": "6.10.4", + "resolved": "https://registry.npmjs.org/bson/-/bson-6.10.4.tgz", + "integrity": "sha512-WIsKqkSC0ABoBJuT1LEX+2HEvNmNKKgnTAyd0fL8qzK4SH2i9NXg+t08YtdZp/V9IZ33cxe3iV4yM0qg8lMQng==", + "license": "Apache-2.0", + "engines": { + "node": ">=16.20.1" } }, "node_modules/mongoose": { @@ -887,12 +899,98 @@ "url": "https://opencollective.com/mongoose" } }, + "node_modules/mongoose/node_modules/@types/whatwg-url": { + "version": "8.2.2", + "resolved": "https://registry.npmjs.org/@types/whatwg-url/-/whatwg-url-8.2.2.tgz", + "integrity": "sha512-FtQu10RWgn3D9U4aazdwIE2yzphmTJREDqNdODHrbrZmmMqI0vMheC/6NE/J1Yveaj8H+ela+YwWTjq5PGmuhA==", + "license": "MIT", + "dependencies": { + "@types/node": "*", + "@types/webidl-conversions": "*" + } + }, + "node_modules/mongoose/node_modules/mongodb": { + "version": "5.9.2", + "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-5.9.2.tgz", + "integrity": "sha512-H60HecKO4Bc+7dhOv4sJlgvenK4fQNqqUIlXxZYQNbfEWSALGAwGoyJd/0Qwk4TttFXUOHJ2ZJQe/52ScaUwtQ==", + "license": "Apache-2.0", + "dependencies": { + "bson": "^5.5.0", + "mongodb-connection-string-url": "^2.6.0", + "socks": "^2.7.1" + }, + "engines": { + "node": ">=14.20.1" + }, + "optionalDependencies": { + "@mongodb-js/saslprep": "^1.1.0" + }, + "peerDependencies": { + "@aws-sdk/credential-providers": "^3.188.0", + "@mongodb-js/zstd": "^1.0.0", + "kerberos": "^1.0.0 || ^2.0.0", + "mongodb-client-encryption": ">=2.3.0 <3", + "snappy": "^7.2.2" + }, + "peerDependenciesMeta": { + "@aws-sdk/credential-providers": { + "optional": true + }, + "@mongodb-js/zstd": { + "optional": true + }, + "kerberos": { + "optional": true + }, + "mongodb-client-encryption": { + "optional": true + }, + "snappy": { + "optional": true + } + } + }, + "node_modules/mongoose/node_modules/mongodb-connection-string-url": { + "version": "2.6.0", + "resolved": "https://registry.npmjs.org/mongodb-connection-string-url/-/mongodb-connection-string-url-2.6.0.tgz", + "integrity": "sha512-WvTZlI9ab0QYtTYnuMLgobULWhokRjtC7db9LtcVfJ+Hsnyr5eo6ZtNAt3Ly24XZScGMelOcGtm7lSn0332tPQ==", + "license": "Apache-2.0", + "dependencies": { + "@types/whatwg-url": "^8.2.1", + "whatwg-url": "^11.0.0" + } + }, "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==", "license": "MIT" }, + "node_modules/mongoose/node_modules/tr46": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/tr46/-/tr46-3.0.0.tgz", + "integrity": "sha512-l7FvfAHlcmulp8kr+flpQZmVwtu7nfRV7NZujtN0OqES8EL4O4e0qqzL0DC5gAvx/ZC/9lk6rhcUwYvkBnBnYA==", + "license": "MIT", + "dependencies": { + "punycode": "^2.1.1" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/mongoose/node_modules/whatwg-url": { + "version": "11.0.0", + "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-11.0.0.tgz", + "integrity": "sha512-RKT8HExMpoYx4igMiVMY83lN6UeITKJlBQ+vR/8ZJ8OCdSiN3RwCq+9gH0+Xzj0+5IrM6i4j/6LuvzbZIQgEcQ==", + "license": "MIT", + "dependencies": { + "tr46": "^3.0.0", + "webidl-conversions": "^7.0.0" + }, + "engines": { + "node": ">=12" + } + }, "node_modules/mpath": { "version": "0.9.0", "resolved": "https://registry.npmjs.org/mpath/-/mpath-0.9.0.tgz", @@ -1377,7 +1475,6 @@ "resolved": "https://registry.npmjs.org/sparse-bitfield/-/sparse-bitfield-3.0.3.tgz", "integrity": "sha512-kvzhi7vqKTfkh0PZU+2D2PIllw2ymqJKujUcyPMd9Y75Nv4nPbGJZXNhxsgdQab2BmlDct1YnfQCguEvHr7VsQ==", "license": "MIT", - "optional": true, "dependencies": { "memory-pager": "^1.0.2" } @@ -1437,15 +1534,15 @@ } }, "node_modules/tr46": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/tr46/-/tr46-3.0.0.tgz", - "integrity": "sha512-l7FvfAHlcmulp8kr+flpQZmVwtu7nfRV7NZujtN0OqES8EL4O4e0qqzL0DC5gAvx/ZC/9lk6rhcUwYvkBnBnYA==", + "version": "5.1.1", + "resolved": "https://registry.npmjs.org/tr46/-/tr46-5.1.1.tgz", + "integrity": "sha512-hdF5ZgjTqgAntKkklYw0R03MG2x/bSzTtkxmIRw/sTNV8YXsCJ1tfLAX23lhxhHJlEf3CRCOCGGWw3vI3GaSPw==", "license": "MIT", "dependencies": { - "punycode": "^2.1.1" + "punycode": "^2.3.1" }, "engines": { - "node": ">=12" + "node": ">=18" } }, "node_modules/type-is": { @@ -1511,16 +1608,16 @@ } }, "node_modules/whatwg-url": { - "version": "11.0.0", - "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-11.0.0.tgz", - "integrity": "sha512-RKT8HExMpoYx4igMiVMY83lN6UeITKJlBQ+vR/8ZJ8OCdSiN3RwCq+9gH0+Xzj0+5IrM6i4j/6LuvzbZIQgEcQ==", + "version": "14.2.0", + "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-14.2.0.tgz", + "integrity": "sha512-De72GdQZzNTUBBChsXueQUnPKDkg/5A5zp7pFDuQAj5UFoENpiACU0wlCvzpAGnTkj++ihpKwKyYewn/XNUbKw==", "license": "MIT", "dependencies": { - "tr46": "^3.0.0", + "tr46": "^5.1.0", "webidl-conversions": "^7.0.0" }, "engines": { - "node": ">=12" + "node": ">=18" } } } diff --git a/package.json b/package.json index 4eec488..25c8c6f 100644 --- a/package.json +++ b/package.json @@ -18,12 +18,13 @@ "author": "", "license": "MIT", "dependencies": { - "express": "^4.18.2", - "mongoose": "^7.5.0", "cors": "^2.8.5", - "dotenv": "^16.3.1" + "dotenv": "^16.3.1", + "express": "^4.18.2", + "mongodb": "^6.20.0", + "mongoose": "^7.5.0" }, "devDependencies": { "nodemon": "^3.0.1" } -} \ No newline at end of file +}