Skip to content

Commit a668acc

Browse files
committed
update api also done
1 parent 363a7e8 commit a668acc

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

CRUD API's/app/controllers/controller.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ const Note=require('../models/model');
22

33
// create and save a new note
44
exports.create=function(req,res){
5+
6+
if(!req.body.title||!req.body.author||!req.body.content) {
7+
return res.status(400).send({
8+
message: "Every field is required"
9+
});
10+
}
11+
512
const newnote=Note({
613
title: req.body.title,
714
author: req.body.author,
@@ -75,6 +82,32 @@ exports.findbytitle=function(req,res){
7582
//update any note with given id
7683
exports.update=function(req,res){
7784

85+
if(!req.body.content){
86+
return res.status(400).send({message :"Note content can not be empty"})
87+
}
88+
89+
Note.findByIdAndUpdate(req.params.noteId,{
90+
title : req.body.title,
91+
author : req.body.author,
92+
content : req.body.content
93+
},{new: true})
94+
.then(note=>{
95+
if(!note){
96+
return res.status(400).send({message: "note not found with id "+req.params.noteId});
97+
}
98+
res.send(note);
99+
}).catch(err => {
100+
if(err.kind === 'ObjectId') {
101+
return res.status(404).send({
102+
message: "Note not found with id " + req.params.noteId
103+
});
104+
}
105+
return res.status(500).send({
106+
message: "Error updating note with id " + req.params.noteId
107+
});
108+
});
109+
110+
78111
};
79112

80113
//to delete any note by id

0 commit comments

Comments
 (0)