Skip to content

Commit 363a7e8

Browse files
committed
delete api done
1 parent 603252d commit 363a7e8

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

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

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ exports.findone=function(req,res){
5656
exports.findbyauthor=function(req,res){
5757
Note.find({author:req.params.author},function(err,note){
5858
if(note) return res.status(400).send(note);
59-
60-
res.status(400).send({message : "No author with given name has been found"});
61-
59+
else{
60+
return res.status(400).send({message : "No author with given name has been found"});
61+
}
6262
});
6363
};
6464

@@ -67,15 +67,32 @@ exports.findbytitle=function(req,res){
6767
Note.find({title:req.params.title},function(err,note){
6868
if(note) return res.status(400).send(note);
6969

70-
res.status(400).send({message : "No author with given name has been found"});
70+
res.status(400).send({message : "No note with given title name has been found"});
7171

7272
});
7373
};
7474

75+
//update any note with given id
7576
exports.update=function(req,res){
76-
77+
7778
};
7879

80+
//to delete any note by id
7981
exports.delete=function(req,res){
80-
82+
Note.findByIdAndDelete(req.params.noteId)
83+
.then(note=>{
84+
if(!note){
85+
return res.status(400).send({message :"Note not found with id"+req.params.noteId});
86+
}
87+
res.send({message : "note deleted successfully"});
88+
}).catch(err => {
89+
if(err.kind === 'ObjectId' || err.name === 'NotFound') {
90+
return res.status(404).send({
91+
message: "Note not found with id " + req.params.noteId
92+
});
93+
}
94+
return res.status(500).send({
95+
message: "Could not delete note with id " + req.params.noteId
96+
});
97+
});
8198
};

CRUD API's/app/routes/routes.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ module.exports=(app)=>{
1717
app.get('/api/findbytitle/:title',notes.findbytitle);
1818

1919
//update a note with noteId
20-
app.put('/api/notes/:noteId',notes.update);
20+
app.put('/api/update/:noteId',notes.update);
2121

2222
//delete a Note with noteId
23-
app.delete('/api/notes/:noteId',notes.delete);
23+
app.delete('/api/delete/:noteId',notes.delete);
2424
}

0 commit comments

Comments
 (0)