From 149a25c251ac5c1855471f31e2de984db238465a Mon Sep 17 00:00:00 2001 From: Gregory Campbell Date: Thu, 25 Nov 2021 22:02:18 -0500 Subject: [PATCH] Added and updated comments throughout entire project --- controllers/msg.js | 35 +++++++++++++++++++++-------------- models/msg.js | 2 +- mongoConfig.js | 5 +++-- routes/msg.js | 3 ++- server.js | 13 +++++++------ test/msg.js | 28 ++++++++++++++++++++++++---- 6 files changed, 58 insertions(+), 28 deletions(-) diff --git a/controllers/msg.js b/controllers/msg.js index 022df84..38fec10 100644 --- a/controllers/msg.js +++ b/controllers/msg.js @@ -2,6 +2,9 @@ const Msg = require('../models/msg'); //import msg model //GET all messages const getAllMsg = (req, res) => { + + // Get info for the database and returns json data along with a 200 status code + // or returns the error json with 404 Msg.find({}, (err, data)=>{ if (err){ return res.status(404).json({Error: err}); @@ -13,7 +16,9 @@ const getAllMsg = (req, res) => { //GET message based on id const getOneMsg = (req, res) => { - //find the specific msg with that id + // Finds the id within the database and returns json data along with a 200 status code + // returns the error json with 404 if an error is returned + // or it is assumed the message doesnt exist so a 400 message is returned instead Msg.findOne({id:req.params.id}, (err, data) => { if(data) { return res.status(200).json(data); @@ -32,7 +37,7 @@ const newMsg = (req, res) => { //if this message is not in db, add it if (!data) { - //create a new msg object using the Msg model and req.params + //create a new Msg object using the Msg model, req.params and req.body const newMsg = new Msg({ id:req.params.id, message:req.body.message, @@ -44,7 +49,7 @@ const newMsg = (req, res) => { if(err) return res.status(404).json({Error: err}); return res.status(201).json(data); }) - //if there's an error or the msg is in db, return an error message + //if there's an error or the message is already in db, return an error message }else{ if(err) return res.status(404).json(`Something went wrong, please try again. ${err}`); return res.status(400).json({message:"Message can't be posted. A message with that id already exists."}); @@ -55,13 +60,12 @@ const newMsg = (req, res) => { //PUT message based on id const updateMsg = (req, res) => { - //check if the id already exists in db + // Finds the id within the database and updates json data along with a 200 status code + // returns the error json with 404 if an error is returned + // or it is assumed the message doesnt exist so a 400 message is returned instead Msg.findOneAndUpdate({id:req.params.id}, {message:req.body.message}, {new: true}, (err, data) => { - - //if this message is in db, update it if (data) { - return res.status(201).json(data); - //if there's an error or the msg is in db, return an error message + return res.status(201).json(data); }else{ if(err) return res.status(404).json(`Something went wrong, please try again. ${err}`); return res.status(400).json({message:"Message can't be updated, it doesn't exist."}); @@ -71,6 +75,9 @@ const updateMsg = (req, res) => { //DELETE all messages const deleteAllMsg = (req, res) => { + + // Deletes all info from database and returns a json message and a 200 status code + // or returns the error json with 404 Msg.deleteMany({}, err => { if(err) { return res.status(404).json({message: "Deletion of all messages failed"}); @@ -82,13 +89,13 @@ const deleteAllMsg = (req, res) => { //DELETE message based on id const deleteOneMsg = (req, res) => { + // Finds the id within the database and removes json data along with a 200 status code + // returns the error json with 404 if an error is returned + // or it is assumed the message doesnt exist so a 400 message is returned instead Msg.deleteOne({id:req.params.id}, (err, data) => { - //if there's nothing to delete return a message - if (err) return res.status(404).json(`Something went wrong, please try again. ${err}`); - //else if there's an error, return the err message - else if( data.deletedCount == 0) return res.status(400).json({message: "Message can't be deleted, it doesn't exist."}); - //else, return the success message - else return res.status(200).json({message: "Message deleted."}); + if (err) return res.status(404).json(`Something went wrong, please try again. ${err}`); + else if( data.deletedCount == 0) return res.status(400).json({message: "Message can't be deleted, it doesn't exist."}); + else return res.status(200).json({message: "Message deleted."}); }); }; diff --git a/models/msg.js b/models/msg.js index 207bfe2..6ef6595 100644 --- a/models/msg.js +++ b/models/msg.js @@ -1,4 +1,4 @@ -const mongoose = require("mongoose"); +const mongoose = require("mongoose"); //import mongoose // msg schema const MsgSchema = new mongoose.Schema({ diff --git a/mongoConfig.js b/mongoConfig.js index 3bfb0cd..f539cd1 100644 --- a/mongoConfig.js +++ b/mongoConfig.js @@ -1,7 +1,8 @@ -require('dotenv').config({ path: 'development.env' }); +require('dotenv').config({ path: 'development.env' }); // Grabs MONGODB URI -const mongoose = require('mongoose'); +const mongoose = require('mongoose'); //import mongoose +//Connects to the mongo databse attached to heroku app mongoose.connect( process.env.MONGODB_URI, (err) => { diff --git a/routes/msg.js b/routes/msg.js index 0b0e04a..bb292c9 100644 --- a/routes/msg.js +++ b/routes/msg.js @@ -1,11 +1,12 @@ const express = require('express'); //import express -const multer = require('multer'); +const multer = require('multer'); //import multer const upload = multer(); const router = express.Router(); const msgController = require('../controllers/msg'); +//Routes for all the desires calls and connected to their respective functions router.post('/msg', upload.none(), msgController.newMsg); router.get('/msg', msgController.getAllMsg); diff --git a/server.js b/server.js index f6c10d7..d871c23 100644 --- a/server.js +++ b/server.js @@ -1,14 +1,14 @@ -require("./mongoConfig") +require("./mongoConfig") //take mongoose info from config file -const express = require('express'); +const express = require('express'); //import express const routes = require('./routes/msg'); //import the routes -const helmet = require('helmet'); -const compression = require('compression'); -const morgan = require('morgan') +const helmet = require('helmet'); //import helmet +const compression = require('compression'); //import compression +const morgan = require('morgan') //import morgan const app = express(); -app.use(helmet()); +app.use(helmet()); //add json security app.use(compression()); //compress all routes app.use(express.json()); //parses incoming requests with JSON payloads app.use(morgan('combined')) //prints logging when requests are made @@ -21,6 +21,7 @@ app.route("/") res.sendFile(process.cwd() + '/index.html'); }); +//listens to port 3000 (or whichever port heroku chooses) const listener = app.listen(process.env.PORT || 3000, () => { console.log('App is listening on port ' + listener.address().port) }) diff --git a/test/msg.js b/test/msg.js index 65bd529..a35b85b 100644 --- a/test/msg.js +++ b/test/msg.js @@ -10,13 +10,14 @@ app.use(express.urlencoded({ extended: false })) app.use("/", routes) /** - * Testing post message logic + * Testing post message logic and response status codes */ describe('POST /msg/:id', function () { const data = { message: "test", } + // Send basic message with id 1 it('respond with 201 created', function (done) { request(app) .post('/msg/1') @@ -27,6 +28,7 @@ describe('POST /msg/:id', function () { .expect(201, done); }); + // Tries to send a message that already exists it('respond with 400 not posted', function (done) { request(app) .post('/msg/1') @@ -39,6 +41,7 @@ describe('POST /msg/:id', function () { }); }); + // Tries to send a message with an id that won't work it('respond with 404 error', function (done) { request(app) .post('/msg/idisnonexisting') @@ -53,9 +56,11 @@ describe('POST /msg/:id', function () { }); /** - * Testing get message logic + * Testing get message logic and response status codes */ describe('GET /msg', function () { + + // Get info of entire database it('respond with 200 info recieved', function (done) { request(app) .get('/msg') @@ -66,6 +71,8 @@ describe('GET /msg', function () { }); describe('GET /msg/:id', function () { + + //Get info of a message with id 1 it('respond with 200 info recieved for one message', function (done) { request(app) .get('/msg/1') @@ -74,6 +81,7 @@ describe('GET /msg/:id', function () { .expect(200, done); }); + // Get info of a message that doesnt exist it('respond with 400 message not found', function (done) { request(app) .get('/msg/2') @@ -86,6 +94,7 @@ describe('GET /msg/:id', function () { }); }); + // Get info of a message with an id that won't work it('respond with 404 error', function (done) { request(app) .get('/msg/idisnonexisting') @@ -100,9 +109,11 @@ describe('GET /msg/:id', function () { }); /** - * Testing put message logic + * Testing update message logic and response status codes */ describe('PUT /msg/:id', function () { + + // Update the info of a message with id 1 it('respond with 201 message updated', function (done) { request(app) .put('/msg/1') @@ -111,6 +122,7 @@ describe('PUT /msg/:id', function () { .expect(201, done); }); + // Update the info of a message that doesn't exist it('respond with 400 message not found', function (done) { request(app) .put('/msg/2') @@ -123,6 +135,8 @@ describe('PUT /msg/:id', function () { }); }); + + // Update the info of a message with an id that won't work it('respond with 404 error', function (done) { request(app) .put('/msg/idisnonexisting') @@ -137,9 +151,11 @@ describe('PUT /msg/:id', function () { }); /** - * Testing delete message logic + * Testing delete message logic and response status codes */ describe('DELETE /msg/:id', function () { + + // Delete a message with id of 1 it('respond with 200 single message deleted', function (done) { request(app) .delete('/msg/1') @@ -148,6 +164,7 @@ describe('DELETE /msg/:id', function () { .expect(200, done); }); + // Delete a message that doesn't exist it('respond with 400 message not found', function (done) { request(app) .delete('/msg/1') @@ -160,6 +177,7 @@ describe('DELETE /msg/:id', function () { }); }); + // Delete a message with an id that won't work it('respond with 404 error', function (done) { request(app) .delete('/msg/idisnonexisting') @@ -174,6 +192,8 @@ describe('DELETE /msg/:id', function () { }); describe('DELETE /msg', function () { + + // Delete all messages within database it('respond with 200 all messages deleted', function (done) { request(app) .delete('/msg')