Archived
videotrack
This commit is contained in:
+32
-4
@@ -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
|
// GET /globpop
|
||||||
app.get('/globpop', (req, res) => {
|
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');
|
req.query.id ? res.send(req.query.id) : res.status(400).send('Bad Request');
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -74,21 +87,36 @@ low(new FileAsync(__dirname + '/db.json')) // production
|
|||||||
res.set({
|
res.set({
|
||||||
'Access-Control-Allow-Origin': '*'
|
'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 videos = db.get('videos');
|
||||||
const row = videos.getById(req.body.id);
|
const row = videos.getById(req.body.id);
|
||||||
if (row.value()) {
|
if (row.value()) {
|
||||||
row
|
row
|
||||||
.update('timesWatched', (n) => ++n)
|
.update('timesWatched', (n) => ++n)
|
||||||
.update('lastWatched', () => new Date().toISOString())
|
.update('lastWatched', () => new Date().toISOString())
|
||||||
|
.update('reason', (n) => setReason(req.body.reason, n))
|
||||||
.write()
|
.write()
|
||||||
.then(output => res.send(output))
|
.then(output => res.send(200, output))
|
||||||
} else {
|
} else {
|
||||||
let newRow = {
|
let newRow = {
|
||||||
"id": req.body.id.toString(),
|
"id": req.body.id.toString(),
|
||||||
"timesWatched": 1,
|
"timesWatched": 1,
|
||||||
"lastWatched": new Date().toISOString(),
|
"lastWatched": new Date().toISOString(),
|
||||||
|
"reason": setReason(req.body.reason)
|
||||||
}
|
}
|
||||||
videos.push(newRow).last().write().then(output => res.send(200,output))
|
videos.push(newRow).last().write().then(output => res.send(200, output))
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -97,8 +125,8 @@ low(new FileAsync(__dirname + '/db.json')) // production
|
|||||||
})
|
})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
app.use(express.static(__dirname + '/build')); // serve static build site
|
app.use(express.static(__dirname + '/build')); // serve static build site
|
||||||
app.get('*', (req,res) =>{
|
app.get('*', (req, res) => {
|
||||||
res.sendFile(__dirname+'/build/index.html');
|
res.sendFile(__dirname + '/build/index.html');
|
||||||
});
|
});
|
||||||
app.listen(8000, () => console.log('listen on 8000')); // bind to port 8000 as required from specs
|
app.listen(8000, () => console.log('listen on 8000')); // bind to port 8000 as required from specs
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user