Added and updated comments throughout entire project

This commit is contained in:
Gregory Campbell
2021-11-25 22:02:18 -05:00
parent 1776e011f2
commit 149a25c251
6 changed files with 58 additions and 28 deletions
+20 -13
View File
@@ -2,6 +2,9 @@ const Msg = require('../models/msg'); //import msg model
//GET all messages //GET all messages
const getAllMsg = (req, res) => { 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)=>{ Msg.find({}, (err, data)=>{
if (err){ if (err){
return res.status(404).json({Error: err}); return res.status(404).json({Error: err});
@@ -13,7 +16,9 @@ const getAllMsg = (req, res) => {
//GET message based on id //GET message based on id
const getOneMsg = (req, res) => { 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) => { Msg.findOne({id:req.params.id}, (err, data) => {
if(data) { if(data) {
return res.status(200).json(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 this message is not in db, add it
if (!data) { 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({ const newMsg = new Msg({
id:req.params.id, id:req.params.id,
message:req.body.message, message:req.body.message,
@@ -44,7 +49,7 @@ const newMsg = (req, res) => {
if(err) return res.status(404).json({Error: err}); if(err) return res.status(404).json({Error: err});
return res.status(201).json(data); 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{ }else{
if(err) return res.status(404).json(`Something went wrong, please try again. ${err}`); 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."}); 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 //PUT message based on id
const updateMsg = (req, res) => { 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) => { Msg.findOneAndUpdate({id:req.params.id}, {message:req.body.message}, {new: true}, (err, data) => {
//if this message is in db, update it
if (data) { if (data) {
return res.status(201).json(data); return res.status(201).json(data);
//if there's an error or the msg is in db, return an error message
}else{ }else{
if(err) return res.status(404).json(`Something went wrong, please try again. ${err}`); 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."}); 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 //DELETE all messages
const deleteAllMsg = (req, res) => { 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 => { Msg.deleteMany({}, err => {
if(err) { if(err) {
return res.status(404).json({message: "Deletion of all messages failed"}); return res.status(404).json({message: "Deletion of all messages failed"});
@@ -82,13 +89,13 @@ const deleteAllMsg = (req, res) => {
//DELETE message based on id //DELETE message based on id
const deleteOneMsg = (req, res) => { 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) => { 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}`);
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 if there's an error, return the err message else return res.status(200).json({message: "Message deleted."});
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."});
}); });
}; };
+1 -1
View File
@@ -1,4 +1,4 @@
const mongoose = require("mongoose"); const mongoose = require("mongoose"); //import mongoose
// msg schema // msg schema
const MsgSchema = new mongoose.Schema({ const MsgSchema = new mongoose.Schema({
+3 -2
View File
@@ -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( mongoose.connect(
process.env.MONGODB_URI, process.env.MONGODB_URI,
(err) => { (err) => {
+2 -1
View File
@@ -1,11 +1,12 @@
const express = require('express'); //import express const express = require('express'); //import express
const multer = require('multer'); const multer = require('multer'); //import multer
const upload = multer(); const upload = multer();
const router = express.Router(); const router = express.Router();
const msgController = require('../controllers/msg'); 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.post('/msg', upload.none(), msgController.newMsg);
router.get('/msg', msgController.getAllMsg); router.get('/msg', msgController.getAllMsg);
+7 -6
View File
@@ -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 routes = require('./routes/msg'); //import the routes
const helmet = require('helmet'); const helmet = require('helmet'); //import helmet
const compression = require('compression'); const compression = require('compression'); //import compression
const morgan = require('morgan') const morgan = require('morgan') //import morgan
const app = express(); const app = express();
app.use(helmet()); app.use(helmet()); //add json security
app.use(compression()); //compress all routes app.use(compression()); //compress all routes
app.use(express.json()); //parses incoming requests with JSON payloads app.use(express.json()); //parses incoming requests with JSON payloads
app.use(morgan('combined')) //prints logging when requests are made app.use(morgan('combined')) //prints logging when requests are made
@@ -21,6 +21,7 @@ app.route("/")
res.sendFile(process.cwd() + '/index.html'); res.sendFile(process.cwd() + '/index.html');
}); });
//listens to port 3000 (or whichever port heroku chooses)
const listener = app.listen(process.env.PORT || 3000, () => { const listener = app.listen(process.env.PORT || 3000, () => {
console.log('App is listening on port ' + listener.address().port) console.log('App is listening on port ' + listener.address().port)
}) })
+24 -4
View File
@@ -10,13 +10,14 @@ app.use(express.urlencoded({ extended: false }))
app.use("/", routes) app.use("/", routes)
/** /**
* Testing post message logic * Testing post message logic and response status codes
*/ */
describe('POST /msg/:id', function () { describe('POST /msg/:id', function () {
const data = { const data = {
message: "test", message: "test",
} }
// Send basic message with id 1
it('respond with 201 created', function (done) { it('respond with 201 created', function (done) {
request(app) request(app)
.post('/msg/1') .post('/msg/1')
@@ -27,6 +28,7 @@ describe('POST /msg/:id', function () {
.expect(201, done); .expect(201, done);
}); });
// Tries to send a message that already exists
it('respond with 400 not posted', function (done) { it('respond with 400 not posted', function (done) {
request(app) request(app)
.post('/msg/1') .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) { it('respond with 404 error', function (done) {
request(app) request(app)
.post('/msg/idisnonexisting') .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 () { describe('GET /msg', function () {
// Get info of entire database
it('respond with 200 info recieved', function (done) { it('respond with 200 info recieved', function (done) {
request(app) request(app)
.get('/msg') .get('/msg')
@@ -66,6 +71,8 @@ describe('GET /msg', function () {
}); });
describe('GET /msg/:id', 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) { it('respond with 200 info recieved for one message', function (done) {
request(app) request(app)
.get('/msg/1') .get('/msg/1')
@@ -74,6 +81,7 @@ describe('GET /msg/:id', function () {
.expect(200, done); .expect(200, done);
}); });
// Get info of a message that doesnt exist
it('respond with 400 message not found', function (done) { it('respond with 400 message not found', function (done) {
request(app) request(app)
.get('/msg/2') .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) { it('respond with 404 error', function (done) {
request(app) request(app)
.get('/msg/idisnonexisting') .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 () { describe('PUT /msg/:id', function () {
// Update the info of a message with id 1
it('respond with 201 message updated', function (done) { it('respond with 201 message updated', function (done) {
request(app) request(app)
.put('/msg/1') .put('/msg/1')
@@ -111,6 +122,7 @@ describe('PUT /msg/:id', function () {
.expect(201, done); .expect(201, done);
}); });
// Update the info of a message that doesn't exist
it('respond with 400 message not found', function (done) { it('respond with 400 message not found', function (done) {
request(app) request(app)
.put('/msg/2') .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) { it('respond with 404 error', function (done) {
request(app) request(app)
.put('/msg/idisnonexisting') .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 () { describe('DELETE /msg/:id', function () {
// Delete a message with id of 1
it('respond with 200 single message deleted', function (done) { it('respond with 200 single message deleted', function (done) {
request(app) request(app)
.delete('/msg/1') .delete('/msg/1')
@@ -148,6 +164,7 @@ describe('DELETE /msg/:id', function () {
.expect(200, done); .expect(200, done);
}); });
// Delete a message that doesn't exist
it('respond with 400 message not found', function (done) { it('respond with 400 message not found', function (done) {
request(app) request(app)
.delete('/msg/1') .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) { it('respond with 404 error', function (done) {
request(app) request(app)
.delete('/msg/idisnonexisting') .delete('/msg/idisnonexisting')
@@ -174,6 +192,8 @@ describe('DELETE /msg/:id', function () {
}); });
describe('DELETE /msg', function () { describe('DELETE /msg', function () {
// Delete all messages within database
it('respond with 200 all messages deleted', function (done) { it('respond with 200 all messages deleted', function (done) {
request(app) request(app)
.delete('/msg') .delete('/msg')