|
| 1 | +import Note from '../models/note.model'; |
| 2 | + |
| 3 | +// Create and Save a new Note |
| 4 | +export const createNote = (req, res) => { |
| 5 | + // Validate request |
| 6 | + if (!req.body.content) { |
| 7 | + return res.status(400).send({ |
| 8 | + message: 'Note content can not be empty', |
| 9 | + }); |
| 10 | + } |
| 11 | + |
| 12 | + // Create a Note |
| 13 | + const note = new Note({ |
| 14 | + title: req.body.title || 'Untitled Note', |
| 15 | + content: req.body.content, |
| 16 | + }); |
| 17 | + |
| 18 | + // Save Note in the database |
| 19 | + note |
| 20 | + .save() |
| 21 | + .then((data) => { |
| 22 | + res.send(data); |
| 23 | + }) |
| 24 | + .catch((err) => { |
| 25 | + res.status(500).send({ |
| 26 | + message: err.message || 'Some error occurred while creating the Note.', |
| 27 | + }); |
| 28 | + }); |
| 29 | +}; |
| 30 | + |
| 31 | +// Retrieve and return all notes from the database. |
| 32 | +export const findAll = (req, res) => { |
| 33 | + Note.find() |
| 34 | + .then((notes) => { |
| 35 | + res.send(notes); |
| 36 | + }) |
| 37 | + .catch((err) => { |
| 38 | + res.status(500).send({ |
| 39 | + message: err.message || 'Some error occurred while retrieving notes.', |
| 40 | + }); |
| 41 | + }); |
| 42 | +}; |
| 43 | + |
| 44 | +// Find a single note with a noteId |
| 45 | +export const findOne = (req, res) => { |
| 46 | + Note.findById(req.params.noteId) |
| 47 | + .then((note) => { |
| 48 | + if (!note) { |
| 49 | + return res.status(404).send({ |
| 50 | + message: 'Note not found with id ' + req.params.noteId, |
| 51 | + }); |
| 52 | + } |
| 53 | + res.send(note); |
| 54 | + }) |
| 55 | + .catch((err) => { |
| 56 | + if (err.kind === 'ObjectId') { |
| 57 | + return res.status(404).send({ |
| 58 | + message: 'Note not found with id ' + req.params.noteId, |
| 59 | + }); |
| 60 | + } |
| 61 | + return res.status(500).send({ |
| 62 | + message: 'Error retrieving note with id ' + req.params.noteId, |
| 63 | + }); |
| 64 | + }); |
| 65 | +}; |
| 66 | + |
| 67 | +// Update a note identified by the noteId in the request |
| 68 | +export const updateNote = (req, res) => { |
| 69 | + // Validate Request |
| 70 | + if (!req.body.content) { |
| 71 | + return res.status(400).send({ |
| 72 | + message: 'Note content can not be empty', |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + // Find note and update it with the request body |
| 77 | + Note.findByIdAndUpdate( |
| 78 | + req.params.noteId, |
| 79 | + { |
| 80 | + title: req.body.title || 'Untitled Note', |
| 81 | + content: req.body.content, |
| 82 | + }, |
| 83 | + { new: true } |
| 84 | + ) |
| 85 | + .then((note) => { |
| 86 | + if (!note) { |
| 87 | + return res.status(404).send({ |
| 88 | + message: 'Note not found with id ' + req.params.noteId, |
| 89 | + }); |
| 90 | + } |
| 91 | + res.send(note); |
| 92 | + }) |
| 93 | + .catch((err) => { |
| 94 | + if (err.kind === 'ObjectId') { |
| 95 | + return res.status(404).send({ |
| 96 | + message: 'Note not found with id ' + req.params.noteId, |
| 97 | + }); |
| 98 | + } |
| 99 | + return res.status(500).send({ |
| 100 | + message: 'Error updating note with id ' + req.params.noteId, |
| 101 | + }); |
| 102 | + }); |
| 103 | +}; |
| 104 | + |
| 105 | +// Delete a note with the specified noteId in the request |
| 106 | +export const deleteNote = (req, res) => { |
| 107 | + Note.findByIdAndRemove(req.params.noteId) |
| 108 | + .then((note) => { |
| 109 | + if (!note) { |
| 110 | + return res.status(404).send({ |
| 111 | + message: 'Note not found with id ' + req.params.noteId, |
| 112 | + }); |
| 113 | + } |
| 114 | + res.send({ message: 'Note deleted successfully!' }); |
| 115 | + }) |
| 116 | + .catch((err) => { |
| 117 | + if (err.kind === 'ObjectId' || err.name === 'NotFound') { |
| 118 | + return res.status(404).send({ |
| 119 | + message: 'Note not found with id ' + req.params.noteId, |
| 120 | + }); |
| 121 | + } |
| 122 | + return res.status(500).send({ |
| 123 | + message: 'Could not delete note with id ' + req.params.noteId, |
| 124 | + }); |
| 125 | + }); |
| 126 | +}; |
0 commit comments