-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransactions.js
88 lines (77 loc) · 2.9 KB
/
transactions.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
module.exports = function () {
var express = require('express');
var router = express.Router();
function getTransactions(res, mysql, context, complete) {
mysql.pool.query("SELECT transaction_ID,account_ID,date_time,amount,transaction_type FROM transactions", function (error, results, fields) {
if (error) {
res.write(JSON.stringify(error));
res.end();
}
//populates the context
context.transactions = results;
complete();
});
}
/*dropdown*/
function getID(res, mysql, context, complete) {
mysql.pool.query("SELECT account_ID FROM accounts", function (error, results, fields) {
if (error) {
res.write(JSON.stringify(error));
res.end();
}
//populates the context
context.accounts = results;
complete();
});
}
/*Display all offers*/
router.get('/', function (req, res) {
let callbackCount = 0;
let context = {};
context.jsscripts = ["deletetransaction.js"];
let mysql = req.app.get('mysql');
getTransactions(res, mysql, context, complete);
getID(res, mysql, context, complete);
function complete() {
callbackCount++;
if (callbackCount >= 2) {
res.render('transactions', context);
}
}
});
/*Adds an offer, redirects to offers page after adding*/
router.post('/', function (req, res) {
console.log(req.body)
let mysql = req.app.get('mysql');
let sql = "INSERT INTO transactions (account_ID, amount, transaction_type) VALUES (? , ?, ?)";
let inserts = [req.body.accountID, req.body.amount, req.body.transactionType];
sql = mysql.pool.query(sql, inserts, function (error, results, fields) {
if (error) {
console.log(JSON.stringify(error))
res.write(JSON.stringify(error));
res.end();
} else {
res.redirect('/transactions');
}
});
});
/* For dev - Route to delete a transaction, simply returns a 202 upon success. Ajax will handle this. */
/*
router.delete('/:transaction_id', function (req, res) {
var mysql = req.app.get('mysql');
var sql = "DELETE FROM transactions WHERE transaction_ID = ?";
var inserts = [req.params.transaction_id];
sql = mysql.pool.query(sql, inserts, function (error, results, fields) {
if (error) {
console.log(error)
res.write(JSON.stringify(error));
res.status(400);
res.end();
} else {
res.status(202).end();
}
})
});
*/
return router;
}();