refactor: switch to async/await syntax in track routes

This commit is contained in:
Paul Bienkowski 2020-11-18 23:00:08 +01:00
parent 114a9677f1
commit f890fc673f
2 changed files with 348 additions and 378 deletions

View file

@ -8,7 +8,7 @@
"start": "node ./app.js", "start": "node ./app.js",
"dev": "nodemon ./app.js", "dev": "nodemon ./app.js",
"mongo:stop": "docker stop realworld-mongo && docker rm realworld-mongo", "mongo:stop": "docker stop realworld-mongo && docker rm realworld-mongo",
"autoformat": "eslint --fix logic", "autoformat": "eslint --fix logic routes/api/tracks.js",
"test": "jest" "test": "jest"
}, },
"repository": { "repository": {

View file

@ -9,313 +9,286 @@ const currentTracks = new Map();
const TrackInfo = require('../../logic/TrackInfo'); const TrackInfo = require('../../logic/TrackInfo');
const { addPointsToTrack } = require('../../logic/tracks'); const { addPointsToTrack } = require('../../logic/tracks');
const wrapRoute = (fn) => async (req, res, next) => {
try {
return await fn(req, res);
} catch (err) {
next(err);
}
};
// Preload track objects on routes with ':track' // Preload track objects on routes with ':track'
router.param('track', function (req, res, next, slug) { router.param('track', async (req, res, next, slug) => {
Track.findOne({ slug: slug }) try {
.populate('author') const track = await Track.findOne({ slug }).populate('author');
.then(function (track) {
if (!track) {
return res.sendStatus(404);
}
req.track = track; if (!track) {
return res.sendStatus(404);
}
return next(); req.track = track;
})
.catch(next); return next();
} catch (err) {
return next(err);
}
}); });
router.param('comment', function (req, res, next, id) { router.param('comment', async (req, res, next, id) => {
Comment.findById(id) try {
.then(function (comment) { const comment = await Comment.findById(id);
if (!comment) {
return res.sendStatus(404);
}
req.comment = comment; if (!comment) {
return res.sendStatus(404);
}
return next(); req.comment = comment;
})
.catch(next); return next();
} catch (err) {
return next(err);
}
}); });
router.get('/', auth.optional, function (req, res, next) { router.get(
const query = {}; '/',
let limit = 20; auth.optional,
let offset = 0; wrapRoute(async (req, res) => {
const query = {};
let limit = 20;
let offset = 0;
if (typeof req.query.limit !== 'undefined') { if (typeof req.query.limit !== 'undefined') {
limit = req.query.limit; limit = req.query.limit;
} }
if (typeof req.query.offset !== 'undefined') { if (typeof req.query.offset !== 'undefined') {
offset = req.query.offset; offset = req.query.offset;
} }
if (typeof req.query.tag !== 'undefined') { if (typeof req.query.tag !== 'undefined') {
query.tagList = { $in: [req.query.tag] }; query.tagList = { $in: [req.query.tag] };
} }
Promise.all([ const [author, favoriter] = await Promise.all([
req.query.author ? User.findOne({ username: req.query.author }) : null, req.query.author ? User.findOne({ username: req.query.author }) : null,
req.query.favorited ? User.findOne({ username: req.query.favorited }) : null, req.query.favorited ? User.findOne({ username: req.query.favorited }) : null,
]) ]);
.then(function (results) {
const author = results[0];
const favoriter = results[1];
if (author) { if (author) {
query.author = author._id; query.author = author._id;
} }
if (favoriter) { if (favoriter) {
query._id = { $in: favoriter.favorites }; query._id = { $in: favoriter.favorites };
} else if (req.query.favorited) { } else if (req.query.favorited) {
query._id = { $in: [] }; query._id = { $in: [] };
} }
return Promise.all([ const results = await Promise.all([
Track.find(query) Track.find(query)
.limit(Number(limit)) .limit(Number(limit))
.skip(Number(offset)) .skip(Number(offset))
.sort({ createdAt: 'desc' }) .sort({ createdAt: 'desc' })
.populate('author') .populate('author')
.where('visible') .where('visible')
.equals(true) .equals(true)
.exec(), .exec(),
Track.countDocuments(query).exec(), Track.countDocuments(query).exec(),
req.payload ? User.findById(req.payload.id) : null, req.payload ? User.findById(req.payload.id) : null,
]).then(function (results) { ]);
const tracks = results[0];
// const tracksCount = results[1];
const user = results[2];
// console.log(tracks);
const retTracks = [];
for (const t of tracks) {
// console.log(t);
// if (t.author.areTracksVisibleForAll || t.author == user) {
retTracks.push(t);
// }
}
return res.json({
tracks: retTracks.map(function (track) {
return track.toJSONFor(user);
}),
tracksCount: retTracks.length,
});
});
})
.catch(next);
});
router.get('/feed', auth.required, function (req, res, next) { const [tracks, tracksCount, user] = results;
let limit = 20;
let offset = 0;
if (typeof req.query.limit !== 'undefined') { return res.json({
limit = req.query.limit; tracks: tracks.map((track) => track.toJSONFor(user)),
} tracksCount,
});
}),
);
if (typeof req.query.offset !== 'undefined') { router.get(
offset = req.query.offset; '/feed',
} auth.required,
wrapRoute(async (req, res) => {
let limit = 20;
let offset = 0;
if (typeof req.query.limit !== 'undefined') {
limit = req.query.limit;
}
if (typeof req.query.offset !== 'undefined') {
offset = req.query.offset;
}
const user = await User.findById(req.payload.id);
User.findById(req.payload.id).then(function (user) {
if (!user) { if (!user) {
return res.sendStatus(401); return res.sendStatus(401);
} }
if (user.following !== '') {
Promise.all([
Track.find({ author: { $in: user.following } })
.limit(Number(limit))
.skip(Number(offset))
.populate('author')
.exec(),
Track.countDocuments({ author: { $in: user.following } }),
])
.then(function (results) {
const tracks = results[0];
const tracksCount = results[1];
return res.json({ const showByUserIds = [req.payload.id, ...(user.following || [])];
tracks: tracks.map(function (track) {
return track.toJSONFor(user);
}),
tracksCount: tracksCount,
});
})
.catch(next);
} else {
Promise.all([
Track.find({ author: { $in: req.payload.id } })
.limit(Number(limit))
.skip(Number(offset))
.populate('author')
.exec(),
Track.countDocuments({ author: { $in: req.payload.id } }),
])
.then(function (results) {
const tracks = results[0];
const tracksCount = results[1];
return res.json({ const [tracks, tracksCount] = await Promise.all([
tracks: tracks.map(function (track) { Track.find({ author: { $in: showByUserIds } })
return track.toJSONFor(user); .limit(Number(limit))
}), .skip(Number(offset))
tracksCount: tracksCount, .populate('author')
}); .exec(),
}) Track.countDocuments({ author: { $in: showByUserIds } }),
.catch(next); ]);
return res.json({
tracks: tracks.map(function (track) {
return track.toJSONFor(user);
}),
tracksCount: tracksCount,
});
}),
);
router.post(
'/',
auth.required,
wrapRoute(async (req, res) => {
const user = await User.findById(req.payload.id);
if (!user) {
return res.sendStatus(401);
} }
});
});
router.post('/', auth.required, function (req, res, next) { const track = new Track(req.body.track);
User.findById(req.payload.id) const trackData = new TrackData();
.then(function (user) { track.trackData = trackData._id;
if (!user) {
return res.sendStatus(401);
}
const track = new Track(req.body.track); track.author = user;
const trackData = new TrackData(); track.visible = track.author.areTracksVisibleForAll;
track.trackData = trackData._id; await trackData.save();
track.author = user; await track.save();
track.visible = track.author.areTracksVisibleForAll;
trackData.save(function (err) {
if (err) {
console.log('failed to save trackData');
}
});
return track.save().then(function () { // console.log(track.author);
// console.log(track.author); return res.json({ track: track.toJSONFor(user) });
return res.json({ track: track.toJSONFor(user) }); }),
}); );
})
.catch(next);
});
router.post('/add', auth.optional, function (req, res, next) { router.post(
// console.log("Add"); '/add',
auth.optional,
wrapRoute(async (req, res) => {
// console.log("Add");
// console.log(req.payload); // console.log(req.payload);
User.findById(req.body.id) const user = await User.findById(req.body.id);
.then(function (user) {
if (!user) {
return res.sendStatus(401);
}
let ti = null; if (!user) {
if (currentTracks.has(req.body.id)) ti = currentTracks.get(req.body.id); return res.sendStatus(401);
}
// console.log("TI" + ti); let ti = null;
// console.log("TILen" + ti.trackData.points.length); if (currentTracks.has(req.body.id)) ti = currentTracks.get(req.body.id);
// console.log("TITrack" + ti.track);
// console.log("Body" + req.body.track.body);
if (ti.track) {
addPointsToTrack(ti, req.body.track.body);
// console.log("TLen" + ti.trackData.points.length);
ti.track.author = user;
}
// return track.save().then(function(){ // console.log("TI" + ti);
// console.log(track.author); // console.log("TILen" + ti.trackData.points.length);
// return res.json({ track: track.toJSONFor(user) }); // console.log("TITrack" + ti.track);
return res.sendStatus(200); // console.log("Body" + req.body.track.body);
// }); if (ti.track) {
}) addPointsToTrack(ti, req.body.track.body);
.catch(next);
});
router.post('/begin', auth.optional, function (req, res, next) {
// console.log("Begin");
// console.log(req.payload);
User.findById(req.body.id)
.then(function (user) {
if (!user) {
return res.sendStatus(401);
}
if (currentTracks.has(req.body.id)) currentTracks.delete(req.body.id); // delete old parts if there are leftovers
const ti = new TrackInfo(new Track(req.body.track), new TrackData());
ti.track.trackData = ti.trackData._id;
currentTracks.set(req.body.id, ti);
// console.log("addToTrack"+req.body);
addPointsToTrack(ti, ti.track.body);
// console.log("TLen" + ti.track);
// console.log("TLen" + ti.trackData);
// console.log("TLen" + ti.trackData.points.length); // console.log("TLen" + ti.trackData.points.length);
// console.log(track.trackData.points[0].date);
ti.track.author = user; ti.track.author = user;
}
// return track.save().then(function () { // await track.save()
// console.log(track.author); // return res.json({ track: track.toJSONFor(user) });
return res.sendStatus(200); return res.sendStatus(200);
// }); }),
}) );
.catch(next);
});
router.post('/end', auth.optional, function (req, res, next) { router.post(
// console.log("End"); '/begin',
// console.log(req.payload); auth.optional,
User.findById(req.body.id) wrapRoute(async (req, res) => {
.then(function (user) { // console.log("Begin");
if (!user) { // console.log(req.payload);
return res.sendStatus(401); const user = await User.findById(req.body.id);
}
let ti; if (!user) {
if (currentTracks.has(req.body.id)) { return res.sendStatus(401);
ti = currentTracks.get(req.body.id); }
addPointsToTrack(ti, req.body.track.body);
} else {
ti = new TrackInfo(new Track(req.body.track), new TrackData());
ti.track.trackData = ti.trackData._id;
addPointsToTrack(ti, ti.track.body);
}
if (ti.track) {
ti.track.author = user;
}
currentTracks.delete(req.body.id); // we are done with this track, it is complete if (currentTracks.has(req.body.id)) currentTracks.delete(req.body.id); // delete old parts if there are leftovers
const ti = new TrackInfo(new Track(req.body.track), new TrackData());
ti.track.trackData = ti.trackData._id;
currentTracks.set(req.body.id, ti);
// console.log("addToTrack"+req.body);
addPointsToTrack(ti, ti.track.body);
// console.log("TLen" + ti.track);
// console.log("TLen" + ti.trackData);
// console.log("TLen" + ti.trackData.points.length);
// console.log(track.trackData.points[0].date);
ti.track.author = user;
// await track.save()
// console.log(track.author);
return res.sendStatus(200);
}),
);
router.post(
'/end',
auth.optional,
wrapRoute(async (req, res) => {
const user = await User.findById(req.body.id);
if (!user) {
return res.sendStatus(401);
}
let ti;
if (currentTracks.has(req.body.id)) {
ti = currentTracks.get(req.body.id);
addPointsToTrack(ti, req.body.track.body);
} else {
ti = new TrackInfo(new Track(req.body.track), new TrackData());
ti.track.trackData = ti.trackData._id;
addPointsToTrack(ti, ti.track.body);
}
if (ti.track) {
ti.track.author = user; ti.track.author = user;
}
// console.log(track); currentTracks.delete(req.body.id); // we are done with this track, it is complete
// console.log("user:"+user); ti.track.author = user;
return ti.track.save().then(function () {
// console.log("TLen" + ti.track);
// console.log("TLen" + ti.trackData);
// console.log("TLen" + ti.trackData.points.length);
ti.trackData.save(function (err) {
if (err) {
console.log('failed to save trackData' + err.toString());
}
});
console.log('successfulSave:'); // console.log(track);
return res.sendStatus(200); // console.log("user:"+user);
}); await ti.track.save();
})
.catch(next); // console.log("TLen" + ti.track);
}); // console.log("TLen" + ti.trackData);
// console.log("TLen" + ti.trackData.points.length);
await ti.trackData.save();
return res.sendStatus(200);
}),
);
// return a track // return a track
router.get('/:track', auth.optional, function (req, res, next) { router.get(
Promise.all([req.payload ? User.findById(req.payload.id) : null, req.track.populate('author').execPopulate()]) '/:track',
.then(function (results) { auth.optional,
const user = results[0]; wrapRoute(async (req, res) => {
const [user] = await Promise.all([
return res.json({ track: req.track.toJSONFor(user, { body: true }) }); req.payload ? User.findById(req.payload.id) : null,
}) req.track.populate('author').execPopulate(),
.catch(next); ]);
}); return res.json({ track: req.track.toJSONFor(user, { body: true }) });
}),
);
// update track // update track
router.put('/:track', auth.required, async function (req, res, next) { router.put('/:track', auth.required, async function (req, res, next) {
@ -356,140 +329,137 @@ router.put('/:track', auth.required, async function (req, res, next) {
}); });
// delete track // delete track
router.delete('/:track', auth.required, function (req, res, next) { router.delete(
User.findById(req.payload.id) '/:track',
.then(function (user) { auth.required,
if (!user) { wrapRoute(async (req, res) => {
return res.sendStatus(401); const user = await User.findById(req.payload.id);
} if (!user) {
if (req.track.author._id.toString() === req.payload.id.toString()) { return res.sendStatus(401);
TrackData.findByIdAndDelete(req.track.trackData, function (err, td) { }
console.log('doneDelete'); if (req.track.author._id.toString() === req.payload.id.toString()) {
}); // delet our track data await TrackData.findByIdAndDelete(req.track.trackData);
return req.track.remove().then(function () { await req.track.remove();
return res.sendStatus(204); return res.sendStatus(204);
}); } else {
} else { return res.sendStatus(403);
return res.sendStatus(403); }
} }),
}) );
.catch(next);
});
// Favorite an track // Favorite an track
router.post('/:track/favorite', auth.required, function (req, res, next) { router.post(
const trackId = req.track._id; '/:track/favorite',
auth.required,
wrapRoute(async (req, res) => {
const trackId = req.track._id;
User.findById(req.payload.id) const user = await User.findById(req.payload.id);
.then(function (user) { if (!user) {
if (!user) { return res.sendStatus(401);
return res.sendStatus(401); }
}
return user.favorite(trackId).then(function () { await user.favorite(trackId);
return req.track.updateFavoriteCount().then(function (track) { const track = await req.track.updateFavoriteCount();
return res.json({ track: track.toJSONFor(user) }); return res.json({ track: track.toJSONFor(user) });
}); }),
}); );
})
.catch(next);
});
// Unfavorite an track // Unfavorite an track
router.delete('/:track/favorite', auth.required, function (req, res, next) { router.delete(
const trackId = req.track._id; '/:track/favorite',
auth.required,
wrapRoute(async (req, res) => {
const trackId = req.track._id;
User.findById(req.payload.id) const user = await User.findById(req.payload.id);
.then(function (user) { if (!user) {
if (!user) { return res.sendStatus(401);
return res.sendStatus(401); }
}
return user.unfavorite(trackId).then(function () { await user.unfavorite(trackId);
return req.track.updateFavoriteCount().then(function (track) { const track = await req.track.updateFavoriteCount();
return res.json({ track: track.toJSONFor(user) }); return res.json({ track: track.toJSONFor(user) });
}); }),
}); );
})
.catch(next);
});
// return an track's comments // return an track's comments
router.get('/:track/comments', auth.optional, function (req, res, next) { router.get(
Promise.resolve(req.payload ? User.findById(req.payload.id) : null) '/:track/comments',
.then(function (user) { auth.optional,
return req.track wrapRoute(async (req, res) => {
.populate({ const user = await Promise.resolve(req.payload ? User.findById(req.payload.id) : null);
path: 'comments',
populate: { await req.track
path: 'author', .populate({
path: 'comments',
populate: {
path: 'author',
},
options: {
sort: {
createdAt: 'desc',
}, },
options: { },
sort: { })
createdAt: 'desc', .execPopulate();
},
}, return res.json({
}) comments: req.track.comments.map(function (comment) {
.execPopulate() return comment.toJSONFor(user);
.then(function (track) { }),
return res.json({ });
comments: req.track.comments.map(function (comment) { }),
return comment.toJSONFor(user); );
}),
});
});
})
.catch(next);
});
// create a new comment // create a new comment
router.post('/:track/comments', auth.required, function (req, res, next) { router.post(
User.findById(req.payload.id) '/:track/comments',
.then(function (user) { auth.required,
if (!user) { wrapRoute(async (req, res) => {
return res.sendStatus(401); const user = await User.findById(req.payload.id);
} if (!user) {
return res.sendStatus(401);
}
const comment = new Comment(req.body.comment); const comment = new Comment(req.body.comment);
comment.track = req.track; comment.track = req.track;
comment.author = user; comment.author = user;
return comment.save().then(function () { await comment.save();
req.track.comments.push(comment);
return req.track.save().then(function (track) { req.track.comments.push(comment);
res.json({ comment: comment.toJSONFor(user) });
});
});
})
.catch(next);
});
router.delete('/:track/comments/:comment', auth.required, function (req, res, next) { await req.track.save();
if (req.comment.author.toString() === req.payload.id.toString()) { return res.json({ comment: comment.toJSONFor(user) });
req.track.comments.remove(req.comment._id); }),
req.track );
.save()
.then(Comment.find({ _id: req.comment._id }).remove().exec()) router.delete(
.then(function () { '/:track/comments/:comment',
res.sendStatus(204); auth.required,
}); wrapRoute(async (req, res) => {
} else { if (req.comment.author.toString() === req.payload.id.toString()) {
res.sendStatus(403); req.track.comments.remove(req.comment._id);
} await req.track.save();
}); await Comment.find({ _id: req.comment._id }).remove();
res.sendStatus(204);
} else {
res.sendStatus(403);
}
}),
);
// return an track's trackData // return an track's trackData
router.get('/:track/TrackData', auth.optional, function (req, res, next) { router.get(
Promise.resolve(req.payload ? User.findById(req.payload.id) : null) '/:track/TrackData',
.then(function (user) { auth.optional,
// console.log("requestTrackData"+req.track); wrapRoute(async (req, res) => {
TrackData.findById(req.track.trackData, function (err, trackData) { // console.log("requestTrackData"+req.track);
// console.log({trackData: trackData}); const trackData = await TrackData.findById(req.track.trackData);
return res.json({ trackData: trackData }); // console.log({trackData: trackData});
}); return res.json({ trackData: trackData });
}) }),
.catch(next); );
});
module.exports = router; module.exports = router;