Code cleanup
This commit is contained in:
@@ -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
|
||||
|
||||
+27
-28
@@ -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,
|
||||
newMsg,
|
||||
deleteAllMsg,
|
||||
getOneMsg,
|
||||
newMsg,
|
||||
updateMsg,
|
||||
deleteOneMsg
|
||||
deleteOneMsg,
|
||||
deleteAllMsg
|
||||
};
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
const mongoose = require("mongoose"); //import mongoose
|
||||
const mongoose = require("mongoose");
|
||||
|
||||
// msg schema
|
||||
const MsgSchema = new mongoose.Schema({
|
||||
|
||||
+1
-1
@@ -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,
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
+21
-21
@@ -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')
|
||||
|
||||
Reference in New Issue
Block a user