-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
const Note = require('../model/note.model'); | ||
const uuidv1 = require("UUID/V1") | ||
|
||
// Create and Save a new Note | ||
exports.create = (req, res) => { | ||
// Validate request | ||
if (!req.body.content) { | ||
return res.status(400).send({ | ||
message: "Note content can not be empty" | ||
}); | ||
} | ||
|
||
|
||
Note.findOne({ "email": req.body.email }, (err, result) => { | ||
if (err) throw err; | ||
if (result != null) { | ||
return res.status(409).send(); | ||
} | ||
|
||
// Create a Note | ||
const note = new Note({ | ||
_id: uuidv1(), | ||
title: req.body.title || "Untitled Note", | ||
content: req.body.content, | ||
email: req.body.email | ||
}); | ||
|
||
// Save Note in the database | ||
note.save() | ||
.then(data => { | ||
res.send(data); | ||
}).catch(err => { | ||
res.status(500).send({ | ||
message: err.message || "Some error occurred while creating the Note." | ||
}); | ||
}); | ||
}) | ||
}; | ||
|
||
// Retrieve and return all notes from the database. | ||
exports.findAll = (req, res) => { | ||
Note.find() | ||
.then(notes => { | ||
res.send(notes); | ||
}).catch(err => { | ||
res.status(500).send({ | ||
message: err.message || "Some error occurred while retrieving notes." | ||
}); | ||
}); | ||
}; | ||
|
||
exports.findOne = (req, res) => { | ||
Note.findById(req.params.noteId) | ||
.then(note => { | ||
if (!note) { | ||
return res.status(404).send({ | ||
message: "Note not found with id " + req.params.noteId | ||
}); | ||
} | ||
res.send(note); | ||
}).catch(err => { | ||
if (err.kind === 'ObjectId') { | ||
return res.status(404).send({ | ||
message: "Note not found with id " + req.params.noteId | ||
}); | ||
} | ||
return res.status(500).send({ | ||
message: "Error retrieving note with id " + req.params.noteId | ||
}); | ||
}); | ||
}; | ||
|
||
exports.update = (req, res) => { | ||
|
||
if (!req.body.content && !req.body._id) { | ||
return res.status(400).send({ | ||
message: "Note content can not be empty" | ||
}); | ||
} | ||
|
||
// Find note and update it with the request body | ||
Note.findByIdAndUpdate(req.params.noteId, { | ||
title: req.body.title || "Untitled Note", | ||
content: req.body.content | ||
}, { new: true }) | ||
.then(note => { | ||
if (!note) { | ||
return res.status(404).send({ | ||
message: "Note not found with id " + req.params.noteId | ||
}); | ||
} | ||
res.send(note); | ||
}).catch(err => { | ||
if (err.kind === 'ObjectId') { | ||
return res.status(404).send({ | ||
message: "Note not found with id " + req.params.noteId | ||
}); | ||
} | ||
return res.status(500).send({ | ||
message: "Error updating note with id " + req.params.noteId | ||
}); | ||
}); | ||
}; | ||
|
||
exports.delete = (req, res) => { | ||
Note.findByIdAndRemove(req.params.noteId) | ||
.then(note => { | ||
if (!note) { | ||
return res.status(404).send({ | ||
message: "Note not found with id " + req.params.noteId | ||
}); | ||
} | ||
res.send({ message: "Note deleted successfully!" }); | ||
}).catch(err => { | ||
if (err.kind === 'ObjectId' || err.name === 'NotFound') { | ||
return res.status(404).send({ | ||
message: "Note not found with id " + req.params.noteId | ||
}); | ||
} | ||
return res.status(500).send({ | ||
message: "Could not delete note with id " + req.params.noteId | ||
}); | ||
}); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const mongoose = require('mongoose'); | ||
|
||
const NoteSchema = mongoose.Schema({ | ||
_id: String, | ||
title: String, | ||
content: String, | ||
email: String | ||
}, { | ||
timestamps: true | ||
}); | ||
|
||
|
||
|
||
module.exports = mongoose.model('Note', NoteSchema); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
module.exports = (app) => { | ||
const notes = require('../controllers/controller'); | ||
|
||
// Create a new Note | ||
app.post('/notes', notes.create); | ||
|
||
// Retrieve all Notes | ||
app.get('/notes', notes.findAll); | ||
|
||
// Retrieve a single Note with noteId | ||
app.get('/notes/:noteId', notes.findOne); | ||
|
||
// Update a Note with noteId | ||
app.put('/notes/:noteId', notes.update); | ||
|
||
// Delete a Note with noteId | ||
app.delete('/notes/:noteId', notes.delete); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
url: 'mongodb://localhost:27017/easy-notes' | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.