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 // Initialize routers here
const routers = require('./routes/routers'); const routers = require('./routes/routers');
app.use('/', routers.root); app.use('/', routers.root);
app.use('/favourites', routers.favourites); app.use('/favorites', routers.favourites);
app.use('/bookmarked', routers.bookmarked);
}); });
app.locals.writeFavs = () => { app.locals.writeFavs = () => {

View file

@ -49,7 +49,7 @@ class App {
const form = document.createElement('form'); const form = document.createElement('form');
form.method = 'POST'; form.method = 'POST';
form.action = '/favourites'; form.action = '/favorites';
const image = document.createElement('input'); const image = document.createElement('input');
image.type = 'hidden'; 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 = { const favourite = {
_id: nextId(), _id: req.body._id ? req.body._id : nextId(),
name: req.body.name, 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.favourites.push(favourite);
req.app.locals.writeFavs(); req.app.locals.writeFavs();
res.status = 201; res.status = 201;
renderOne(res, favourite); renderFav(req, res, favourite, false);
} }
router.post('/', createFav); router.post('/', createFav);
function renderAll(res, favs) { function renderFav(req, res, favs, list = true) {
res.format({ if (req.accepts('html')) {
html: () => res.render('favourites.dust', { favs: favs }), res.render(list ? 'favourites.dust' : 'favourite.dust',
json: () => res.send(favs) list ? { favs: favs } : Object.assign({b: favs.bookmarked === 'true' ||
}); favs.bookmarked === true}, favs));
} } else if (req.accepts('json')) {
res.json(favs);
function renderOne(res, fav) { } else {
res.format({ res.writeHead(406);
html: () => res.render('favourite.dust', fav), res.end();
json: () => res.send(fav) }
});
} }
router.get('/', (req, res) => { router.get('/', (req, res) => {
renderAll(res, req.app.locals.favourites); renderFav(req, 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');
}
}); });
router.get('/search', (req, res) => { router.get('/search', (req, res) => {
const filtered = req.app.locals.favourites.filter(e => { const filtered = req.app.locals.favourites.filter(e => {
for (const k in req.query) { 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 false;
} }
} }
return true; 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) => { router.put('/:id', (req, res) => {
const edit = req.app.locals.favourites const edit = req.app.locals.favourites
.find(e => e._id === parseInt(req.params.id)); .find(e => e._id == req.params.id);
if (!edit) { if (!edit) {
createFav(req, res); 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(); req.app.locals.writeFavs();
res.status = 200; res.status = 200;
renderOne(res, edit); renderFav(req, res, edit, false);
} }
}); });
router.delete('/:id', (req, res) => { router.delete('/:id', (req, res) => {
let idx = -1; let idx = -1;
const id = parseInt(req.params.id);
for (let i = 0; i < req.app.locals.favourites.length; i++) { 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; idx = i;
break; break;
} }
@ -120,14 +119,31 @@ router.delete('/:id', (req, res) => {
req.app.locals.writeFavs(); req.app.locals.writeFavs();
res.format({ res.format({
json: () => res.writeHead(202), json: () => res.writeHead(204),
html: () => res.writeHead(302, { 'Location': '/favourites' }) html: () => res.writeHead(302, { 'Location': '/favorites' })
}); });
res.end(); 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 */ /** router for /root */
module.exports = router; 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){ it('should change the name of an existing favorite', function(done){
let reqBody = {} let reqBody = {}
reqBody[config.form.name] = 'newName' reqBody[config.form.name] = 'newName'
reqBody[config.form.dataURL] = favs[3][config.form.dataURL] // maggicl: added to comply with assignment
request(config.url) request(config.url)
.put('/favorites/' + favs[3]._id) .put('/favorites/' + favs[3]._id)

View file

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

View file

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

View file

@ -28,7 +28,7 @@
<h2>Favourites</h2> <h2>Favourites</h2>
<div id="favourites"> <div id="favourites">
{#favs} {#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} {/favs}
</div> </div>
<script src="scripts/brushes.js"></script> <script src="scripts/brushes.js"></script>