12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- var express = require('express');
- var router = express.Router();
- var db = require('../db');
- var Crawler = require('./crawler.js');
- // /* GET home page. */
- // router.get('/', function(req, res, next) {
- // res.render('index', { title: 'Express' });
- // });
- function parseInput(input) {
- return {
- data: {},
- price: 0.0,
- position: 0,
- enemies: input.enemies.split(','),
- category: input.category
- }
- }
- router.put('/:code', function(req, res) {
- let items = parseInput(req.body);
- console.log('Body:');
- console.log(items);
- db.put(req.params.code, items)
- .then(() => {
- return db.get(req.params.code);
- })
- .then(() => res.sendStatus(200));
- });
- router.delete('/:code', function(req, res) {
- db.del(req.params.code)
- .then(() => res.sendStatus(200));
- });
- router.get('/', function(req, res) {
- db.getAll().then((v) => {
- res.send(v);
- });
- });
- router.get('/fix', (req, res) => {
- let proms = [];
- db.getAll().then((items) => {
- for(var i in items) {
- items[i].category = "926/symvata-anakataskevasmena-toner-drums-ektypwtwn";
- proms.push(db.put(i, items[i]));
- }
- }).then(() => {
- Promise.all(proms).then(() => res.sendStatus(200)).catch((e)=> res.send(e));
- });
- })
- function addMinutes(date, Minutes) {
- var result = new Date(date);
- result.setMinutes(result.getMinutes() + Minutes);
- return result;
- }
- router.get('/refresh', function(req, res) {
- let now = new Date().toISOString();
- db.getAll().then((items) => {
- let proms = [];
- for (var i in items) {
- if( !items[i].lastDate || addMinutes(items[i].lastDate, 10).toISOString() < now) {
- console.log(items[i]);
- proms.push(
- Crawler.collect(i, items[i])
- );
- }
- }
- Promise.all(proms).then(() => {
- res.sendStatus(200);
- }).catch((d) => {
- console.log(d);
- res.sendStatus(201);
- });
- });
- });
- module.exports = router;
|