2019-10-21 16:56:21 +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();
|
|
|
|
|
|
|
|
let id = 1;
|
|
|
|
|
|
|
|
function nextId() {
|
|
|
|
return id++;
|
|
|
|
}
|
|
|
|
|
|
|
|
function createFav(req, res) {
|
|
|
|
if (!req.body.name || !req.body.dataURL) {
|
|
|
|
res.writeHead(400, { 'Content-Type': 'text/plain' });
|
|
|
|
res.end('Bad create form parameters');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const favourite = {
|
2019-10-21 20:17:21 +00:00
|
|
|
_id: req.body._id ? req.body._id : nextId(),
|
2019-10-21 16:56:21 +00:00
|
|
|
name: req.body.name,
|
2019-10-21 20:17:21 +00:00
|
|
|
dataURL: req.body.dataURL,
|
|
|
|
bookmarked: req.body.bookmarked,
|
2019-10-21 16:56:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
req.app.locals.favourites.push(favourite);
|
|
|
|
req.app.locals.writeFavs();
|
|
|
|
|
|
|
|
res.status = 201;
|
2019-10-21 20:17:21 +00:00
|
|
|
renderFav(req, res, favourite, false);
|
2019-10-21 16:56:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
router.post('/', createFav);
|
|
|
|
|
2019-10-21 20:17:21 +00:00
|
|
|
function renderFav(req, res, favs, list = true) {
|
|
|
|
if (req.accepts('html')) {
|
|
|
|
res.render(list ? 'favourites.dust' : 'favourite.dust',
|
|
|
|
list ? { favs: favs } : Object.assign({b: favs.bookmarked === 'true' ||
|
|
|
|
favs.bookmarked === true}, favs));
|
|
|
|
} else if (req.accepts('json')) {
|
|
|
|
res.json(favs);
|
|
|
|
} else {
|
|
|
|
res.writeHead(406);
|
|
|
|
res.end();
|
|
|
|
}
|
2019-10-21 16:56:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
router.get('/', (req, res) => {
|
2019-10-21 20:17:21 +00:00
|
|
|
renderFav(req, res, req.app.locals.favourites);
|
2019-10-21 16:56:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
router.get('/search', (req, res) => {
|
|
|
|
const filtered = req.app.locals.favourites.filter(e => {
|
|
|
|
for (const k in req.query) {
|
2019-10-21 20:17:21 +00:00
|
|
|
if (k != 'dataURL' && k != '_method' && req.query[k] != e[k]) {
|
2019-10-21 16:56:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2019-10-21 20:17:21 +00:00
|
|
|
renderFav(req, res, filtered);
|
|
|
|
});
|
|
|
|
|
|
|
|
router.get('/:id', (req, res) => {
|
|
|
|
const fav = req.app.locals.favourites
|
|
|
|
.filter(e => e._id == req.params.id)[0];
|
|
|
|
if (fav) {
|
|
|
|
renderFav(req, res, fav, false);
|
|
|
|
} else {
|
|
|
|
res.writeHead(404, {'Content-Type': 'text/plain'});
|
|
|
|
res.end('Not found');
|
|
|
|
}
|
2019-10-21 16:56:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
router.put('/:id', (req, res) => {
|
|
|
|
const edit = req.app.locals.favourites
|
2019-10-21 20:17:21 +00:00
|
|
|
.find(e => e._id == req.params.id);
|
2019-10-21 16:56:21 +00:00
|
|
|
|
|
|
|
if (!edit) {
|
|
|
|
createFav(req, res);
|
|
|
|
} else {
|
|
|
|
for (const key of ['dataURL', 'name']) {
|
|
|
|
if (req.body[key]) {
|
|
|
|
edit[key] = req.body[key];
|
|
|
|
} else {
|
|
|
|
res.writeHead(400, { 'Content-Type': 'text/plain' });
|
|
|
|
res.end('Bad PUT form parameters');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-21 20:17:21 +00:00
|
|
|
edit.bookmarked = !!req.body.bookmarked;
|
2019-10-21 16:56:21 +00:00
|
|
|
req.app.locals.writeFavs();
|
|
|
|
|
|
|
|
res.status = 200;
|
2019-10-21 20:17:21 +00:00
|
|
|
renderFav(req, res, edit, false);
|
2019-10-21 16:56:21 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
router.delete('/:id', (req, res) => {
|
|
|
|
let idx = -1;
|
|
|
|
for (let i = 0; i < req.app.locals.favourites.length; i++) {
|
2019-10-21 20:17:21 +00:00
|
|
|
if (req.app.locals.favourites[i]._id == req.params.id) {
|
2019-10-21 16:56:21 +00:00
|
|
|
idx = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (idx == -1) {
|
|
|
|
res.writeHead(404, { 'Content-Type': 'text/plain' });
|
|
|
|
res.end('Favourite not found');
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
req.app.locals.favourites.splice(idx, 1);
|
|
|
|
req.app.locals.writeFavs();
|
|
|
|
|
|
|
|
res.format({
|
2019-10-21 20:17:21 +00:00
|
|
|
json: () => res.writeHead(204),
|
|
|
|
html: () => res.writeHead(302, { 'Location': '/favorites' })
|
2019-10-21 16:56:21 +00:00
|
|
|
});
|
|
|
|
res.end();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-10-21 20:17:21 +00:00
|
|
|
router.put('/:id/bookmarked', (req, res) => {
|
|
|
|
const edit = req.app.locals.favourites
|
|
|
|
.find(e => e._id == req.params.id);
|
|
|
|
|
|
|
|
if (!edit) {
|
|
|
|
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 {
|
|
|
|
edit.bookmarked = req.body.bookmarked;
|
|
|
|
req.app.locals.writeFavs();
|
2019-10-21 16:56:21 +00:00
|
|
|
|
2019-10-21 20:17:21 +00:00
|
|
|
res.status = 200;
|
|
|
|
renderFav(req, res, edit, false);
|
|
|
|
}
|
|
|
|
});
|
2019-10-21 16:56:21 +00:00
|
|
|
|
|
|
|
/** router for /root */
|
|
|
|
module.exports = router;
|