Essential backend functionality, including logins.

This commit is contained in:
2019-08-21 12:52:12 +02:00
parent d2168ab4a0
commit 99f2c5e7d8
14 changed files with 3965 additions and 1 deletions

View File

@@ -0,0 +1,49 @@
'use strict';
const mongoose = require('mongoose'),
Book = mongoose.model('Book');
exports.list_all = function(req, res) {
Book.find({}, function(error, books) {
if (error) res.status(400).send(error);
else res.json(books);
});
};
exports.create = function(req, res) {
// TODO authenticate
const book = new Book(req.body);
book.save(function(error, book) {
if (error) res.status(400).send(error);
else res.json(book);
});
};
exports.get = function(req, res) {
Book.findById(req.params.bookId, function(error, book) {
if (error) res.status(400).send(error);
else res.json(book);
});
};
exports.update = function(req, res) {
Book.findOneAndUpdate({
_id: req.params.bookId
}, req.body, {
new: true
}, function(error, book) {
if (error) res.status(400).send(error);
else res.json(book);
});
};
exports.delete = function(req, res) {
Book.remove({
_id: req.params.bookId
}, function(error, book) {
if (error) res.status(400).send(error);
else res.json({
message: 'Book ' + book.title + ' deleted'
});
});
};