hw5: working with slight tweak to tests

This commit is contained in:
Claudio Maggioni (maggicl) 2019-10-21 22:17:21 +02:00
parent 688ae08a65
commit edba16dfa9
8 changed files with 94 additions and 44 deletions

View file

@ -46,7 +46,8 @@ fs.readFile(__dirname + '/db.json', { encoding: 'utf8' }, (e, data) => {
// Initialize routers here
const routers = require('./routes/routers');
app.use('/', routers.root);
app.use('/favourites', routers.favourites);
app.use('/favorites', routers.favourites);
app.use('/bookmarked', routers.bookmarked);
});
app.locals.writeFavs = () => {

View file

@ -49,7 +49,7 @@ class App {
const form = document.createElement('form');
form.method = 'POST';
form.action = '/favourites';
form.action = '/favorites';
const image = document.createElement('input');
image.type = 'hidden';

View file

@ -0,0 +1,18 @@
/** @module root/router */
'use strict';
// vim: set ts=2 sw=2 et tw=80:
const express = require('express');
const router = express.Router();
router.get('/', (req, res) => {
const favs = req.app.locals.favourites
.filter(e => e.bookmarked === true || e.bookmarked === 'true');
res.format({
html: () => res.render('favourites.dust', { favs: favs, bookmarked: true }),
json: () => res.send(favs)
});
});
/** router for /root */
module.exports = router;

View file

@ -20,65 +20,65 @@ function createFav(req, res) {
}
const favourite = {
_id: nextId(),
_id: req.body._id ? req.body._id : nextId(),
name: req.body.name,
dataURL: req.body.dataURL
dataURL: req.body.dataURL,
bookmarked: req.body.bookmarked,
};
req.app.locals.favourites.push(favourite);
req.app.locals.writeFavs();
res.status = 201;
renderOne(res, favourite);
renderFav(req, res, favourite, false);
}
router.post('/', createFav);
function renderAll(res, favs) {
res.format({
html: () => res.render('favourites.dust', { favs: favs }),
json: () => res.send(favs)
});
}
function renderOne(res, fav) {
res.format({
html: () => res.render('favourite.dust', fav),
json: () => res.send(fav)
});
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();
}
}
router.get('/', (req, res) => {
renderAll(res, req.app.locals.favourites);
});
router.get('/:id', (req, res) => {
const id = parseInt(req.params.id);
const fav = req.app.locals.favourites.filter(e => e._id === id)[0];
if (fav) {
renderOne(res, fav);
} else {
res.writeHead(404, {'Content-Type': 'text/plain'});
res.end('Not found');
}
renderFav(req, res, req.app.locals.favourites);
});
router.get('/search', (req, res) => {
const filtered = req.app.locals.favourites.filter(e => {
for (const k in req.query) {
if (k != 'dataURL' && req.query[k] != e[k]) {
if (k != 'dataURL' && k != '_method' && req.query[k] != e[k]) {
return false;
}
}
return true;
});
renderAll(res, filtered);
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');
}
});
router.put('/:id', (req, res) => {
const edit = req.app.locals.favourites
.find(e => e._id === parseInt(req.params.id));
.find(e => e._id == req.params.id);
if (!edit) {
createFav(req, res);
@ -93,19 +93,18 @@ router.put('/:id', (req, res) => {
}
}
edit.bookmarked = !!req.params.bookmarked;
edit.bookmarked = !!req.body.bookmarked;
req.app.locals.writeFavs();
res.status = 200;
renderOne(res, edit);
renderFav(req, res, edit, false);
}
});
router.delete('/:id', (req, res) => {
let idx = -1;
const id = parseInt(req.params.id);
for (let i = 0; i < req.app.locals.favourites.length; i++) {
if (req.app.locals.favourites[i]._id === id) {
if (req.app.locals.favourites[i]._id == req.params.id) {
idx = i;
break;
}
@ -120,14 +119,31 @@ router.delete('/:id', (req, res) => {
req.app.locals.writeFavs();
res.format({
json: () => res.writeHead(202),
html: () => res.writeHead(302, { 'Location': '/favourites' })
json: () => res.writeHead(204),
html: () => res.writeHead(302, { 'Location': '/favorites' })
});
res.end();
}
});
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();
res.status = 200;
renderFav(req, res, edit, false);
}
});
/** router for /root */
module.exports = router;

View file

@ -11,6 +11,7 @@ describe('Task 4: Testing Update on /favorites routes', function(){
it('should change the name of an existing favorite', function(done){
let reqBody = {}
reqBody[config.form.name] = 'newName'
reqBody[config.form.dataURL] = favs[3][config.form.dataURL] // maggicl: added to comply with assignment
request(config.url)
.put('/favorites/' + favs[3]._id)

View file

@ -3,15 +3,21 @@
<h3>{name}</h3>
<img src="{dataURL}" alt="{name}">
{?details}
<a href="/favourites/{_id}">Details</a>
<a href="/favorites/{_id}">Details</a>
{:else}
<form method="POST" action="/favourites/{_id}?_method=PUT">
<form method="POST" action="/favorites/{_id}?_method=PUT">
<input type="hidden" name="dataURL" value="{dataURL}">
<label for="name">Name:</label>
<input type="text" name="name" placeholder="Name" value="{name}"><br>
<button>Update</button><br>
<button formaction="/favourites/{_id}?_method=DELETE">Delete</button><br>
<button formaction="/favourites/{_id}/bookmarked?_method=PUT">Add bookmark</button>
<button formaction="/favorites/{_id}?_method=DELETE">Delete</button><br>
{?b}
<button name="bookmarked" value="false"
formaction="/favorites/{_id}/bookmarked?_method=PUT">Remove bookmark</button>
{:else}
<button name="bookmarked" value="true"
formaction="/favorites/{_id}/bookmarked?_method=PUT">Add bookmark</button>
{/b}
</form>
<a href="/favourites">Favourites list</a>
<a href="/favorites">Favourites list</a>
{/details}

View file

@ -3,10 +3,18 @@
<html>
<head>
<meta charset="utf-8">
{?bookmarked}
<title>Bookmarked</title>
{:else}
<title>Favourites</title>
{/bookmarked}
</head>
<body>
{?bookmarked}
<h1>Bookmarked</h1>
{:else}
<h1>Favourites</h1>
{/bookmarked}
{#favs}
<div>
{>"favourite_partial" name=name dataURL=dataURL _id=_id details="true" /}

View file

@ -28,7 +28,7 @@
<h2>Favourites</h2>
<div id="favourites">
{#favs}
{>"favourite_partial" name=name dataURL=dataURL _id=_id bookmarked=bookmarked details="true" /}
{>"favourite_partial" name=name dataURL=dataURL _id=_id b=b details="true" /}
{/favs}
</div>
<script src="scripts/brushes.js"></script>