hw5: mastery check edits
This commit is contained in:
parent
edba16dfa9
commit
f8d3a2a323
16 changed files with 80 additions and 42 deletions
BIN
.DS_Store
vendored
BIN
.DS_Store
vendored
Binary file not shown.
|
@ -50,10 +50,18 @@ fs.readFile(__dirname + '/db.json', { encoding: 'utf8' }, (e, data) => {
|
|||
app.use('/bookmarked', routers.bookmarked);
|
||||
});
|
||||
|
||||
app.locals.writeFavs = () => {
|
||||
fs.writeFile(__dirname + '/db.json', JSON.stringify(app.locals.favourites),
|
||||
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();
|
||||
});
|
||||
}
|
||||
|
||||
|
|
3
hw5/Claudio_Maggioni/readme.md
Normal file
3
hw5/Claudio_Maggioni/readme.md
Normal file
|
@ -0,0 +1,3 @@
|
|||
# Bonuses implemented:
|
||||
- *Excercise 6*, Bookmarks
|
||||
- *Excercise 7*, Persistent favourites
|
|
@ -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();
|
||||
|
||||
res.status = 201;
|
||||
renderFav(req, res, favourite, false);
|
||||
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,30 +90,38 @@ router.get('/:id', (req, res) => {
|
|||
}
|
||||
});
|
||||
|
||||
router.put('/:id', (req, res) => {
|
||||
const edit = req.app.locals.favourites
|
||||
.find(e => e._id == req.params.id);
|
||||
function handleUpdate(partial = false) {
|
||||
return (req, res) => {
|
||||
const edit = req.app.locals.favourites
|
||||
.find(e => e._id == req.params.id);
|
||||
|
||||
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;
|
||||
if (!edit) {
|
||||
createFav(req, res);
|
||||
} else {
|
||||
for (const key of ['dataURL', 'name']) {
|
||||
if (req.body[key]) {
|
||||
edit[key] = req.body[key];
|
||||
} else if (!partial) {
|
||||
res.writeHead(400, { 'Content-Type': 'text/plain' });
|
||||
res.end('Bad PUT form parameters');
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (req.body.bookmarked !== undefined) {
|
||||
edit.bookmarked = req.body.bookmarked;
|
||||
}
|
||||
req.app.locals.writeFavs(() => {
|
||||
res.status = 200;
|
||||
renderFav(req, res, edit, false);
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
edit.bookmarked = !!req.body.bookmarked;
|
||||
req.app.locals.writeFavs();
|
||||
router.put('/:id', handleUpdate());
|
||||
router.patch('/:id', handleUpdate(true));
|
||||
|
||||
res.status = 200;
|
||||
renderFav(req, res, edit, false);
|
||||
}
|
||||
});
|
||||
|
||||
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();
|
||||
|
||||
res.format({
|
||||
json: () => res.writeHead(204),
|
||||
html: () => res.writeHead(302, { 'Location': '/favorites' })
|
||||
req.app.locals.writeFavs(() => {
|
||||
res.format({
|
||||
json: () => res.writeHead(204),
|
||||
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');
|
||||
} else {
|
||||
edit.bookmarked = req.body.bookmarked;
|
||||
req.app.locals.writeFavs();
|
||||
|
||||
res.status = 200;
|
||||
renderFav(req, res, edit, false);
|
||||
req.app.locals.writeFavs(() => {
|
||||
res.status = 200;
|
||||
renderFav(req, res, edit, false);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
@ -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}
|
||||
|
|
|
@ -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>
|
||||
|
|
Reference in a new issue