diff --git a/node-entry.js b/node-entry.js index e572520..42f372f 100644 --- a/node-entry.js +++ b/node-entry.js @@ -4,20 +4,11 @@ const low = require('lowdb'); const lodashId = require('lodash-id'); const FileAsync = require('lowdb/adapters/FileAsync'); const app = express(); // app is an instance of express -const axios = require('axios'); app.use(bodyParser.urlencoded({ extended: false })) // parse application/x-www-form-urlencoded app.use(bodyParser.json()) // parse application/json app.use(express.static(__dirname + '/build')); // serve static build site - -app.get('/test/:id',(req,res) =>{ - axios.post('http://localhost:8000/test', { - "id": req.params.id.toString(), - "reason": req.query.reason.toString() - }).then(response => res.send(response.data), error => console.log(error)) -}); - // Create database instance and start server // const adapter = new FileAsync(__dirname + '/db.json'); low(new FileAsync(__dirname + '/db.json')) // production @@ -29,72 +20,49 @@ low(new FileAsync(__dirname + '/db.json')) // production // ============== app.post('/test',(req,res)=>{ - //console.log(req.body); - const table = db.get('videos'); - let row = table.getById(req.body.id); + // "reason": { + // "fitali": req.body.reason.toString() === "fvitali" ? 1:0, + // "": req.body.reason.toString() === "" ? 1:0, + // "": req.body.reason.toString() === "" ? 1:0, + // } + // try { + // let test = table.updateById(req.body.id, { }) + // } + // catch(e) { + // } + res.send(req.body); + }); + +// ============== + + // GET /videotrack/:id? + app.get('/videotrack/:id?', (req, res) => { + const videos = db.get('videos'); + req.params.id ? res.send(videos.getById(req.params.id).value()) : res.send(videos.value()) + }); + + // POST /videotrack + app.post('/videotrack', (req, res) => { + 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()) .write() - .then(final => res.send(final)) + .then(output => res.send(output)) } else { - // "reason": { - // "fitali": req.body.reason.toString() === "fvitali" ? 1:0, - // "": req.body.reason.toString() === "" ? 1:0, - // "": req.body.reason.toString() === "" ? 1:0, - // } - let post = { + let newRow = { "id": req.body.id.toString(), "timesWatched": 1, "lastWatched": new Date().toISOString(), } - table.push(post).last().write().then(final => res.send(final)) + videos.push(newRow).last().write().then(output => res.send(output)) } + }); - // try { - // let test = table.updateById(req.body.id, { }) - // } - // catch(e) { - - // } - - }); - -// ============== - - // GET /videolog - app.get('/videolog', (req, res) => { - const post = db - .get('videos') - // .find({ id: req.params.id }) - .value(); - res.send(post); - }); - - // POST /videolog - app.post('/videolog', (req, res) => { - db.get('videos') - .push(req.body) - .last() - //.update('timesWatched', n => n + 1 ) - .write() - .then(post => res.send(post)); // - }); - // Set db default values - return db - .defaults({ - videos: [ - // { - // videoId: '', - // timesWatched: 0, - // prevalentReason: '', - // lastSelected: new Date() - // } - ] - }) - .write(); + return db.defaults({ videos: [] }).write(); }) .then(() => { app.listen(8000, () => console.log('listen on 8000')); // bind to port 8000 as required from specs diff --git a/src/VideoList.js b/src/VideoList.js index c098398..d8bc5df 100644 --- a/src/VideoList.js +++ b/src/VideoList.js @@ -36,7 +36,7 @@ class VideoList extends React.Component { ); } -// METODI + // METODI getThumbnails(videoItems) { let idToLookFor = videoItems.map(item => item.videoID); let finalChunk = (videoItems.length % 50) + 100; @@ -125,29 +125,36 @@ class VideoList extends React.Component { {items.map(item => (
{item.category}
++ {item.category} +