This commit is contained in:
Paul Bienkowski 2020-11-18 20:53:35 +01:00
parent d8975db15a
commit 22f8c46d6a
6 changed files with 927 additions and 327 deletions

File diff suppressed because one or more lines are too long

View file

@ -1,3 +1,5 @@
const csvParse = require('csv-parse/lib/sync');
function _parseFloat(token) { function _parseFloat(token) {
let f = parseFloat(token); let f = parseFloat(token);
if (isNaN(f)) { if (isNaN(f)) {
@ -9,13 +11,55 @@ function _parseFloat(token) {
return f; return f;
} }
module.exports.addPointsToTrack = function addPointsToTrack(track, body) { function addPointsToTrack(trackInfo, body, format = null) {
const detectedFormat = format != null ? format : detectFormat(body);
let parser;
switch (detectedFormat) {
case 'invalid':
throw new Error('track format cannot be detected');
case 1:
parser = parseObsver1;
break;
case 2:
parser = parseObsver2;
break;
}
const points = trackInfo.trackData.points;
for (const newPoint of parser(body)) {
points.push(newPoint);
}
}
function detectFormat(body) {
if (!body.length) {
return 'invalid';
}
const firstLinebreakIndex = body.indexOf('\n');
if (firstLinebreakIndex === -1) {
return 1;
}
const firstLine = body.substring(0, firstLinebreakIndex);
const match = firstLine.match(/(^|&)OBSDataFormat=([\d]+)($|&)/);
if (match) {
return Number(match[2]);
}
return 'invalid';
}
function* parseObsver1(body) {
let num = 0; let num = 0;
let start = 0; let start = 0;
let end = 0; let end = 0;
// reference to the array we will mutate
const points = track.trackData.points;
let currentPoint; let currentPoint;
while (end < body.length) { while (end < body.length) {
@ -24,6 +68,9 @@ module.exports.addPointsToTrack = function addPointsToTrack(track, body) {
end++; end++;
} }
if (body[end] === '$') { if (body[end] === '$') {
if (currentPoint) {
yield currentPoint;
}
// $ is replacing \n as newlines are not allowed in json strings // $ is replacing \n as newlines are not allowed in json strings
num = 0; num = 0;
} }
@ -46,7 +93,7 @@ module.exports.addPointsToTrack = function addPointsToTrack(track, body) {
switch (num) { switch (num) {
case 0: case 0:
currentPoint = { currentPoint = {
date: 'dummy', date: token,
time: '', time: '',
latitude: '', latitude: '',
longitude: '', longitude: '',
@ -57,10 +104,6 @@ module.exports.addPointsToTrack = function addPointsToTrack(track, body) {
flag: '', flag: '',
private: '', private: '',
}; };
points.push(currentPoint);
currentPoint.date = token;
break; break;
case 1: case 1:
@ -104,4 +147,88 @@ module.exports.addPointsToTrack = function addPointsToTrack(track, body) {
} }
} }
} }
}; if (currentPoint) {
yield currentPoint;
}
}
function* parseObsver2(body) {
for (const record of csvParse(body, {
from_line: 2,
trim: true,
columns: true,
skip_empty_lines: true,
delimiter: ';',
encoding: 'utf8',
relax_column_count: true,
cast(value, context) {
if (value === '') {
return null;
}
let type;
switch (context.column) {
case 'Millis':
case 'Left':
case 'Right':
case 'Confirmed':
case 'Invalid':
case 'InsidePrivacyArea':
case 'Measurements':
case 'Satellites':
type = 'int';
break;
case 'Date':
case 'Time':
case 'Comment':
case 'Marked':
type = 'string';
break;
case 'Latitude':
case 'Longitude':
case 'Altitude':
case 'Course':
case 'Speed':
case 'HDOP':
case 'BatteryLevel':
case 'Factor':
type = 'float';
break;
default:
type = /^(Tms|Lus|Rus)/.test(context.column) ? 'int' : 'string';
}
switch (type) {
case 'int':
return parseInt(value);
case 'float':
return parseFloat(value);
case 'string':
return value;
}
},
})) {
// We convert the new format back to the old format for storage here, until
// we upgrade the storage format as well to include all data. But we'll
// have to upgrade the obsApp first.
yield {
date: record.Date,
time: record.Time,
latitude: record.Latitude,
longitude: record.Longitude,
course: record.Course,
speed: record.Speed,
d1: record.Left,
d2: record.Right,
flag: Boolean(record.Confirmed),
private: Boolean(record.InsidePrivacyArea),
};
}
}
module.exports = { addPointsToTrack, detectFormat, parseObsver1, parseObsver2 };

View file

@ -1,7 +1,7 @@
const { addPointsToTrack } = require('./tracks'); const { addPointsToTrack, parseObsver1, detectFormat, parseObsver2 } = require('./tracks');
const TrackInfo = require('./TrackInfo'); const TrackInfo = require('./TrackInfo');
const { test1 } = require('./_tracks_testdata'); const { test1, test2 } = require('./_tracks_testdata');
describe('addPointsToTrack', () => { describe('addPointsToTrack', () => {
it('is a function', () => { it('is a function', () => {
@ -27,3 +27,70 @@ describe('addPointsToTrack', () => {
}); });
}); });
}); });
describe('parseObsver1', () => {
it('can parse sample data', () => {
const points = Array.from(parseObsver1(test1));
expect(points).toHaveLength(324);
expect(points[0]).toEqual({
date: '12.07.2020',
time: '09:02:59',
latitude: 0,
longitude: 0,
course: 0,
speed: 0,
d1: '255',
d2: '255',
flag: '0',
private: '0',
});
});
});
describe('parseObsver2', () => {
it('can parse sample data', () => {
const points = Array.from(parseObsver2(test2));
expect(points).toHaveLength(18);
expect(points[0]).toEqual({
date: '18.11.2020',
time: '16:05:59',
latitude: 48.723224,
longitude: 9.094103,
course: 189.86,
speed: 3.2,
d1: 770,
d2: null,
flag: false,
private: true,
});
// this is a non-private, flagged point (i.e. "Confirmed" overtaking)
expect(points[17]).toEqual({
date: '18.11.2020',
time: '16:06:16',
latitude: 48.723109,
longitude: 9.093963,
course: 247.62,
speed: 0,
d1: 5,
d2: 89,
flag: true,
private: false,
});
});
});
describe('detectFormat', () => {
it('detects format 1', () => {
expect(detectFormat(test1)).toBe(1);
});
it('detects format 2', () => {
expect(detectFormat(test2)).toBe(2);
});
it('detects invalid format', () => {
expect(detectFormat('foobar\nbaz')).toBe('invalid');
expect(detectFormat('')).toBe('invalid');
});
});

13
package-lock.json generated
View file

@ -2143,10 +2143,9 @@
} }
}, },
"csv-parse": { "csv-parse": {
"version": "4.12.0", "version": "4.14.1",
"resolved": "https://registry.npmjs.org/csv-parse/-/csv-parse-4.12.0.tgz", "resolved": "https://registry.npmjs.org/csv-parse/-/csv-parse-4.14.1.tgz",
"integrity": "sha512-wPQl3H79vWLPI8cgKFcQXl0NBgYYEqVnT1i6/So7OjMpsI540oD7p93r3w6fDSyPvwkTepG05F69/7AViX2lXg==", "integrity": "sha512-4wmcO7QbWtDAncGFaBwlWFPhEN4Akr64IbM4zvDwEOFekI8blLc04Nw7XjQjtSNy+3AUAgBgtUa9nWo5Cq89Xg=="
"dev": true
}, },
"dashdash": { "dashdash": {
"version": "1.14.1", "version": "1.14.1",
@ -6347,6 +6346,12 @@
"integrity": "sha512-TR2mEZFVOj2pLStYxLht7TyfuRzaydfpxr3k9RpHIzMgw7A64dzsdqCxH1WJyQdoe8T10nDXd9wnEigmiuHIZw==", "integrity": "sha512-TR2mEZFVOj2pLStYxLht7TyfuRzaydfpxr3k9RpHIzMgw7A64dzsdqCxH1WJyQdoe8T10nDXd9wnEigmiuHIZw==",
"dev": true "dev": true
}, },
"csv-parse": {
"version": "4.12.0",
"resolved": "https://registry.npmjs.org/csv-parse/-/csv-parse-4.12.0.tgz",
"integrity": "sha512-wPQl3H79vWLPI8cgKFcQXl0NBgYYEqVnT1i6/So7OjMpsI540oD7p93r3w6fDSyPvwkTepG05F69/7AViX2lXg==",
"dev": true
},
"semver": { "semver": {
"version": "7.3.2", "version": "7.3.2",
"resolved": "https://registry.npmjs.org/semver/-/semver-7.3.2.tgz", "resolved": "https://registry.npmjs.org/semver/-/semver-7.3.2.tgz",

View file

@ -19,6 +19,7 @@
"dependencies": { "dependencies": {
"body-parser": "1.19.0", "body-parser": "1.19.0",
"cors": "2.8.5", "cors": "2.8.5",
"csv-parse": "^4.14.1",
"ejs": "^3.1.5", "ejs": "^3.1.5",
"errorhandler": "1.5.1", "errorhandler": "1.5.1",
"express": "4.17.1", "express": "4.17.1",

View file

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