-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
95 lines (81 loc) · 2.73 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const express = require('express');
const app = express();
const fs = require('fs');
fs.readFile('password', 'utf8', function (err, data) {
if (err) {
return console.log(err);
}
config.password = data;
console.log('Set password');
});
var Connection = require('tedious').Connection;
var Request = require('tedious').Request;
var config = {
userName: 'catfin_user',
password: 'haha no',
server: 'db-personal-00.crjmlasrvtc6.us-west-2.rds.amazonaws.com',
options: {database: 'catfin'}
};
function runSql(query, res) {
console.log('Received query ' + query);
var connection = new Connection(config);
//connection.on('debug', function (err) {
// console.log('debug:', err);
//});
connection.on('connect', function (err) {
if (err) {
console.log(err);
}
// If no error, then good to proceed.
executeStatement();
});
function executeStatement() {
var result = "";
request = new Request(query, function (err, rowCount) {
if (err) {
console.log(err);
} else {
if ('' == result) {
result = '{}'
}
console.log('Returning: ' + result);
connection.close();
//do response
res.append('Content-Type', 'application/json');
res.append('Access-Control-Allow-Origin', '*');
res.write(result);
res.end();
}
});
request.on('row', function (columns) {
columns.forEach(function (column) {
result += column.value;
});
});
connection.execSql(request);
}
}
app.get('/goal', function (req, res) {
runSql('exec dp_goal ' + req.query.user, res);
});
app.get('/cats', function (req, res) {
runSql('exec dp_cats ' + req.query.user, res);
});
app.get('/transaction', function (req, res) {
runSql('exec dp_transaction ' + req.query.user, res);
});
app.get('/category', function (req, res) {
runSql('exec dp_category', res);
});
app.post('/create/user', function (req, res) {
runSql('exec up_create_user ' + req.query.username, res);//
});
app.post('/create/goal', function (req, res) {
runSql('declare @temp money = CAST(' + req.query.goalamt + ' as MONEY);exec up_create_goal ' + req.query.user + ',' + req.query.goalname + ',@temp', res);
});
app.post('/transaction', function (req, res) {
runSql('declare @temp money = CAST(' + req.query.transamt + ' as MONEY);exec up_do_transaction ' + req.query.user + ',' + req.query.transname + ',@temp,' + req.query.transcat, res);
});
app.listen(8080, function () {
console.log('Listening on port 8080');
});