This repository has been archived on 2021-10-31. You can view files and clone it, but cannot push or open issues or pull requests.
SA3/hw6/Claudio_Maggioni/routes/favourites_db/router.js

163 lines
3.4 KiB
JavaScript
Raw Permalink Normal View History

2019-10-28 14:00:36 +00:00
/** @module root/router */
'use strict';
// vim: set ts=2 sw=2 et tw=80:
const fs = require('fs');
const express = require('express');
const router = express.Router();
const mongoose = require('mongoose');
const Favorite = mongoose.model('Favorite');
const { error, renderFav, parseId, notFound } = require('../utils');
function findAndRender(filter, req, res) {
Favorite.find(filter, (err, favs) => {
if (err) {
return error(err, res);
}
renderFav(req, res, favs);
});
}
2019-10-28 14:00:36 +00:00
router.post('/', (req, res) => {
if (!req.body.name || !req.body.dataURL) {
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.end('Bad create form parameters');
return;
}
const data = {
name: req.body.name,
dataURL: req.body.dataURL,
2019-10-28 21:45:15 +00:00
bookmarked: req.body.bookmarked
}
2019-10-28 14:00:36 +00:00
const favourite = new Favorite(data);
if (req.body._id) {
favourite._id = req.body._id;
} else {
favourite._id = mongoose.Types.ObjectId();
2019-10-28 14:00:36 +00:00
}
favourite.save((err, fav) => {
if (err) {
return error(err, res);
}
res.status = 201;
2019-10-28 21:45:15 +00:00
renderFav(req, res, fav, false);
2019-10-28 14:00:36 +00:00
});
});
router.get('/', (req, res) => {
findAndRender({}, req, res);
2019-10-28 14:00:36 +00:00
});
router.get('/search', (req, res) => {
const filter = Object.assign({}, req.query);
delete filter['dataURL'];
delete filter['_method'];
findAndRender(filter, req, res);
2019-10-28 14:00:36 +00:00
});
2019-10-28 21:45:15 +00:00
function findOne(id) {
return (req, res) => {
Favorite.findById(id(req), (err, fav) => {
if (err) {
return error(err, res);
}
2019-10-28 21:45:15 +00:00
if (notFound(fav, res)) {
return;
}
2019-10-28 21:45:15 +00:00
renderFav(req, res, fav, false);
});
};
}
router.get('/:id', findOne(req => parseId(req)));
2019-10-28 14:00:36 +00:00
function handleUpdate(partial = false) {
return (req, res) => {
const edit = {};
for (const key of ['dataURL', 'name']) {
if (req.body[key]) {
edit[key] = req.body[key];
} else if (!partial) {
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.end('Bad PUT form parameters');
return;
}
}
if (req.body.bookmarked !== undefined) {
edit.bookmarked = req.body.bookmarked;
2019-10-28 14:00:36 +00:00
}
Favorite.findByIdAndUpdate(parseId(req), { $set: edit }, {
2019-10-28 21:45:15 +00:00
new: false,
2019-10-28 14:00:36 +00:00
upsert: true,
setDefaultsOnInsert: true,
2019-10-28 21:45:15 +00:00
passRawResult: true,
2019-10-28 14:00:36 +00:00
}, (err, fav) => {
if (err) {
return error(err, res);
}
2019-10-28 21:45:15 +00:00
if (fav == null) {
res.status = 201;
findOne(() => parseId(req))(req, res);
return;
}
2019-10-28 14:00:36 +00:00
res.status = 200;
renderFav(req, res, fav, false);
});
};
}
router.put('/:id', handleUpdate());
router.patch('/:id', handleUpdate(true));
2019-10-28 14:00:36 +00:00
router.delete('/:id', (req, res) => {
Favorite.findByIdAndDelete(parseId(req), (err, fav) => {
2019-10-28 14:00:36 +00:00
if (err) {
return error(err, res);
}
if (notFound(fav, res)) {
2019-10-28 14:00:36 +00:00
return;
}
res.format({
json: () => res.writeHead(204),
html: () => res.writeHead(302, { 'Location': '/favorites' })
});
res.end();
});
});
router.put('/:id/bookmarked', (req, res) => {
Favorite.findByIdAndUpdate(parseId(req), {
$set: { bookmarked: req.body.bookmarked }
2019-10-28 14:00:36 +00:00
}, { new: true }, (err, fav) => {
if (notFound(fav, res)) {
return;
}
if (!req.body.bookmarked) {
2019-10-28 14:00:36 +00:00
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.end('Bad PUT bookmark form parameters');
} else {
renderFav(req, res, fav, false);
2019-10-28 14:00:36 +00:00
}
});
});
/** router for /root */
module.exports = router;