diff --git a/node-entry.js b/node-entry.js index 81fcbfd..e572520 100644 --- a/node-entry.js +++ b/node-entry.js @@ -1,22 +1,68 @@ const express = require('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 +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.post('/test',(req,res)=>{ - res.send(req.body); -}) + + +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')) +low(new FileAsync(__dirname + '/db.json')) // production +// low(new FileAsync('/tmp/db.json')) // dev .then(db => { + db._.mixin(lodashId); // DATABASE ROUTES +// ============== + + app.post('/test',(req,res)=>{ + //console.log(req.body); + const table = db.get('videos'); + let row = table.getById(req.body.id); + if(row.value()) { + row + .update('timesWatched',(n) => ++n) + .update('lastWatched',() => new Date().toISOString()) + .write() + .then(final => res.send(final)) + } else { + // "reason": { + // "fitali": req.body.reason.toString() === "fvitali" ? 1:0, + // "": req.body.reason.toString() === "" ? 1:0, + // "": req.body.reason.toString() === "" ? 1:0, + // } + let post = { + "id": req.body.id.toString(), + "timesWatched": 1, + "lastWatched": new Date().toISOString(), + } + table.push(post).last().write().then(final => res.send(final)) + } + + // try { + // let test = table.updateById(req.body.id, { }) + // } + // catch(e) { + + // } + + }); + +// ============== + // GET /videolog app.get('/videolog', (req, res) => { const post = db @@ -31,9 +77,9 @@ low(new FileAsync(__dirname + '/db.json')) db.get('videos') .push(req.body) .last() - .update('timesWatched', n => n + 1 ) + //.update('timesWatched', n => n + 1 ) .write() - .then(post => res.send(post)); // non funziona minimamente lol + .then(post => res.send(post)); // }); // Set db default values diff --git a/package-lock.json b/package-lock.json index 30554be..56822a9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8228,6 +8228,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", diff --git a/package.json b/package.json index 682ab72..0119f16 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "body-parser": "^1.18.3", "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", diff --git a/src/VideoPlayer.js b/src/VideoPlayer.js index 33ba5ad..2a40472 100644 --- a/src/VideoPlayer.js +++ b/src/VideoPlayer.js @@ -1,5 +1,6 @@ import React, { Component } from 'react'; // eslint-disable-line no-unused-vars import YouTube from 'react-youtube'; // eslint-disable-line no-unused-vars +import axios from 'axios'; /* eslint no-console: ["error", { allow: ["info", "error", "warn"] }] */ class VideoPlayer extends Component { @@ -91,8 +92,7 @@ class VideoPlayer extends Component { break; } }, 500); - this.setState({outsideReject2: reject }); - + this.setState({ outsideReject2: reject }); }) }); this.state.promise2.then(msg => { @@ -111,8 +111,16 @@ class VideoPlayer extends Component { this.state.promise1 .then( risultato => { - console.info(risultato); + //console.info(risultato); // post request to local db + axios + .post('http://localhost:8000/test', { + id: risultato.toString() + }) + .then( + response => console.info(response.data), + error => console.info(error) + ); // local storage logic for recent reccomender clearInterval(timerId1); },