diff --git a/node-entry.js b/node-entry.js index 5adfe7f..42f372f 100644 --- a/node-entry.js +++ b/node-entry.js @@ -1,14 +1,69 @@ const express = require('express'); -const app = express(); +const bodyParser = require('body-parser'); +const low = require('lowdb'); +const lodashId = require('lodash-id'); +const FileAsync = require('lowdb/adapters/FileAsync'); +const app = express(); // app is an instance of express -app.use(express.static(__dirname + '/build')); +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) => { - // req.params.id - // new wiki().page(req.params.id) - // .then(page => page.fullInfo()) - // .then(parsed => res.send(parsed)); - res.send({ok:req.params.id}); -}); +// Create database instance and start server +// const adapter = new FileAsync(__dirname + '/db.json'); +low(new FileAsync(__dirname + '/db.json')) // production +// low(new FileAsync('/tmp/db.json')) // dev + .then(db => { + db._.mixin(lodashId); + // DATABASE ROUTES -app.listen(8000, () => console.log('listen on 8000')); +// ============== + + app.post('/test',(req,res)=>{ + // "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(output => res.send(output)) + } else { + let newRow = { + "id": req.body.id.toString(), + "timesWatched": 1, + "lastWatched": new Date().toISOString(), + } + videos.push(newRow).last().write().then(output => res.send(output)) + } + }); + + // Set db default values + 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/package-lock.json b/package-lock.json index 52d3751..2f16f45 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8236,6 +8236,11 @@ "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.11.tgz", "integrity": "sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==" }, + "lodash-id": { + "version": "0.14.0", + "resolved": "https://registry.npmjs.org/lodash-id/-/lodash-id-0.14.0.tgz", + "integrity": "sha1-uvSJNOVDobXWNG+MhGmLGoyAOJY=" + }, "lodash._reinterpolate": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/lodash._reinterpolate/-/lodash._reinterpolate-3.0.0.tgz", @@ -8301,6 +8306,25 @@ "js-tokens": "^3.0.0 || ^4.0.0" } }, + "lowdb": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/lowdb/-/lowdb-1.0.0.tgz", + "integrity": "sha512-2+x8esE/Wb9SQ1F9IHaYWfsC9FIecLOPrK4g17FGEayjUWH172H6nwicRovGvSE2CPZouc2MCIqCI7h9d+GftQ==", + "requires": { + "graceful-fs": "^4.1.3", + "is-promise": "^2.1.0", + "lodash": "4", + "pify": "^3.0.0", + "steno": "^0.4.1" + }, + "dependencies": { + "pify": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/pify/-/pify-3.0.0.tgz", + "integrity": "sha1-5aSs0sEB/fPZpNB/DbxNtJ3SgXY=" + } + } + }, "lower-case": { "version": "1.1.4", "resolved": "https://registry.npmjs.org/lower-case/-/lower-case-1.1.4.tgz", @@ -13328,6 +13352,14 @@ "resolved": "https://registry.npmjs.org/stealthy-require/-/stealthy-require-1.1.1.tgz", "integrity": "sha1-NbCYdbT/SfJqd35QmzCQoyJr8ks=" }, + "steno": { + "version": "0.4.4", + "resolved": "https://registry.npmjs.org/steno/-/steno-0.4.4.tgz", + "integrity": "sha1-BxEFvfwobmYVwEA8J+nXtdy4Vcs=", + "requires": { + "graceful-fs": "^4.1.3" + } + }, "stream-browserify": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/stream-browserify/-/stream-browserify-2.0.1.tgz", diff --git a/package.json b/package.json index ec9559a..1a08271 100644 --- a/package.json +++ b/package.json @@ -3,9 +3,16 @@ "version": "0.1.0", "private": true, "dependencies": { +<<<<<<< HEAD "axios": "latest", +======= + "axios": "^0.18.0", + "body-parser": "^1.18.3", +>>>>>>> 5ff198adc326a86bc0742f4beeb603992ad7ed5c "bootstrap": "^4.1.3", "express": "^4.16.4", + "lodash-id": "^0.14.0", + "lowdb": "^1.0.0", "react": "^16.6.1", "react-app-polyfill": "^0.1.3", "react-bootstrap": "^1.0.0-beta.2", 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} +