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) => {
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);
});
};

View File

@ -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.
*/

View File

@ -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(() => {

View File

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