-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
229 lines (119 loc) · 3.72 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
const express =require("express");
const mysql = require("mysql");
const fs =require("fs");
const bodyParser= require("body-parser");
app =new express();
app.use(bodyParser.urlencoded({extended:true}))
app.use(bodyParser.json());
//Create Nysql Connection
var conn =mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'bank',
insecureAuth : true,
});
conn.connect(function(err){
if(err)
console.log(err);
else
console.log("connect established.....")
})
// to Load HomePage
app.get('/',function(req,res){
fs.readFile('./MainPage.html',function(error,data){
if(error)
res.write(error);
else
res.write(data);
res.end();
})
})
//Load Insert Data Page
app.get('/insertPage', function(req,res){
fs.readFile('./InsertData.html',function(error,data){
if(error)
res.write(error);
else
res.write(data);
res.end();
})
})
// insert new data in BankDetails Table.
app.post('/insertdata', function(req,res){
console.log(req.body);
var name=req.body.name;
var bal=req.body.balance;
var sqlquery="insert into BankDetails(name,balance) values('"+name+"',"+bal+")";
conn.query(sqlquery,function(err){
if(err)
throw err;
});
res.redirect('/');
res.end();
})
//Display All Transaction Page
app.get('/DisplayTransaction', function(req,res){
var sqlquery1="select * from Transactions";
conn.query(sqlquery1, (err, data)=>{
if(err) throw err;
console.log(data);
var table =''; //to store html table
//create html table with data from res.
for(var i=0; i<data.length; i++){
table +='<tr><td>'+ data[i].t_id +'</td><td>'+ data[i].u_id +'</td><td>'+ data[i].details +'</td></tr>';
}
table ='<table border="1"><tr><th>ID</th><th>User Id</th><th>Details</th></tr>'+ table +'</table>';
res.writeHead(200, {'Content-Type':'text/html; charset=utf-8'});
res.write(table);
res.end();
})
})
//Load Page to Transfer money
app.get('/TransactionPage', function(req,res){
fs.readFile('./TransactionPage.html',function(error,data){
if(error)
res.write(error);
else
res.write(data);
res.end();
})
})
//Trnasfer money From One Account To Another
app.post('/TransactionData', function(req,res){
//update Balance of User1
var sqlquery1="select * from Bankdetails where id= "+req.body.id1;
conn.query(sqlquery1 ,function(err,data1){
if(err)
throw err;
var bal1=data1[0].Balance-req.body.balance;//if Bal1 has come negative then also work ,negative Balance condition has not apply yet.
var sqlquery2="UPDATE BankDetails SET Balance="+bal1+" WHERE id="+req.body.id1;
conn.query(sqlquery2,function(err){
if(err)
throw err;
})
})
//update Balance of User2
var sqlquery3="select * from Bankdetails where id= "+req.body.id2;
conn.query(sqlquery3 ,function(err,data2){
if(err)
throw err;
var bal2=parseInt(data2[0].Balance) + parseInt(req.body.balance);
console.log(bal2);
var sqlquery4="UPDATE BankDetails SET Balance="+bal2+" WHERE id="+req.body.id2;
conn.query(sqlquery4,function(err){
if(err)
throw err;
})
})
//Add Transaction Details in Transactions Table
var sqlquery5="insert into Transactions(u_id,Details) values("+req.body.id1+ ",'Trnasfer money "+req.body.balance+" to "+req.body.id2+"')";
conn.query(sqlquery5,function(err){
if(err)
throw err;
});
//redirect on Home Page
res.redirect('/');
res.end();
})
app.listen(8080);