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 = {
name: req.body.name,
dataURL: req.body.dataURL,
bookmarked: req.body.bookmarked,
};
bookmarked: req.body.bookmarked
}
const favourite = new Favorite(data);
if (req.body._id) {
@ -45,8 +45,7 @@ router.post('/', (req, res) => {
}
res.status = 201;
const _id = fav._id;
renderFav(req, res, Object.assign({ _id }, data), false);
renderFav(req, res, fav, false);
});
});
@ -62,8 +61,9 @@ router.get('/search', (req, res) => {
findAndRender(filter, req, res);
});
router.get('/:id', (req, res) => {
Favorite.findById(parseId(req), (err, fav) => {
function findOne(id) {
return (req, res) => {
Favorite.findById(id(req), (err, fav) => {
if (err) {
return error(err, res);
}
@ -74,7 +74,10 @@ router.get('/:id', (req, res) => {
renderFav(req, res, fav, false);
});
});
};
}
router.get('/:id', findOne(req => parseId(req)));
function handleUpdate(partial = false) {
return (req, res) => {
@ -95,17 +98,21 @@ function handleUpdate(partial = false) {
}
Favorite.findByIdAndUpdate(parseId(req), { $set: edit }, {
new: true,
new: false,
upsert: true,
setDefaultsOnInsert: true,
passRawResult: true,
}, (err, fav) => {
if (err) {
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;
renderFav(req, res, fav, false);
});

View file

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