hw5: mastery check edits

This commit is contained in:
Claudio Maggioni 2019-10-28 08:35:04 +01:00
parent edba16dfa9
commit f8d3a2a323
16 changed files with 80 additions and 42 deletions

BIN
.DS_Store vendored

Binary file not shown.

View file

@ -50,10 +50,18 @@ fs.readFile(__dirname + '/db.json', { encoding: 'utf8' }, (e, data) => {
app.use('/bookmarked', routers.bookmarked); app.use('/bookmarked', routers.bookmarked);
}); });
app.locals.writeFavs = () => { let writing = false;
app.locals.writeFavs = (done) => {
if (writing) {
setTimeout(() => app.locals.writeFavs(done), 100);
return;
}
writing = true;
fs.writeFile(__dirname + '/db.json', JSON.stringify(app.locals.favourites), fs.writeFile(__dirname + '/db.json', JSON.stringify(app.locals.favourites),
{ encoding: 'utf8' }, err => { { encoding: 'utf8' }, err => {
if (err) console.error(err); if (err) console.error(err);
writing = false;
done();
}); });
} }

View file

@ -0,0 +1,3 @@
# Bonuses implemented:
- *Excercise 6*, Bookmarks
- *Excercise 7*, Persistent favourites

View file

@ -8,8 +8,9 @@ const router = express.Router();
let id = 1; let id = 1;
function nextId() { function nextId(favs) {
return id++; while (favs.some((newId => e => e._id == newId)(++id)));
return id;
} }
function createFav(req, res) { function createFav(req, res) {
@ -20,26 +21,39 @@ function createFav(req, res) {
} }
const favourite = { const favourite = {
_id: req.body._id ? req.body._id : nextId(), _id: req.body._id ? req.body._id : nextId(req.app.locals.favourites),
name: req.body.name, name: req.body.name,
dataURL: req.body.dataURL, dataURL: req.body.dataURL,
bookmarked: req.body.bookmarked, 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;
renderFav(req, res, favourite, false); renderFav(req, res, favourite, false);
});
} }
router.post('/', createFav); router.post('/', createFav);
function renderFav(req, res, favs, list = true) { function renderFav(req, res, favs, list = true) {
if (req.accepts('html')) { if (req.accepts('html')) {
res.render(list ? 'favourites.dust' : 'favourite.dust', let ctx;
list ? { favs: favs } : Object.assign({b: favs.bookmarked === 'true' || if (list) {
favs.bookmarked === true}, favs)); const f = [];
for (const e of favs) {
f.push(Object.assign({
b: e.bookmarked === 'true' || e.bookmarked === true
}, e));
}
ctx = {favs: f};
} else {
ctx = Object.assign({
b: favs.bookmarked === 'true' || favs.bookmarked === true
}, favs);
}
res.render(list ? 'favourites.dust' : 'favourite.dust', ctx);
} else if (req.accepts('json')) { } else if (req.accepts('json')) {
res.json(favs); res.json(favs);
} else { } else {
@ -76,7 +90,8 @@ router.get('/:id', (req, res) => {
} }
}); });
router.put('/:id', (req, res) => { function handleUpdate(partial = false) {
return (req, res) => {
const edit = req.app.locals.favourites const edit = req.app.locals.favourites
.find(e => e._id == req.params.id); .find(e => e._id == req.params.id);
@ -86,20 +101,27 @@ router.put('/:id', (req, res) => {
for (const key of ['dataURL', 'name']) { for (const key of ['dataURL', 'name']) {
if (req.body[key]) { if (req.body[key]) {
edit[key] = req.body[key]; edit[key] = req.body[key];
} else { } else if (!partial) {
res.writeHead(400, { 'Content-Type': 'text/plain' }); res.writeHead(400, { 'Content-Type': 'text/plain' });
res.end('Bad PUT form parameters'); res.end('Bad PUT form parameters');
return; return;
} }
} }
edit.bookmarked = !!req.body.bookmarked; if (req.body.bookmarked !== undefined) {
req.app.locals.writeFavs(); edit.bookmarked = req.body.bookmarked;
}
req.app.locals.writeFavs(() => {
res.status = 200; res.status = 200;
renderFav(req, res, edit, false); renderFav(req, res, edit, false);
}
}); });
}
};
}
router.put('/:id', handleUpdate());
router.patch('/:id', handleUpdate(true));
router.delete('/:id', (req, res) => { router.delete('/:id', (req, res) => {
let idx = -1; let idx = -1;
@ -116,13 +138,13 @@ router.delete('/:id', (req, res) => {
return; return;
} else { } else {
req.app.locals.favourites.splice(idx, 1); req.app.locals.favourites.splice(idx, 1);
req.app.locals.writeFavs(); req.app.locals.writeFavs(() => {
res.format({ res.format({
json: () => res.writeHead(204), json: () => res.writeHead(204),
html: () => res.writeHead(302, { 'Location': '/favorites' }) html: () => res.writeHead(302, { 'Location': '/favorites' })
}); });
res.end(); res.end();
});
} }
}); });
@ -138,10 +160,10 @@ router.put('/:id/bookmarked', (req, res) => {
res.end('Bad PUT bookmark form parameters'); res.end('Bad PUT bookmark form parameters');
} else { } else {
edit.bookmarked = req.body.bookmarked; edit.bookmarked = req.body.bookmarked;
req.app.locals.writeFavs(); req.app.locals.writeFavs(() => {
res.status = 200; res.status = 200;
renderFav(req, res, edit, false); renderFav(req, res, edit, false);
});
} }
}); });

View file

@ -2,6 +2,11 @@
<h3>{name}</h3> <h3>{name}</h3>
<img src="{dataURL}" alt="{name}"> <img src="{dataURL}" alt="{name}">
{?b}
<p>
<strong>Bookmarked</strong>
</p>
{/b}
{?details} {?details}
<a href="/favorites/{_id}">Details</a> <a href="/favorites/{_id}">Details</a>
{:else} {:else}

View file

@ -17,7 +17,7 @@
{/bookmarked} {/bookmarked}
{#favs} {#favs}
<div> <div>
{>"favourite_partial" name=name dataURL=dataURL _id=_id details="true" /} {>"favourite_partial" name=name dataURL=dataURL _id=_id b=b details="true" /}
</div> </div>
{:else} {:else}
<strong>No favourites.</strong> <strong>No favourites.</strong>