<commit_before>var express = require('express');
var app = express();
var fs = require("fs");
var pg = require('pg');

var connectionString = process.env.DATABASE_URL || 'postgres://iceboxuser:testForIce@localhost:5432/icobox';

var bodyParser = require('body-parser')
app.use( bodyParser.json() );       // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({     // to support URL-encoded bodies
  extended: true
}));

app.post('/test-page', function(req, res) {
    //var name = req.body.name,
    console.log(req.body);
    var users = req.body;
    var user = users["user" + 2]
    console.log( user );

    res.end( JSON.stringify(user));
});

app.get('/listUsers', function (req, res) {
  console.log("1");
   fs.readFile( __dirname + "/" + "backup.json", 'utf8', function (err, data) {
       console.log( data );
       res.end( data );
   });
})

app.get('/lolTest2', function (req, res) {
  console.log("1");
  var client = new pg.Client(connectionString);
  client.connect();
  client.query("INSERT INTO items(text, complete) values($1, $2)", ["lol", true]);
  res.end("...");
})

app.get('/lolTest3', function (req, res) {
  console.log("1");
  var results = [];
  var client = new pg.Client(connectionString);
  client.connect();
  var query = client.query("SELECT * FROM items ORDER BY id ASC");
  query.on('row', function(row) {
    results.push(row);
  });
  query.on('end', function() {
    return res.json(results);
  });
})

/*routing.resources(app, controller_dir, "drinks", {}); // last param optional*/

app.put('/drinks/:id', function(req, res) {
    console.log("1-put Drink");
    var drinkId = req.params.id;

    var fullprice = req.body.fullprice;
    var discountprice = req.body.discountprice;

    var client = new pg.Client(connectionString);
    client.connect();
    var query = client.query("UPDATE drinks SET fullprice=($1), discountprice=($2) WHERE id=($3)", [fullprice, discountprice, drinkId]);
    res.end();
});

app.delete('/drinks/:id', function(req, res) {
    console.log("1-delete Drink");

    var drinkId = req.params.id;

    var client = new pg.Client(connectionString);
    client.connect();
    var query = client.query("DELETE FROM drinks WHERE id=($1)", [drinkId]);
    res.end();

});

app.get('/drinks/:id', function(req, res) {
  console.log("1-get Drink");
  var results = [];
  var drinkId = req.params.id;
  var client = new pg.Client(connectionString);
  client.connect();
  var query = client.query("SELECT name, barcode, fullprice, discountprice, quantity FROM drinks WHERE id=($1) ORDER BY id ASC", [drinkId]);
  query.on('row', function(row) {
    results.push(row);
  }, function(){console("loltest")});
  query.on('end', function() {
    return res.json(results);
  });
});

app.get('/drinks', function(req, res) {
  console.log("1-list Drinks");
  var results = [];
  var client = new pg.Client(connectionString);
  client.connect();
  var query = client.query("SELECT id, name, barcode, fullprice, discountprice, quantity FROM drinks ORDER BY id ASC");
  query.on('row', function(row) {
    results.push(row);
  });
  query.on('end', function() {
    return res.json(results);
  });
});

app.post('/drinks', function(req, res) {
    console.log("1-createDrink");
    var name = req.body.productname;
    var barcode = req.body.barcode;
    var fullprice = req.body.fullprice;
    var discountprice = req.body.discountprice;
    console.log(name);

    var client = new pg.Client(connectionString);
    client.connect();
    //TODO: sollte übrigens auch nicht zweimal existieren oder so... lol
    var query = client.query("INSERT INTO drinks(name, barcode, fullprice, discountprice, quantity) values($1, $2, $3, $4, $5)", [name, barcode, fullprice, discountprice, 0]);

    query.on('end', function() {
      client.end();
      return;
    });
    res.end();
});

var server = app.listen(8081, function () {

  var host = server.address().address
  var port = server.address().port

  updateTables();

  console.log("Example app listening at http://%s:%s", host, port)
})

function updateTables() {
  updateDepotTables();
  updateDrinkTables();
}

function updateDepotTables() {
  var client = new pg.Client(connectionString);
  client.connect();
  var query = client.query('CREATE TABLE IF NOT EXISTS depot(id SERIAL PRIMARY KEY, name VARCHAR(200) not null)');
  //TODO: sollte man auch gleich füllen...
  query.on('end', function() {
    client.end();
    return;
  });
}

function updateDrinkTables() {
  var client = new pg.Client(connectionString);
  client.connect();
  //var query = client.query('DROP TABLE drinks');
  var query = client.query('CREATE TABLE IF NOT EXISTS drinks(id SERIAL PRIMARY KEY, name VARCHAR(200) not null, barcode VARCHAR(200) not null, fullprice INTEGER not null, discountprice INTEGER not null, quantity INTEGER not null)');
  query.on('end', function() {
    client.end();
    return;
  });
}<commit_msg>extracting a persistence layer for the drinks database<commit_after>var express = require('express');
var app = express();
var bodyParser = require('body-parser');

var connectionString = process.env.DATABASE_URL || 'postgres://iceboxuser:testForIce@localhost:5432/icobox';

var persis = require('./iceboxpersistence/drinks_persistence.js');

app.use( bodyParser.json() );       // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({     // to support URL-encoded bodies
  extended: true
}));

/*routing.resources(app, controller_dir, "drinks", {}); // last param optional*/

app.put('/drinks/:id', function(req, res) {
    console.log("1-put Drink");
    var drinkId = req.params.id;

    var fullprice = req.body.fullprice;
    var discountprice = req.body.discountprice;
    var quantity = req.body.quantity;

    persis.updateDrink(fullprice, discountprice, drinkId, quantity);

    res.end();
});

app.delete('/drinks/:id', function(req, res) {
    console.log("1-delete Drink");

    var drinkId = req.params.id;

    persis.deleteDrinkById(drinkId);
    res.end();

});

app.get('/drinks/:id', function(req, res) {
  console.log("1-get Drink");
  var drinkId = req.params.id;
  return persis.getDrinkById(drinkId, function(results){
      return res.json(results);
    });
  });

app.get('/drinks', function(req, res) {
  console.log("1-list Drinks");
  var results = persis.getAllDrinks(function(results){
      return res.json(results);
  });
});

app.post('/drinks', function(req, res) {
    console.log("1-createDrink");
    var name = req.body.productname;
    var barcode = req.body.barcode;
    var fullprice = req.body.fullprice;
    var discountprice = req.body.discountprice;
    console.log(name);

    persis.insertNewDrink(name, barcode, fullprice, discountprice);
    res.end();
});

var server = app.listen(8081, function () {

  var host = server.address().address
  var port = server.address().port

  updateTables();

  console.log("IceBox listening at http://%s:%s", host, port)
})

function updateTables() {
  updateDepotTables();
  updateDrinkTables();
}

function updateDepotTables() {
  persis.setUpDepotTable();
}

function updateDrinkTables() {
  persis.setUpDrinksTable();
}
{'fraction_non_alphanumeric': 0.16881479405996078, 'fraction_numerical': 0.007705239562902774, 'mean_word_length': 3.8930774503084304, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '7398592', 'n_tokens_mistral': 2357, 'n_tokens_neox': 2304, 'n_words': 550}