From b0d449d01270705db7f13aef451b520fd9873882 Mon Sep 17 00:00:00 2001 From: Gregory Campbell Date: Thu, 25 Nov 2021 21:34:43 -0500 Subject: [PATCH] Code cleanup --- README.md | 2 +- controllers/msg.js | 55 +++++++++++++++++++++++----------------------- models/msg.js | 2 +- mongoConfig.js | 2 +- server.js | 11 +++++----- test/msg.js | 42 +++++++++++++++++------------------ 6 files changed, 56 insertions(+), 58 deletions(-) diff --git a/README.md b/README.md index e25e48a..1137aac 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ time: {type:Date, required:true}` - [Express](https://expressjs.com/) - [Mongodb](https://www.mongodb.com) - [Mongoose](https://mongoosejs.com/) -- [Morgan](https://github.com/expressjs/morgan), [Helmet](https://github.com/helmetjs/helmet) +- [Morgan](https://github.com/expressjs/morgan), [Helmet](https://github.com/helmetjs/helmet), [Compression](https://www.npmjs.com/package/compression) - [Mocha](https://mochajs.org/#getting-started), [Chai](http://chaijs.com/api/), [Supertest](https://github.com/visionmedia/supertest) ## How to diff --git a/controllers/msg.js b/controllers/msg.js index 8fc0261..3e03a81 100644 --- a/controllers/msg.js +++ b/controllers/msg.js @@ -1,6 +1,5 @@ const Msg = require('../models/msg'); //import msg model - //GET all messages const getAllMsg = (req, res) => { Msg.find({}, (err, data)=>{ @@ -11,6 +10,20 @@ const getAllMsg = (req, res) => { }) }; +//GET message based on id +const getOneMsg = (req, res) => { + + //find the specific msg with that id + Msg.findOne({id:req.params.id}, (err, data) => { + if(data) { + return res.status(200).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 found, it doesn't exist."}); + } + }); +}; + //POST message const newMsg = (req, res) => { @@ -39,30 +52,6 @@ const newMsg = (req, res) => { }) }; -//DELETE all messages -const deleteAllMsg = (req, res) => { - Msg.deleteMany({}, err => { - if(err) { - return res.status(404).json({message: "Deletion of all messages failed"}); - } - return res.status(200).json({message: "Deletion of all messages successful"}); - }) -}; - -//GET message based on id -const getOneMsg = (req, res) => { - - //find the specific msg with that id - Msg.findOne({id:req.params.id}, (err, data) => { - if(data) { - return res.status(200).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 found, it doesn't exist."}); - } - }); -}; - //PUT message based on id const updateMsg = (req, res) => { @@ -80,6 +69,16 @@ const updateMsg = (req, res) => { }) }; +//DELETE all messages +const deleteAllMsg = (req, res) => { + Msg.deleteMany({}, err => { + if(err) { + return res.status(404).json({message: "Deletion of all messages failed"}); + } + return res.status(200).json({message: "Deletion of all messages successful"}); + }) +}; + //DELETE message based on id const deleteOneMsg = (req, res) => { @@ -96,9 +95,9 @@ const deleteOneMsg = (req, res) => { //export controller functions module.exports = { getAllMsg, + getOneMsg, newMsg, - deleteAllMsg, - getOneMsg, updateMsg, - deleteOneMsg + deleteOneMsg, + deleteAllMsg }; \ No newline at end of file diff --git a/models/msg.js b/models/msg.js index 6ef6595..207bfe2 100644 --- a/models/msg.js +++ b/models/msg.js @@ -1,4 +1,4 @@ -const mongoose = require("mongoose"); //import mongoose +const mongoose = require("mongoose"); // msg schema const MsgSchema = new mongoose.Schema({ diff --git a/mongoConfig.js b/mongoConfig.js index e561171..3bfb0cd 100644 --- a/mongoConfig.js +++ b/mongoConfig.js @@ -1,6 +1,6 @@ require('dotenv').config({ path: 'development.env' }); -const mongoose = require('mongoose'); //import mongoose +const mongoose = require('mongoose'); mongoose.connect( process.env.MONGODB_URI, diff --git a/server.js b/server.js index 018abff..f6c10d7 100644 --- a/server.js +++ b/server.js @@ -1,16 +1,15 @@ require("./mongoConfig") + const express = require('express'); const routes = require('./routes/msg'); //import the routes - -const helmet = require('helmet'); //import helmet -const compression = require('compression'); //import compression -const morgan = require('morgan') //import morgan +const helmet = require('helmet'); +const compression = require('compression'); +const morgan = require('morgan') const app = express(); + app.use(helmet()); 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 diff --git a/test/msg.js b/test/msg.js index d1040c9..aadf862 100644 --- a/test/msg.js +++ b/test/msg.js @@ -1,9 +1,9 @@ require("../mongoConfig") const routes = require('../routes/msg') - const request = require('supertest'); const express = require('express'); + const app = express(); app.use(express.urlencoded({ extended: false })) @@ -27,7 +27,7 @@ describe('POST /msg/:id', function () { .expect(201, done); }); - it('respond with 400 not updated', function (done) { + it('respond with 400 not posted', function (done) { request(app) .post('/msg/1') .set('Accept', 'application/json') @@ -39,7 +39,7 @@ describe('POST /msg/:id', function () { }); }); - it('respond with 400 not updated', function (done) { + it('respond with 404 error', function (done) { request(app) .post('/msg/idisnonexisting') .set('Accept', 'application/json') @@ -56,7 +56,7 @@ describe('POST /msg/:id', function () { * Testing get message logic */ describe('GET /msg', function () { - it('respond with json containing a list of all users', function (done) { + it('respond with 200 info recieved', function (done) { request(app) .get('/msg') .set('Accept', 'application/json') @@ -66,7 +66,7 @@ describe('GET /msg', function () { }); describe('GET /msg/:id', function () { - it('respond with json containing a single message', function (done) { + it('respond with 200 info recieved for one message', function (done) { request(app) .get('/msg/1') .set('Accept', 'application/json') @@ -74,24 +74,24 @@ describe('GET /msg/:id', function () { .expect(200, done); }); - it('respond with json message not found', function (done) { + it('respond with 400 message not found', function (done) { request(app) .get('/msg/2') .set('Accept', 'application/json') - .expect(400) //expecting HTTP status code - .expect('Content-Type', /json/) // expecting content value + .expect(400) + .expect('Content-Type', /json/) .end((err) => { if (err) return done(err); done(); }); }); - it('respond with json message not found', function (done) { + it('respond with 404 error', function (done) { request(app) .get('/msg/idisnonexisting') .set('Accept', 'application/json') - .expect(404) //expecting HTTP status code - .expect('Content-Type', /json/) // expecting content value + .expect(404) + .expect('Content-Type', /json/) .end((err) => { if (err) return done(err); done(); @@ -103,7 +103,7 @@ describe('GET /msg/:id', function () { * Testing put message logic */ describe('PUT /msg/:id', function () { - it('respond with json containing a single message', function (done) { + it('respond with 201 message updated', function (done) { request(app) .put('/msg/1') .set('Accept', 'application/json') @@ -111,24 +111,24 @@ describe('PUT /msg/:id', function () { .expect(201, done); }); - it('respond with json message not found', function (done) { + it('respond with 400 message not found', function (done) { request(app) .put('/msg/2') .set('Accept', 'application/json') - .expect(400) //expecting HTTP status code - .expect('Content-Type', /json/) // expecting content value + .expect(400) + .expect('Content-Type', /json/) .end((err) => { if (err) return done(err); done(); }); }); - it('respond with json message not found', function (done) { + it('respond with 404 error', function (done) { request(app) .put('/msg/idisnonexisting') .set('Accept', 'application/json') - .expect(404) //expecting HTTP status code - .expect('Content-Type', /json/) // expecting content value + .expect(404) + .expect('Content-Type', /json/) .end((err) => { if (err) return done(err); done(); @@ -140,7 +140,7 @@ describe('PUT /msg/:id', function () { * Testing delete message logic */ describe('DELETE /msg/:id', function () { - it('respond with json containing a single message', function (done) { + it('respond with 200 single message deleted', function (done) { request(app) .delete('/msg/1') .set('Accept', 'application/json') @@ -148,7 +148,7 @@ describe('DELETE /msg/:id', function () { .expect(200, done); }); - it('respond with json message not found', function (done) { + it('respond with 400 message not found', function (done) { request(app) .delete('/msg/1') .set('Accept', 'application/json') @@ -162,7 +162,7 @@ describe('DELETE /msg/:id', function () { }); describe('DELETE /msg', function () { - it('respond with json containing a list of all users', function (done) { + it('respond with 200 all messages deleted', function (done) { request(app) .delete('/msg') .set('Accept', 'application/json')