From 80a57fc7211df254c923930a4d43bfe57a6be88c Mon Sep 17 00:00:00 2001 From: sundowndev Date: Wed, 14 Nov 2018 18:15:31 +0100 Subject: [PATCH] Note routes --- app/routes/note/all.js | 12 ------------ app/routes/note/delete.js | 19 ++++++++++--------- app/routes/note/index.js | 22 ++++++---------------- app/routes/note/single.js | 8 +++++++- app/routes/note/update.js | 31 ++++++++++++++++++++++--------- 5 files changed, 45 insertions(+), 47 deletions(-) delete mode 100644 app/routes/note/all.js diff --git a/app/routes/note/all.js b/app/routes/note/all.js deleted file mode 100644 index a64ac2c..0000000 --- a/app/routes/note/all.js +++ /dev/null @@ -1,12 +0,0 @@ -const mongoose = require('mongoose'); - -module.exports = (req, res) => { - const NoteModel = mongoose.model('Note'); - - NoteModel.find({}) - .lean() - .exec() - .then((result) => { - res.status(200).json(result); - }); -}; diff --git a/app/routes/note/delete.js b/app/routes/note/delete.js index 68fe7d2..73afb4f 100644 --- a/app/routes/note/delete.js +++ b/app/routes/note/delete.js @@ -6,13 +6,14 @@ const mongoose = require('mongoose'); module.exports = (req, res) => { const NoteModel = mongoose.model('Note'); - NoteModel.findByIdAndDelete(req.params.id) - .lean() - .exec() - .then(() => { - res.status(200).json({ success: true, msg: 'Note deleted.' }); - }) - .catch(() => { - res.status(404).json({ success: false, msg: 'Note does not exists.' }); - }); + NoteModel.findOneAndDelete({ _id: req.params.id }, (err, note) => { + if (err) return res.status(500).send(err); + if (!note) return res.status(404).json({ message: 'Note does not exists.' }); + + const response = { + message: 'Note successfully deleted', + }; + + return res.status(200).send(response); + }); }; diff --git a/app/routes/note/index.js b/app/routes/note/index.js index dbdf13e..b857d60 100644 --- a/app/routes/note/index.js +++ b/app/routes/note/index.js @@ -1,26 +1,16 @@ const note = require('express').Router(); -const all = require('./all'); const single = require('./single'); const create = require('./create'); const update = require('./update'); const remove = require('./delete'); /** - * @api {get} /note Request all notes + * @api {get} /note/:id Get note * @apiName GetNotes * @apiGroup Note * - * @apiSuccess {Array} Array Array of Note objects. - */ -note.get('/', all); - -/** - * @api {get} /note/:id Request Note information - * @apiName GetNotes - * @apiGroup Note - * - * @apiParam {Number} id Notes unique ID. + * @apiParam {String} id Note unique ID. * * @apiSuccess {string} title Title of the note. * @apiSuccess {string} text Text of the note. @@ -37,20 +27,20 @@ note.get('/:id', single); note.post('/', create); /** - * @api {delete} /note/:id Delete a note + * @api {delete} /note/:id Delete note * @apiName DeleteNote * @apiGroup Note * - * @apiParam {Number} id Note id. + * @apiParam {String} id Note unique ID. */ note.delete('/:id', remove); /** - * @api {put} /note/:id Update a note + * @api {put} /note/:id Update note * @apiName UpdateNote * @apiGroup Note * - * @apiParam {Number} id Note id. + * @apiParam {String} id Note unique ID. * * @apiSuccess {Object} Object Updated note. */ diff --git a/app/routes/note/single.js b/app/routes/note/single.js index 5be1fa8..32701c1 100644 --- a/app/routes/note/single.js +++ b/app/routes/note/single.js @@ -1,12 +1,18 @@ const mongoose = require('mongoose'); +// TODO: verify owner + module.exports = (req, res) => { const NoteModel = mongoose.model('Note'); - NoteModel.find({ _id: req.params.id }) + NoteModel.findOne({ _id: req.params.id }) .lean() .exec() .then((result) => { + if (result === null) { + res.status(404).json({ success: false, msg: 'Note does not exists.' }); + } + res.status(200).json(result); }) .catch(() => { diff --git a/app/routes/note/update.js b/app/routes/note/update.js index 5be1fa8..fa7f85d 100644 --- a/app/routes/note/update.js +++ b/app/routes/note/update.js @@ -1,15 +1,28 @@ const mongoose = require('mongoose'); +// TODO: verify auth +// TODO: verify owner + module.exports = (req, res) => { const NoteModel = mongoose.model('Note'); - NoteModel.find({ _id: req.params.id }) - .lean() - .exec() - .then((result) => { - res.status(200).json(result); - }) - .catch(() => { - res.status(404).json({ success: false, msg: 'Note does not exists.' }); - }); + NoteModel.findOneAndUpdate( + // the id of the item to find + { _id: req.params.id }, + + // the change to be made. Mongoose will smartly combine your existing + // document with this change, which allows for partial updates too + req.body, + + // an option that asks mongoose to return the updated version + // of the document instead of the pre-updated one. + { new: true }, + + // the callback function + (err, note) => { + // Handle any possible database errors + if (err) return res.status(500).send(err); + return res.json(note); + }, + ); };