-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexpressServices.js
166 lines (136 loc) · 5.19 KB
/
expressServices.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
var express = require('express');
var User = require('./js/User.js');
var Account = require('./js/Account.js');
var Transaction = require('./js/Transaction.js');
var fs = require('fs');
var path = require('path');
var app = express();
app.set('view engine', 'jade'); //Using Jade as our templating engine
app.get('/', function (req, res) {
res.sendFile('index.html', { root: path.join(__dirname, './views/') });
});
app.get('/registerUser', function (req, res) {
res.render('registration');
});
app.get('/login', function(req, res){
var email = req.query.email;
var password = req.query.password;
var fileName = "data/users/" + email.replace(/@/g, "_");
fs.readFile(fileName, function(err, data){
if(err){
console.log(err);
res.send("Error: User Not Found");
return;
}
var UserJson = data.toString();
var User = JSON.parse(UserJson);
console.log(User.password);
console.log(password);
if(User.password.toString() != password.toString()){
res.send("Error: Incorrect Credentials");
return;
}
res.send(UserJson);
});
});
app.get('/test/transactionView', function(req, res){
res.render('serverTestViews/transactionView');
});
app.get('/persistNewUser', function(req, res){
var user = req.query.user;
var email = req.query.email;
var password = req.query.password;
var newUser = new User(user, password, email);
var fileName = "data/users/" + email.replace(/@/g, "_");
fs.open(fileName, "wx", function(err, fd){
if(err){
console.log(err);
res.send("Error: User Already Exists");
return;
}
fs.write(fd, JSON.stringify(newUser), function(err, written, string){
if(err){
console.log(err);
res.send(err);
return;
}
});
});
console.log(JSON.stringify(newUser));
res.send("Success");
});
app.get('/postTransaction',function(req, res){
var userFromEmail = JSON.parse(req.query.userFromEmail);
var userFromPassword = JSON.parse(req.query.userFromPassword);
var transaction = JSON.parse(req.query.transaction);
fs.readFile("data/users/"+userFromEmail.replace(/@/,"_"), function(err, data){
if(err){
res.send("Error: User Not Found");
return;
}
var sender = JSON.parse(data.toString());
//Validate Credentials
if(sender.password !== userFromPassword){
res.send("Error: Incorrect Credentials");
return;
}
//If the transaction is a transfer and requires two parties, pull up the second party from persistence
if(transaction.kind === "transfer"){
fs.readFile("data/users/"+transaction.recipient.replace(/@/,"_"), function(err, data){
if(err){
res.send("Error: Recipient not found");
return;
}
var recipientObject = JSON.parse(data.toString());
recipientObject.accounts[0].amount += transaction.amount;
sender.accounts[0].amount -= transaction.amount;
transaction.date = new Date(); //Be careful using this function to reconstruct an account
sender.accounts[0].push(transaction);
recipientObject.accounts[0].transactions.push(
new Transaction(new Date(), "Transfer From: "+ sender.email, transaction.amount, undefined, "Deposit"));
//Persist changes to the recipient to the storage medium
fs.writeFile('/data/users/'+recipientObject.email.replace(/@/, "_"), JSON.stringify(recipientObject), function(){
if(err){
res.send("Error persisting changes to recipient, transaction canceled");
return;
}
console.log("Updated User: "+ recipientObject.email)
});
fs.writeFile('/data/users/'+sender.email.replace(/@/, "_"), JSON.stringify(sender), function(){
if(err){
res.send("Error persisting changes to your account, transaction canceled");
return;
}
console.log("Updated User: "+ sender.email)
});
});
}else if(transaction.kind === "Deposit"){
sender.accounts[0].amount += transaction.amount;
sender.accounts[0].transactions.push(new Transaction(new Date(), "Deposit", transaction.amount, undefined, "Deposit"));
fs.writeFile('/data/users/'+sender.email.replace(/@/, "_"), JSON.stringify(sender), function(){
if(err){
res.send("Error persisting changes to your account, transaction canceled");
return;
}
console.log("Updated User: "+ sender.email)
});
}else if(transaction.kind === "Withdrawal"){
sender.accounts[0].amount -= transaction.amount;
sender.accounts[0].transactions.push(new Transaction(new Date(), "Withdrawal", transaction.amount, undefined, "Withdrawal"));
fs.writeFile('/data/users/'+sender.email.replace(/@/, "_"), JSON.stringify(sender), function(){
if(err){
res.send("Error persisting changes to your account, transaction canceled");
return;
}
console.log("Updated User: "+ sender.email);
});
}
});
});
app.get('/createAccount', function(req, res){
});
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});