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

159 lines
3.6 KiB
JavaScript

/** @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 } = require('../utils');
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,
bookmarked: req.body.bookmarked,
};
const favourite = new Favorite(data);
if (req.body._id) {
favourite._id = req.body._id;
}
favourite.save((err, fav) => {
if (err) {
return error(err, res);
}
res.status = 201;
const _id = fav._id;
renderFav(req, res, Object.assign({ _id }, data), false);
});
});
router.get('/', (req, res) => {
Favorite.find({}, (err, favs) => {
if (err) {
return error(err, res);
}
renderFav(req, res, favs);
});
});
router.get('/search', (req, res) => {
const filter = Object.assign({}, req.query);
delete filter['dataURL'];
delete filter['_method'];
Favorite.find(filter, (err, favs) => {
if (err) {
return error(err, res);
}
renderFav(req, res, favs);
});
});
router.get('/:id', (req, res) => {
Favorite.findById(req.params.id, (err, fav) => {
if (err) {
return error(err, res);
} else if (!fav) {
res.writeHead(404, {'Content-Type': 'text/plain'});
res.end('Not found');
} else {
renderFav(req, res, fav, false);
}
});
});
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;
}
Favorite.findByIdAndUpdate(req.params.id, { $set: edit }, {
new: true,
upsert: true,
setDefaultsOnInsert: true,
}, (err, fav) => {
if (err) {
return error(err, res);
}
console.log(arguments);
// FIXME: return 201 on creation
res.status = 200;
renderFav(req, res, fav, false);
});
};
}
router.put('/:id', handleUpdate());
router.patch('/:id', handleUpdate(true));
router.delete('/:id', (req, res) => {
Favorite.findByIdAndDelete(req.params.id, (err, fav) => {
if (err) {
return error(err, res);
}
if (fav == null) {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Favourite not found');
return;
}
res.format({
json: () => res.writeHead(204),
html: () => res.writeHead(302, { 'Location': '/favorites' })
});
res.end();
});
});
router.put('/:id/bookmarked', (req, res) => {
Favorite.findByIdAndUpdate(req.params.id, {
$set: { bookmarked: !!req.body.bookmarked }
}, { new: true }, (err, fav) => {
if (false) {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Favourite to bookmark not found');
} else if (!req.body.bookmarked) {
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.end('Bad PUT bookmark form parameters');
} else {
res.format({
html: () => renderFav(req, res, fav, false),
json: () => res.json(fav)
});
}
});
});
/** router for /root */
module.exports = router;