minor fixes

This commit is contained in:
matteo
2018-12-03 01:22:48 +01:00
parent 0f38bae769
commit 5f093c35d6
3 changed files with 55 additions and 82 deletions
+30 -62
View File
@@ -4,20 +4,11 @@ const low = require('lowdb');
const lodashId = require('lodash-id'); const lodashId = require('lodash-id');
const FileAsync = require('lowdb/adapters/FileAsync'); const FileAsync = require('lowdb/adapters/FileAsync');
const app = express(); // app is an instance of express 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.urlencoded({ extended: false })) // parse application/x-www-form-urlencoded
app.use(bodyParser.json()) // parse application/json app.use(bodyParser.json()) // parse application/json
app.use(express.static(__dirname + '/build')); // serve static build site 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 // Create database instance and start server
// const adapter = new FileAsync(__dirname + '/db.json'); // const adapter = new FileAsync(__dirname + '/db.json');
low(new FileAsync(__dirname + '/db.json')) // production low(new FileAsync(__dirname + '/db.json')) // production
@@ -29,72 +20,49 @@ low(new FileAsync(__dirname + '/db.json')) // production
// ============== // ==============
app.post('/test',(req,res)=>{ app.post('/test',(req,res)=>{
//console.log(req.body); // "reason": {
const table = db.get('videos'); // "fitali": req.body.reason.toString() === "fvitali" ? 1:0,
let row = table.getById(req.body.id); // "": 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()) { if(row.value()) {
row row
.update('timesWatched',(n) => ++n) .update('timesWatched',(n) => ++n)
.update('lastWatched',() => new Date().toISOString()) .update('lastWatched',() => new Date().toISOString())
.write() .write()
.then(final => res.send(final)) .then(output => res.send(output))
} else { } else {
// "reason": { let newRow = {
// "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(), "id": req.body.id.toString(),
"timesWatched": 1, "timesWatched": 1,
"lastWatched": new Date().toISOString(), "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 // Set db default values
return db return db.defaults({ videos: [] }).write();
.defaults({
videos: [
// {
// videoId: '',
// timesWatched: 0,
// prevalentReason: '',
// lastSelected: new Date()
// }
]
})
.write();
}) })
.then(() => { .then(() => {
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
+18 -11
View File
@@ -36,7 +36,7 @@ class VideoList extends React.Component {
); );
} }
// METODI // METODI
getThumbnails(videoItems) { getThumbnails(videoItems) {
let idToLookFor = videoItems.map(item => item.videoID); let idToLookFor = videoItems.map(item => item.videoID);
let finalChunk = (videoItems.length % 50) + 100; let finalChunk = (videoItems.length % 50) + 100;
@@ -125,29 +125,36 @@ class VideoList extends React.Component {
{items.map(item => ( {items.map(item => (
<Col key={item.videoID} lg={{ span: 3 }} md="6"> <Col key={item.videoID} lg={{ span: 3 }} md="6">
<Card className="my-1 carta"> <Card className="my-1 carta">
<Link to={{ <Link
pathname: '/video/' + item.videoID, to={{
state: { pathname: '/video/' + item.videoID,
artist: item.artist, state: {
title: item.title +' '+ item.artist artist: item.artist,
title: item.title
} }
}}> }}
>
{/* {item.artist} - {item.title} {item.videoID} {item.category} */} {/* {item.artist} - {item.title} {item.videoID} {item.category} */}
<Card.Img src={item.thumbnail} alt={'Thumbnail of '+item.title } /> <Card.Img
src={item.thumbnail}
alt={'Thumbnail of ' + item.title}
/>
<Card.ImgOverlay> <Card.ImgOverlay>
<Card.Title className="bg-dark d-inline text-white"> <Card.Title className="bg-dark d-inline text-white">
{item.artist} - {item.title} {item.artist} - {item.title}
</Card.Title> </Card.Title>
<p className='d-table d-lg-none card-text bg-dark text-white'>{item.category}</p> <p className="d-table d-lg-none card-text bg-dark text-white">
{item.category}
</p>
</Card.ImgOverlay> </Card.ImgOverlay>
</Link> </Link>
</Card> </Card>
</Col> </Col>
))} ))}
</React.Fragment> </React.Fragment>
); );
} }
} }
} }
export default VideoList; export default VideoList;
+7 -9
View File
@@ -64,16 +64,15 @@ class VideoPlayer extends Component {
</React.Fragment> </React.Fragment>
); );
} }
// access to player in all event handlers via event.target
_onError(event) { _onError(event) {
console.error('on error', event); console.error('Error: ', event);
} }
_onPlay(event) {} _onPlay(event) {}
_onReady(event) { _onReady(event) {}
// access to player in all event handlers via event.target
}
_onStateChange(event) { _onStateChange(event) {
let timerId1, timerId2; let timerId1, timerId2;
@@ -110,12 +109,11 @@ class VideoPlayer extends Component {
}); });
this.state.promise1 this.state.promise1
.then( .then(
risultato => { videoIdWatched => {
//console.info(risultato);
// post request to local db // post request to local db
axios axios
.post('http://localhost:8000/test', { .post('http://localhost:8000/videotrack', {
id: risultato.toString() id: videoIdWatched.toString()
}) })
.then( .then(
response => console.info(response.data), response => console.info(response.data),