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
+21 -14
View File
@@ -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."});
});
};