<commit_before>var express = require('express');
var path = require('path');
var logger = require('morgan');
var bodyParser = require('body-parser');
var async = require('async');

var swig = require('swig');
var React = require('react');
var ReactDOM = require('react-dom/server');
var Router = require('react-router');
var RoutingContext = Router.RoutingContext;
var routes = require('./app/routes');
var pg = require("pg");
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('./database/database');
var arvdb;

console.log(process.env.DATABASE_URL);
if (process.env.DATABASE_URL !== undefined)
    arvdb = process.env.DATABASE_URL;
else {
    arvdb = new sqlite3.Database('./database/arvosteludb');
}

var app = express();

app.set('port', process.env.PORT || 3000);
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: false
}));
app.use(express.static(path.join(__dirname, 'public')));






app.post('/api/arvostelu', function(req, res, next) {
    var id = req.body.id;
    var arvosana = req.body.arvosana;

    if (process.env.DATABASE_URL !== undefined) {
        pg.connect(arvdb, function(err, client) {
            if (err) throw err;
            console.log('Connected to postgres! Getting schemas...');
            client.query('INSERT INTO arvostelu (yleid,arvosana) VALUES ($1,$2)', [id, arvosana],function(err,result) {
                //done();
                res.send({
                    message: "Arvostelu onnistui!"
                });
            });
        });

    } else {
        var stmt = arvdb.prepare("INSERT INTO arvostelu (yleid, arvosana) VALUES(?,?)");
        stmt.run(id, arvosana);
        stmt.finalize();
        res.send({
            message: "Arvostelu onnistui!"
        });
    }
});
app.get('/api/arvostelut/:id', function(req, res, next) {
    var id = req.params.id;
    if (process.env.DATABASE_URL === undefined) {
        arvdb.get("Select avg(arvosana) as ka from arvostelu where yleid = ?", id, function(err, row) {
            res.send(row);
        });
    }
    else {
        pg.connect(arvdb, function(err, client) {
            if (err) throw err;
            console.log('Connected to postgres! Getting schemas...');
            client.query('Select avg(arvosana) as ka from arvostelu where yleid = $1', [id] ,function(err, result) {
                //done();
                res.send(result.ka);
            });
        });
    }
});
app.get('/api/titles/all', function(req, res, next) {
    db.all('Select * from elokuvat LEFT JOIN omdb ON elokuvat.originalnimi=omdb.originalnimi', function(err, row) {
        res.send(row);
    });

});

app.get('/api/titles/:id', function(req, res, next) {
    var id = req.params.id;

    db.get('SELECT * FROM elokuvat LEFT JOIN omdb ON elokuvat.originalnimi=omdb.originalnimi where id = ?', id, function(err, row) {


        res.json({
            "originalnimi": row.originalnimi,
            "id": row.id,
            "suominimi": row.suominimi,<commit_msg>jotain postgresql säätöö ver 7<commit_after>var express = require('express');
var path = require('path');
var logger = require('morgan');
var bodyParser = require('body-parser');
var async = require('async');

var swig = require('swig');
var React = require('react');
var ReactDOM = require('react-dom/server');
var Router = require('react-router');
var RoutingContext = Router.RoutingContext;
var routes = require('./app/routes');
var pg = require("pg");
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('./database/database');
var arvdb;

console.log(process.env.DATABASE_URL);
if (process.env.DATABASE_URL !== undefined)
    arvdb = process.env.DATABASE_URL;
else {
    arvdb = new sqlite3.Database('./database/arvosteludb');
}

var app = express();

app.set('port', process.env.PORT || 3000);
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: false
}));
app.use(express.static(path.join(__dirname, 'public')));






app.post('/api/arvostelu', function(req, res, next) {
    var id = req.body.id;
    var arvosana = req.body.arvosana;

    if (process.env.DATABASE_URL !== undefined) {
        pg.connect(arvdb, function(err, client) {
            if (err) throw err;
            console.log('Connected to postgres! Getting schemas...');
            client.query('INSERT INTO arvostelu (yleid,arvosana) VALUES ($1,$2)', [id, arvosana],function(err,result) {
                //done();
                res.send({
                    message: "Arvostelu onnistui!"
                });
            });
        });

    } else {
        var stmt = arvdb.prepare("INSERT INTO arvostelu (yleid, arvosana) VALUES(?,?)");
        stmt.run(id, arvosana);
        stmt.finalize();
        res.send({
            message: "Arvostelu onnistui!"
        });
    }
});
app.get('/api/arvostelut/:id', function(req, res, next) {
    var id = req.params.id;
    if (process.env.DATABASE_URL === undefined) {
        arvdb.get("Select avg(arvosana) as ka from arvostelu where yleid = ?", id, function(err, row) {
            res.send(row);
        });
    }
    else {
        pg.connect(arvdb, function(err, client) {
            if (err) throw err;
            console.log('Connected to postgres! Getting schemas...');
            client.query('Select avg(arvosana) as ka from arvostelu where yleid like $1', [id] ,function(err, result) {
                //done();
                res.send(result);
            });
        });
    }
});
app.get('/api/titles/all', function(req, res, next) {
    db.all('Select * from elokuvat LEFT JOIN omdb ON elokuvat.originalnimi=omdb.originalnimi', function(err, row) {
        res.send(row);
    });

});

app.get('/api/titles/:id', function(req, res, next) {
    var id = req.params.id;

    db.get('SELECT * FROM elokuvat LEFT JOIN omdb ON elokuvat.originalnimi=omdb.originalnimi where id = ?', id, function(err, row) {


        res.json({
            "originalnimi": row.originalnimi,
            "id": row.id,
            "suominimi": row.suominimi,
{'fraction_non_alphanumeric': 0.16527024806965665, 'fraction_numerical': 0.003778544438968293, 'mean_word_length': 2.698663426488457, 'pattern_counts': {'":': 6, '<': 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': '1935371', 'n_tokens_mistral': 2092, 'n_tokens_neox': 2029, 'n_words': 430}