From b35a5b26c0fb04c9089735912e27690a4c15582f Mon Sep 17 00:00:00 2001 From: sundowndev Date: Tue, 13 Nov 2018 18:47:42 +0100 Subject: [PATCH] Note routes --- app/routes/note/all.js | 13 ++++++++++--- app/routes/note/create.js | 17 +++++++++++++++++ app/routes/note/delete.js | 18 ++++++++++++++++++ app/routes/note/index.js | 19 ++++++++++++++----- app/routes/note/single.js | 16 +++++++++++++--- app/routes/note/update.js | 15 +++++++++++++++ 6 files changed, 87 insertions(+), 11 deletions(-) diff --git a/app/routes/note/all.js b/app/routes/note/all.js index b0c2789..a64ac2c 100644 --- a/app/routes/note/all.js +++ b/app/routes/note/all.js @@ -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); + }); }; diff --git a/app/routes/note/create.js b/app/routes/note/create.js index e69de29..a0b6c2a 100644 --- a/app/routes/note/create.js +++ b/app/routes/note/create.js @@ -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); + }); +}; diff --git a/app/routes/note/delete.js b/app/routes/note/delete.js index e69de29..68fe7d2 100644 --- a/app/routes/note/delete.js +++ b/app/routes/note/delete.js @@ -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.' }); + }); +}; diff --git a/app/routes/note/index.js b/app/routes/note/index.js index 88ef8af..dbdf13e 100644 --- a/app/routes/note/index.js +++ b/app/routes/note/index.js @@ -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; diff --git a/app/routes/note/single.js b/app/routes/note/single.js index bcbb2f1..5be1fa8 100644 --- a/app/routes/note/single.js +++ b/app/routes/note/single.js @@ -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.' }); + }); }; diff --git a/app/routes/note/update.js b/app/routes/note/update.js index e69de29..5be1fa8 100644 --- a/app/routes/note/update.js +++ b/app/routes/note/update.js @@ -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.' }); + }); +};