Note routes

pull/1/head
sundowndev 2018-11-14 18:15:31 +01:00
parent 78604bc8bd
commit 80a57fc721
5 changed files with 45 additions and 47 deletions

View File

@ -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);
});
};

View File

@ -6,13 +6,14 @@ const mongoose = require('mongoose');
module.exports = (req, res) => { module.exports = (req, res) => {
const NoteModel = mongoose.model('Note'); const NoteModel = mongoose.model('Note');
NoteModel.findByIdAndDelete(req.params.id) NoteModel.findOneAndDelete({ _id: req.params.id }, (err, note) => {
.lean() if (err) return res.status(500).send(err);
.exec() if (!note) return res.status(404).json({ message: 'Note does not exists.' });
.then(() => {
res.status(200).json({ success: true, msg: 'Note deleted.' }); const response = {
}) message: 'Note successfully deleted',
.catch(() => { };
res.status(404).json({ success: false, msg: 'Note does not exists.' });
return res.status(200).send(response);
}); });
}; };

View File

@ -1,26 +1,16 @@
const note = require('express').Router(); const note = require('express').Router();
const all = require('./all');
const single = require('./single'); const single = require('./single');
const create = require('./create'); const create = require('./create');
const update = require('./update'); const update = require('./update');
const remove = require('./delete'); const remove = require('./delete');
/** /**
* @api {get} /note Request all notes * @api {get} /note/:id Get note
* @apiName GetNotes * @apiName GetNotes
* @apiGroup Note * @apiGroup Note
* *
* @apiSuccess {Array} Array Array of Note objects. * @apiParam {String} id Note unique ID.
*/
note.get('/', all);
/**
* @api {get} /note/:id Request Note information
* @apiName GetNotes
* @apiGroup Note
*
* @apiParam {Number} id Notes unique ID.
* *
* @apiSuccess {string} title Title of the note. * @apiSuccess {string} title Title of the note.
* @apiSuccess {string} text Text of the note. * @apiSuccess {string} text Text of the note.
@ -37,20 +27,20 @@ note.get('/:id', single);
note.post('/', create); note.post('/', create);
/** /**
* @api {delete} /note/:id Delete a note * @api {delete} /note/:id Delete note
* @apiName DeleteNote * @apiName DeleteNote
* @apiGroup Note * @apiGroup Note
* *
* @apiParam {Number} id Note id. * @apiParam {String} id Note unique ID.
*/ */
note.delete('/:id', remove); note.delete('/:id', remove);
/** /**
* @api {put} /note/:id Update a note * @api {put} /note/:id Update note
* @apiName UpdateNote * @apiName UpdateNote
* @apiGroup Note * @apiGroup Note
* *
* @apiParam {Number} id Note id. * @apiParam {String} id Note unique ID.
* *
* @apiSuccess {Object} Object Updated note. * @apiSuccess {Object} Object Updated note.
*/ */

View File

@ -1,12 +1,18 @@
const mongoose = require('mongoose'); const mongoose = require('mongoose');
// TODO: verify owner
module.exports = (req, res) => { module.exports = (req, res) => {
const NoteModel = mongoose.model('Note'); const NoteModel = mongoose.model('Note');
NoteModel.find({ _id: req.params.id }) NoteModel.findOne({ _id: req.params.id })
.lean() .lean()
.exec() .exec()
.then((result) => { .then((result) => {
if (result === null) {
res.status(404).json({ success: false, msg: 'Note does not exists.' });
}
res.status(200).json(result); res.status(200).json(result);
}) })
.catch(() => { .catch(() => {

View File

@ -1,15 +1,28 @@
const mongoose = require('mongoose'); const mongoose = require('mongoose');
// TODO: verify auth
// TODO: verify owner
module.exports = (req, res) => { module.exports = (req, res) => {
const NoteModel = mongoose.model('Note'); const NoteModel = mongoose.model('Note');
NoteModel.find({ _id: req.params.id }) NoteModel.findOneAndUpdate(
.lean() // the id of the item to find
.exec() { _id: req.params.id },
.then((result) => {
res.status(200).json(result); // the change to be made. Mongoose will smartly combine your existing
}) // document with this change, which allows for partial updates too
.catch(() => { req.body,
res.status(404).json({ success: false, msg: 'Note does not exists.' });
}); // 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);
},
);
}; };