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

View file

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

View file

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