102 lines
2.3 KiB
JavaScript
102 lines
2.3 KiB
JavaScript
|
|
// Create and Save a new Note
|
|
const dbmanager = require('../lib/dbmanager');
|
|
|
|
exports.create = (req, res) => {
|
|
// Validate request
|
|
console.log('>create req', req.body);
|
|
|
|
if(!req.body.name)
|
|
return res.status(400).send({
|
|
'message': 'Recipe content can not be empty'
|
|
});
|
|
|
|
dbmanager.insertOne(req.body)
|
|
.then((data) => {
|
|
res.send(data);
|
|
})
|
|
.catch((err) => {
|
|
res.status(500).send({
|
|
'message': err.message || 'Some error occurred while querying the database.'
|
|
});
|
|
});
|
|
};
|
|
|
|
// Retrieve and return all notes from the database.
|
|
exports.findAll = (req, res) => {
|
|
console.log('>findAll req');
|
|
|
|
dbmanager.getAll()
|
|
.then((data) => {
|
|
res.send(data);
|
|
})
|
|
.catch((err) => {
|
|
res.status(500).send({
|
|
'message': err.message || 'Some error occurred while querying the database.'
|
|
});
|
|
});
|
|
};
|
|
|
|
// Find a single note with a noteId
|
|
exports.findOne = (req, res) => {
|
|
console.log('>findOne req', req.params);
|
|
|
|
if(!req.params.recipeId)
|
|
return res.status(400).send({
|
|
'message': 'Recipe id missing'
|
|
});
|
|
|
|
const hash = req.params.recipeId;
|
|
|
|
dbmanager.getOne(hash)
|
|
.then((data) => {
|
|
res.send(data);
|
|
})
|
|
.catch((err) => {
|
|
res.status(500).send({
|
|
'message': err.message || 'Some error occurred while querying the database.'
|
|
});
|
|
});
|
|
};
|
|
|
|
// Update a note identified by the noteId in the request
|
|
exports.update = (req, res) => {
|
|
console.log('>update req', req.body.content);
|
|
if(!req.params.recipeId)
|
|
return res.status(400).send({
|
|
'message': 'Recipe id missing'
|
|
});
|
|
|
|
dbmanager.updateOne(req.body)
|
|
.then((data) => {
|
|
res.send(data);
|
|
})
|
|
.catch((err) => {
|
|
res.status(500).send({
|
|
'message': err.message || 'Some error occurred while querying the database.'
|
|
});
|
|
});
|
|
};
|
|
|
|
// Delete a note with the specified noteId in the request
|
|
exports.delete = (req, res) => {
|
|
console.log('>delete req', req.params.recipeId);
|
|
|
|
if(!req.params.recipeId)
|
|
return res.status(400).send({
|
|
'message': 'Recipe id missing'
|
|
});
|
|
|
|
const hash = req.params.recipeId;
|
|
|
|
dbmanager.deleteOne(hash)
|
|
.then((data) => {
|
|
res.send(data);
|
|
})
|
|
.catch((err) => {
|
|
res.status(500).send({
|
|
'message': err.message || 'Some error occurred while querying the database.'
|
|
});
|
|
});
|
|
};
|