Note routes

pull/1/head
sundowndev 2018-11-13 18:47:42 +01:00
parent 5181a9100a
commit b35a5b26c0
6 changed files with 87 additions and 11 deletions

View File

@ -1,5 +1,12 @@
module.exports = (req, res) => {
const notes = [];
const mongoose = require('mongoose');
res.status(200).json({ notes });
module.exports = (req, res) => {
const NoteModel = mongoose.model('Note');
NoteModel.find({})
.lean()
.exec()
.then((result) => {
res.status(200).json(result);
});
};

View File

@ -0,0 +1,17 @@
const mongoose = require('mongoose');
// TODO: verify auth
module.exports = (req, res) => {
const NoteModel = mongoose.model('Note');
const Note = new NoteModel({ title: req.body.title, text: req.body.text });
Note.save((err) => {
if (err) {
return res.status(400).json({ success: false, msg: 'Title and text must not be blank.' });
}
return res.status(200).json(Note);
});
};

View File

@ -0,0 +1,18 @@
const mongoose = require('mongoose');
// TODO: verify auth
// TODO: verify owner
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.' });
});
};

View File

@ -34,17 +34,26 @@ note.get('/:id', single);
*
* @apiSuccess {Object} Object Created note.
*/
note.post('/', all);
note.post('/', create);
/**
* @api {delete} /note/:id Delete a note
* @apiName DeleteNote
* @apiGroup Note
*
* @apiParam {Number} id Note id.
*/
note.delete('/:id', all);
note.delete('/:id', remove);
//note.post('/', create)
//note.put('/:noteId', update)
//note.delete('/:noteId', remove)
/**
* @api {put} /note/:id Update a note
* @apiName UpdateNote
* @apiGroup Note
*
* @apiParam {Number} id Note id.
*
* @apiSuccess {Object} Object Updated note.
*/
note.put('/:id', update);
module.exports = note;

View File

@ -1,5 +1,15 @@
module.exports = (req, res) => {
const note = req.model;
const mongoose = require('mongoose');
res.status(200).json({ note });
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.' });
});
};

View File

@ -0,0 +1,15 @@
const mongoose = require('mongoose');
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.' });
});
};