File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed
CRUD API's/app/controllers Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -2,6 +2,13 @@ const Note=require('../models/model');
22
33// create and save a new note
44exports . 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
7683exports . 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
You can’t perform that action at this time.
0 commit comments