@@ -56,9 +56,9 @@ exports.findone=function(req,res){
5656exports . 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
7576exports . update = function ( req , res ) {
76-
77+
7778} ;
7879
80+ //to delete any note by id
7981exports . 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} ;
0 commit comments