videotrack

This commit is contained in:
matteo
2019-03-03 16:32:50 +01:00
parent 668b259adb
commit db8800965d
+29 -1
View File
@@ -43,9 +43,22 @@ low(new FileAsync(__dirname + '/db.json')) // production
});
// ==============
// OPTIONS /globpop
app.options('/globpop', (req, res) => {
res.set({
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': ' GET, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Max-Age': 600
})
res.status(200).end();
})
// GET /globpop
app.get('/globpop', (req, res) => {
res.set({
'Access-Control-Allow-Origin': '*'
});
req.query.id ? res.send(req.query.id) : res.status(400).send('Bad Request');
});
@@ -74,19 +87,34 @@ low(new FileAsync(__dirname + '/db.json')) // production
res.set({
'Access-Control-Allow-Origin': '*'
});
let setReason = function (reason='Starter', oldObj = {}) {
if (oldObj.hasOwnProperty(reason)) {
++oldObj[reason];
return oldObj;
}
else {
Object.defineProperty(oldObj,
reason, { value: 1, enumerable: true, writable: true }
)
return oldObj;
}
}
const videos = db.get('videos');
const row = videos.getById(req.body.id);
if (row.value()) {
row
.update('timesWatched', (n) => ++n)
.update('lastWatched', () => new Date().toISOString())
.update('reason', (n) => setReason(req.body.reason, n))
.write()
.then(output => res.send(output))
.then(output => res.send(200, output))
} else {
let newRow = {
"id": req.body.id.toString(),
"timesWatched": 1,
"lastWatched": new Date().toISOString(),
"reason": setReason(req.body.reason)
}
videos.push(newRow).last().write().then(output => res.send(200, output))
}