Skip to content

Commit c4dbc3c

Browse files
committed
some changes
1 parent bf7413a commit c4dbc3c

File tree

1 file changed

+46
-64
lines changed

1 file changed

+46
-64
lines changed

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

Lines changed: 46 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -19,63 +19,57 @@ exports.create=function(req,res){
1919
Note.findOne({title: newnote.title},function(err,note){
2020
if(note)return res.status(400).json({message: "same title exists"});
2121

22-
newnote.save()
23-
.then(data=> {res.status(201).send(data)})
24-
.catch(err=>{
25-
res.status(404).json({"message" : "Some error occurred while creating the Note"});
22+
newnote.save(function(err,doc){
23+
if(err) return res.status(400).json(err);
24+
res.status(201).json({
25+
post : true,
26+
note : doc
27+
});
2628
});
27-
2829
});
2930
};
3031

3132

3233
// to find all the notes
3334
exports.findAll=function(req,res){
34-
Note.find()
35-
.then(notes=>{
36-
res.status(200).send(notes);
37-
}).catch(err=>{
38-
res.status(404).json({"message": "Some error occurred while retrieving notes."});
39-
});
35+
Note.find(function(err,doc){
36+
if(err) return res.status(400).send(err);
37+
res.status(200).json(doc);
38+
})
4039
};
4140

4241
// find a note by id
4342
exports.findone=function(req,res){
4443

45-
Note.findById(req.params.noteId)
46-
.then(note=>{
47-
if(!note){
48-
return res.status(404).send({message:"Note not found with id " + req.params.noteId});
49-
}
50-
res.status(200).send(note);
51-
}).catch(err=>{
52-
if(err.kind=='ObjectId'){
53-
return res.status(404).send({ message: "Note not found with id " + req.params.noteId });
54-
}
55-
return res.status(500).send({
56-
message: "Error retrieving note with id " + req.params.noteId
57-
});
58-
});
44+
Note.findById(req.params.noteId,function(err,doc){
45+
if(err) return res.status(400).send(err);
46+
47+
res.status(200),json(doc);
48+
})
49+
5950

6051
};
6152

6253
//request to find ny the name of author
6354
exports.findbyauthor=function(req,res){
6455
Note.find({author:req.params.author},function(err,note){
56+
if(err) return res.status(400).send(err);
57+
6558
if(!note) return res.status(404).send({message:"not found"});
66-
else{
67-
return res.status(200).send(note);
68-
}
59+
60+
res.status(200).json(note);
61+
6962
});
7063
};
7164

7265
// request to find by the name of title of note
7366
exports.findbytitle=function(req,res){
7467
Note.find({title:req.params.title},function(err,note){
75-
if(note) return res.status(200).send(note);
68+
if(err) return res.status(400).send(err);
7669

77-
res.status(404).send({message : "No note with given title name has been found"});
78-
70+
if(!note) return res.status(404).send({message:"not found"});
71+
72+
res.status(200).json(note);
7973
});
8074
};
8175

@@ -90,42 +84,30 @@ exports.update=function(req,res){
9084
title : req.body.title,
9185
author : req.body.author,
9286
content : req.body.content
93-
},{new: true})
94-
.then(note=>{
95-
if(!note){
96-
return res.status(404).send({message: "note not found with id "+req.params.noteId});
97-
}
98-
res.status(200).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-
87+
},{new: true},function(err,doc){
88+
if(err) return res.status(400).send(err);
89+
90+
if(!doc) return res.status(404).json({message : "No note with this id has been found"});
11091

92+
res.status(200).json({
93+
update : true,
94+
note : doc
95+
})
96+
})
97+
11198
};
11299

113100
//to delete any note by id
114101
exports.delete=function(req,res){
115-
Note.findByIdAndDelete(req.params.noteId)
116-
.then(note=>{
117-
if(!note){
118-
return res.status(404).send({message :"Note not found with id"+req.params.noteId});
119-
}
120-
res.send({message : "note deleted successfully"});
121-
}).catch(err => {
122-
if(err.kind === 'ObjectId' || err.name === 'NotFound') {
123-
return res.status(404).send({
124-
message: "Note not found with id " + req.params.noteId
125-
});
126-
}
127-
return res.status(500).send({
128-
message: "Could not delete note with id " + req.params.noteId
102+
Note.findByIdAndDelete(req.params.noteId,function(err,doc){
103+
if(err) return res.status(400).send(err);
104+
105+
if(!doc) return res.status(404).json({message : "NOt found"});
106+
107+
res.status(200).json({
108+
delete : true,
109+
note : doc
129110
});
130-
});
131-
};
111+
})
112+
113+
};

0 commit comments

Comments
 (0)