Added HTTP codes to all API responses

This commit is contained in:
Gregory Campbell
2021-11-25 13:56:55 -05:00
parent c4c658ea7b
commit 1933391ed3
+16 -16
View File
@@ -5,9 +5,9 @@ const Msg = require('../models/msg'); //import msg model
const getAllMsg = (req, res) => { const getAllMsg = (req, res) => {
Msg.find({}, (err, data)=>{ Msg.find({}, (err, data)=>{
if (err){ if (err){
return res.json({Error: err}); return res.status(404).json({Error: err});
} }
return res.json(data); return res.status(200).json(data);
}) })
}; };
@@ -28,13 +28,13 @@ const newMsg = (req, res) => {
// save this object to database // save this object to database
newMsg.save((err, data)=>{ newMsg.save((err, data)=>{
if(err) return res.json({Error: err}); if(err) return res.status(404).json({Error: err});
return res.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 msg is in db, return an error message
}else{ }else{
if(err) return res.json(`Something went wrong, please try again. ${err}`); if(err) return res.status(404).json(`Something went wrong, please try again. ${err}`);
return res.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."});
} }
}) })
}; };
@@ -43,9 +43,9 @@ const newMsg = (req, res) => {
const deleteAllMsg = (req, res) => { const deleteAllMsg = (req, res) => {
Msg.deleteMany({}, err => { Msg.deleteMany({}, err => {
if(err) { if(err) {
return res.json({message: "Deletion of all messages failed"}); return res.status(404).json({message: "Deletion of all messages failed"});
} }
return res.json({message: "Deletion of all messages successful"}); return res.status(200).json({message: "Deletion of all messages successful"});
}) })
}; };
@@ -55,9 +55,9 @@ const getOneMsg = (req, res) => {
//find the specific msg with that id //find the specific msg with that id
Msg.findOne({id:req.params.id}, (err, data) => { Msg.findOne({id:req.params.id}, (err, data) => {
if(err || !data) { if(err || !data) {
return res.json({message: "Message can't be found, it doesn't exist."}); return res.status(400).json({message: "Message can't be found, it doesn't exist."});
} }
else return res.json(data); //return the msg object if found else return res.status(200).json(data); //return the msg object if found
}); });
}; };
@@ -69,11 +69,11 @@ const updateMsg = (req, res) => {
//if this message is in db, update it //if this message is in db, update it
if (data) { if (data) {
return res.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 msg is in db, return an error message
}else{ }else{
if(err) return res.json(`Something went wrong, please try again. ${err}`); if(err) return res.status(404).json(`Something went wrong, please try again. ${err}`);
return res.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."});
} }
}) })
}; };
@@ -83,11 +83,11 @@ const deleteOneMsg = (req, res) => {
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 there's nothing to delete return a message
if( data.deletedCount == 0) return res.json({message: "Message can't be deleted, it doesn't exist."}); 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 if there's an error, return the err message
else if (err) return res.json(`Something went wrong, please try again. ${err}`); else if (err) return res.status(404).json(`Something went wrong, please try again. ${err}`);
//else, return the success message //else, return the success message
else return res.json({message: "Message deleted."}); else return res.status(200).json({message: "Message deleted."});
}); });
}; };