hw6: Fixed 201 (to test)

This commit is contained in:
Claudio Maggioni 2019-10-28 22:45:15 +01:00
parent ab8c9c9668
commit 9d82eaaad2
2 changed files with 44 additions and 31 deletions

View file

@ -29,8 +29,8 @@ router.post('/', (req, res) => {
const data = { const data = {
name: req.body.name, name: req.body.name,
dataURL: req.body.dataURL, dataURL: req.body.dataURL,
bookmarked: req.body.bookmarked, bookmarked: req.body.bookmarked
}; }
const favourite = new Favorite(data); const favourite = new Favorite(data);
if (req.body._id) { if (req.body._id) {
@ -45,8 +45,7 @@ router.post('/', (req, res) => {
} }
res.status = 201; res.status = 201;
const _id = fav._id; renderFav(req, res, fav, false);
renderFav(req, res, Object.assign({ _id }, data), false);
}); });
}); });
@ -62,19 +61,23 @@ router.get('/search', (req, res) => {
findAndRender(filter, req, res); findAndRender(filter, req, res);
}); });
router.get('/:id', (req, res) => { function findOne(id) {
Favorite.findById(parseId(req), (err, fav) => { return (req, res) => {
if (err) { Favorite.findById(id(req), (err, fav) => {
return error(err, res); if (err) {
} return error(err, res);
}
if (notFound(fav, res)) { if (notFound(fav, res)) {
return; return;
} }
renderFav(req, res, fav, false); renderFav(req, res, fav, false);
}); });
}); };
}
router.get('/:id', findOne(req => parseId(req)));
function handleUpdate(partial = false) { function handleUpdate(partial = false) {
return (req, res) => { return (req, res) => {
@ -95,17 +98,21 @@ function handleUpdate(partial = false) {
} }
Favorite.findByIdAndUpdate(parseId(req), { $set: edit }, { Favorite.findByIdAndUpdate(parseId(req), { $set: edit }, {
new: true, new: false,
upsert: true, upsert: true,
setDefaultsOnInsert: true, setDefaultsOnInsert: true,
passRawResult: true,
}, (err, fav) => { }, (err, fav) => {
if (err) { if (err) {
return error(err, res); return error(err, res);
} }
console.log(arguments); if (fav == null) {
res.status = 201;
findOne(() => parseId(req))(req, res);
return;
}
// FIXME: return 201 on creation
res.status = 200; res.status = 200;
renderFav(req, res, fav, false); renderFav(req, res, fav, false);
}); });

View file

@ -39,8 +39,7 @@ router.post('/', (req, res) => {
catchErrs(favourite.save(), res).then(fav => { catchErrs(favourite.save(), res).then(fav => {
res.status = 201; res.status = 201;
const _id = fav._id; renderFav(req, res, fav, false);
renderFav(req, res, Object.assign({ _id }, data), false);
}); });
}); });
@ -56,15 +55,19 @@ router.get('/search', (req, res) => {
findAndRender(filter, req, res); findAndRender(filter, req, res);
}); });
router.get('/:id', (req, res) => { function findOne(id) {
catchErrs(Favorite.findById(parseId(req)), res).then(fav => { return (req, res) => {
if (notFound(fav, res)) { catchErrs(Favorite.findById(id(req)), res).then(fav => {
return; if (notFound(fav, res)) {
} return;
}
renderFav(req, res, fav, false); renderFav(req, res, fav, false);
}); });
}); };
}
router.get('/:id', findOne(req => parseId(req)));
function handleUpdate(partial = false) { function handleUpdate(partial = false) {
return (req, res) => { return (req, res) => {
@ -84,13 +87,16 @@ function handleUpdate(partial = false) {
} }
catchErrs(Favorite.findByIdAndUpdate(parseId(req), { $set: edit }, { catchErrs(Favorite.findByIdAndUpdate(parseId(req), { $set: edit }, {
new: true, new: false,
upsert: true, upsert: true,
setDefaultsOnInsert: true, setDefaultsOnInsert: true,
}), res).then(fav => { }), res).then(fav => {
console.log(arguments); if (fav == null) {
res.status = 201;
findOne(() => parseId(req))(req, res);
return;
}
// FIXME: return 201 on creation
res.status = 200; res.status = 200;
renderFav(req, res, fav, false); renderFav(req, res, fav, false);
}); });