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);
|
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 {
|
return {
|
||||||
slug: this.slug,
|
slug: this.slug,
|
||||||
title: this.title,
|
title: this.title,
|
||||||
|
@ -39,7 +39,8 @@ TrackSchema.methods.toJSONFor = function(user){
|
||||||
updatedAt: this.updatedAt,
|
updatedAt: this.updatedAt,
|
||||||
visibleForAll: this.author ? this.author.areTracksVisibleForAll : false,
|
visibleForAll: this.author ? this.author.areTracksVisibleForAll : false,
|
||||||
visible: this.visible,
|
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) {
|
.then(function (results) {
|
||||||
const user = results[0];
|
const user = results[0];
|
||||||
|
|
||||||
return res.json({ track: req.track.toJSONFor(user) });
|
return res.json({ track: req.track.toJSONFor(user, { body: true }) });
|
||||||
})
|
})
|
||||||
.catch(next);
|
.catch(next);
|
||||||
});
|
});
|
||||||
|
|
||||||
// update track
|
// update track
|
||||||
router.put('/:track', auth.required, function (req, res, next) {
|
router.put('/:track', auth.required, async function (req, res, next) {
|
||||||
User.findById(req.payload.id).then(function (user) {
|
const user = await User.findById(req.payload.id);
|
||||||
if (req.track.author._id.toString() === req.payload.id.toString()) {
|
|
||||||
|
if (req.track.author._id.toString() !== req.payload.id.toString()) {
|
||||||
|
return res.sendStatus(403);
|
||||||
|
}
|
||||||
|
|
||||||
if (typeof req.body.track.title !== 'undefined') {
|
if (typeof req.body.track.title !== 'undefined') {
|
||||||
req.track.title = req.body.track.title;
|
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;
|
req.track.description = req.body.track.description;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof req.body.track.body !== 'undefined') {
|
if (req.body.track.body?.trim()) {
|
||||||
req.track.body = req.body.track.body;
|
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') {
|
if (typeof req.body.track.tagList !== 'undefined') {
|
||||||
req.track.tagList = req.body.track.tagList;
|
req.track.tagList = req.body.track.tagList;
|
||||||
}
|
}
|
||||||
req.track.visible = req.body.track.visible;
|
req.track.visible = req.body.track.visible;
|
||||||
console.log('saving track');
|
|
||||||
|
|
||||||
req.track
|
const track = await req.track.save();
|
||||||
.save()
|
|
||||||
.then(function (track) {
|
|
||||||
return res.json({ track: track.toJSONFor(user) });
|
return res.json({ track: track.toJSONFor(user) });
|
||||||
})
|
|
||||||
.catch(next);
|
|
||||||
} else {
|
|
||||||
return res.sendStatus(403);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// delete track
|
// delete track
|
||||||
|
|
Loading…
Reference in a new issue