feat: generate TrackData points when saving tracks through PUT, and return raw body through GET /:track
This commit is contained in:
parent
0b830601f5
commit
f3740e5f37
|
@ -30,7 +30,7 @@ TrackSchema.methods.slugify = function() {
|
|||
this.slug = slug(this.title) + '-' + (Math.random() * Math.pow(36, 6) | 0).toString(36);
|
||||
};
|
||||
|
||||
TrackSchema.methods.toJSONFor = function(user){
|
||||
TrackSchema.methods.toJSONFor = function (user, include) {
|
||||
return {
|
||||
slug: this.slug,
|
||||
title: this.title,
|
||||
|
@ -39,7 +39,8 @@ TrackSchema.methods.toJSONFor = function(user){
|
|||
updatedAt: this.updatedAt,
|
||||
visibleForAll: this.author ? this.author.areTracksVisibleForAll : false,
|
||||
visible: this.visible,
|
||||
author: this.author.toProfileJSONFor(user)
|
||||
author: this.author.toProfileJSONFor(user),
|
||||
...(include?.body ? { body: this.body } : {}),
|
||||
};
|
||||
};
|
||||
|
||||
|
|
|
@ -313,15 +313,19 @@ router.get('/:track', auth.optional, function (req, res, next) {
|
|||
.then(function (results) {
|
||||
const user = results[0];
|
||||
|
||||
return res.json({ track: req.track.toJSONFor(user) });
|
||||
return res.json({ track: req.track.toJSONFor(user, { body: true }) });
|
||||
})
|
||||
.catch(next);
|
||||
});
|
||||
|
||||
// update track
|
||||
router.put('/:track', auth.required, function (req, res, next) {
|
||||
User.findById(req.payload.id).then(function (user) {
|
||||
if (req.track.author._id.toString() === req.payload.id.toString()) {
|
||||
router.put('/:track', auth.required, async function (req, res, next) {
|
||||
const user = await User.findById(req.payload.id);
|
||||
|
||||
if (req.track.author._id.toString() !== req.payload.id.toString()) {
|
||||
return res.sendStatus(403);
|
||||
}
|
||||
|
||||
if (typeof req.body.track.title !== 'undefined') {
|
||||
req.track.title = req.body.track.title;
|
||||
}
|
||||
|
@ -330,26 +334,26 @@ router.put('/:track', auth.required, function (req, res, next) {
|
|||
req.track.description = req.body.track.description;
|
||||
}
|
||||
|
||||
if (typeof req.body.track.body !== 'undefined') {
|
||||
req.track.body = req.body.track.body;
|
||||
if (req.body.track.body?.trim()) {
|
||||
req.track.body = req.body.track.body.trim();
|
||||
|
||||
let trackData = await TrackData.findById(req.track.trackData);
|
||||
if (!trackData) {
|
||||
trackData = new TrackData();
|
||||
req.track.trackData = trackData._id;
|
||||
}
|
||||
trackData.points = [];
|
||||
addPointsToTrack({ trackData }, req.track.body);
|
||||
await trackData.save();
|
||||
}
|
||||
|
||||
if (typeof req.body.track.tagList !== 'undefined') {
|
||||
req.track.tagList = req.body.track.tagList;
|
||||
}
|
||||
req.track.visible = req.body.track.visible;
|
||||
console.log('saving track');
|
||||
|
||||
req.track
|
||||
.save()
|
||||
.then(function (track) {
|
||||
const track = await req.track.save();
|
||||
return res.json({ track: track.toJSONFor(user) });
|
||||
})
|
||||
.catch(next);
|
||||
} else {
|
||||
return res.sendStatus(403);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// delete track
|
||||
|
|
Loading…
Reference in a new issue