<commit_before>require('dotenv').load();
var express = require('express');
var router = express.Router();
var knex = require('../db/knex');
var db = require('mongodb');
var promise = require('bluebird');
var request = require('request');
var bcrypt = require('bcrypt');
var unirest = require('unirest');


function Batches(){
  return knex('batches');
}

function Users(){
  return knex('users');
}


/* GET batches for dashboard */



router.get('/', function(req, res, next){
  Batches().where('user_id', req.user.id).select().then(function(batches) {
    return promise.map(batches, function(batch) {
      var batchId = batch.id;
      return new Promise(function(res, rej){
        db.MongoClient.connect(process.env.MONGOLAB_URI, function(err, db){
        var brews = db.collection('brews');
        // console.log(batch);
         brews.find({
          brew_id:batchId
        }).limit(1).next(function(err, data){
          if(data){
            batch.created = data.created,
            batch.lastRun = data.lastRun,
            batch.logs = data.logs,
            batch.notes = data.notes
            batch.schedule = data.schedule;
          }
          res(batch)
          })
        })
      });
    })
  }).then(function(batches) {
    res.send(batches);
  })
})

router.post('/', function(req, res, next){
  Batches().insert({
    user_id: req.user.id,
    beer_id: req.body.styleNumber,
    name: req.body.name
  }, 'id').then(function(data){
    db.MongoClient.connect(process.env.MONGOLAB_URI, function(err, db){
      var brews = db.collection('brews');
      brews.insert({
        brew_id:data[0],
        schedule: req.body.schedule,
        logs: [],
        notes: ''
      }, function(){
        res.send("success");
      })
    });
  });
});

router.delete('/', function(req, res, next){
  Batches().where('id', req.body.id).del()
  .then(function(){
    res.end();
  })
})
router.post('/savebrew', function(req, res, next){
  console.log(req.body);
  db.MongoClient.connect(process.env.MONGOLAB_URI, function(err, db){
    var brews = db.collection('brews');
    brews.update(
      {brew_id:req.body.id},
      {
        brew_id:req.body.id,
        schedule: req.body.schedule,
        created: req.body.created,
        lastRun: req.body.lastRun,
        logs: req.body.logs,
        notes: req.body.notes
      },
      {upsert:true}, function(){
      res.send("success");
    })
  });
})
router.post('/startbrew', function(req, res, next){
  console.log(req.body);
  if(req.user.pi_id){
    console.log('have pi id');
    // var salt = bcrypt.genSaltSync(5);
    // var hash = bcrypt.hashSync(process.env.SERVER_SECRET, salt);
    var hash='test'
    console.log(req.user.pi_id);
//     unirest.post('http://192.168.3.11:3000/startycle')
// .send({ "parameter": 23, "foo": "bar" })
// .end(function (response) {
//   console.log(response.body);
//   res.send('starting your brew');
// });
    request.post('http://'+req.user.pi_id+':3000/startycle', {form:{password: <PASSWORD>, sechdule: req.body.sechdule}});
    res.send('sent the sechdule');
  }else{
    res.send('need a pi ip address');
  }

})
module.exports = router;<commit_msg>have users<commit_after>require('dotenv').load();
var express = require('express');
var router = express.Router();
var knex = require('../db/knex');
var db = require('mongodb');
var promise = require('bluebird');
var request = require('request');
var bcrypt = require('bcrypt');
var unirest = require('unirest');


function Batches(){
  return knex('batches');
}

function Users(){
  return knex('users');
}


/* GET batches for dashboard */



router.get('/', function(req, res, next){
  Batches().where('user_id', req.user.id).select().then(function(batches) {
    return promise.map(batches, function(batch) {
      var batchId = batch.id;
      return new Promise(function(res, rej){
        db.MongoClient.connect(process.env.MONGOLAB_URI, function(err, db){
        var brews = db.collection('brews');
        // console.log(batch);
         brews.find({
          brew_id:batchId
        }).limit(1).next(function(err, data){
          if(data){
            batch.created = data.created,
            batch.lastRun = data.lastRun,
            batch.logs = data.logs,
            batch.notes = data.notes
            batch.schedule = data.schedule;
          }
          res(batch)
          })
        })
      });
    })
  }).then(function(batches) {
    res.send(batches);
  })
})

router.post('/', function(req, res, next){
  Batches().insert({
    user_id: req.user.id,
    beer_id: req.body.styleNumber,
    name: req.body.name
  }, 'id').then(function(data){
    db.MongoClient.connect(process.env.MONGOLAB_URI, function(err, db){
      var brews = db.collection('brews');
      brews.insert({
        brew_id:data[0],
        schedule: req.body.schedule,
        logs: [],
        notes: ''
      }, function(){
        res.send("success");
      })
    });
  });
});

router.delete('/', function(req, res, next){
  Batches().where('id', req.body.id).del()
  .then(function(){
    res.end();
  })
})
router.post('/savebrew', function(req, res, next){
  console.log(req.body);
  db.MongoClient.connect(process.env.MONGOLAB_URI, function(err, db){
    var brews = db.collection('brews');
    brews.update(
      {brew_id:req.body.id},
      {
        brew_id:req.body.id,
        schedule: req.body.schedule,
        created: req.body.created,
        lastRun: req.body.lastRun,
        logs: req.body.logs,
        notes: req.body.notes
      },
      {upsert:true}, function(){
      res.send("success");
    })
  });
})
router.post('/startbrew', function(req, res, next){
  console.log(req.body);
  if(req.user.pi_id){
    console.log('have pi id');
    // var salt = bcrypt.genSaltSync(5);
    // var hash = bcrypt.hashSync(process.env.SERVER_SECRET, salt);
    var hash='hello'
    console.log(req.user.pi_id);
//     unirest.post('http://192.168.3.11:3000/startycle')
// .send({ "parameter": 23, "foo": "bar" })
// .end(function (response) {
//   console.log(response.body);
//   res.send('starting your brew');
// });
    request.post(req.user.pi_id+'/startycle', {form:{password: <PASSWORD>, sechdule: req.body.sechdule}});
    res.send('sent the sechdule');
  }else{
    res.send('need a pi ip address');
  }

})
module.exports = router;
{'fraction_non_alphanumeric': 0.1862265045016585, 'fraction_numerical': 0.00631811720107408, 'mean_word_length': 2.7378984651711926, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7441035', 'n_tokens_mistral': 2168, 'n_tokens_neox': 2127, 'n_words': 372}