forked from 1125f16/badbank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
167 lines (141 loc) · 3.65 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
// setup server
// YOUR CODE
var express = require('express');
var app = express();
var low = require('lowdb');
var fs = require('lowdb/adapters/FileSync');
var adapter = new fs('db.json');
var db = low(adapter);
app.use(express.static('public'));
db.defaults({ account: []}).write();
// setup directory used to serve static files
// YOUR CODE
// setup data store
// YOUR CODE
// required data store structure
// YOUR CODE
/*
{
accounts:[
{name : '',
email : '',
balance : 0,
password : '',
transactions: []}
]
}
*/
app.get('/account/create/:name/:email/:password', function (req, res) {
// YOUR CODE
console.log ("request received"+req.params.name)
var account = {
"name" : req.params.name,
"email" : req.params.email,
"password" : req.params.password,
"balance":0
};
var check = db.get('account')
.find({ email: req.params.email })
.value()
console.log ("print"+JSON.stringify(check))
// Create account route
// return success or failure string
//if()
if(!check)
{
db.get("account").push(account).write();
console.log(db.get('account').value());
var obj={status :"success"}
res.send(obj);
}
else{
var obj={status :"account already exists"}
res.send(obj);
}
// var obj=(status :"failure"}
// res.send(obj);
});
app.get('/account/login/:email/:password', function (req, res) {
console.log("Request Login");
var check = db.get('account')
.find({ email: req.params.email })
.value()
if(check)
{
var obj={status :"success"}
res.send(obj);
}
else{
var obj={status :"account doesn't exist"}
res.send(obj);
}
// If success, return account object
// If fail, return null
});
app.get('/account/get/:email', function (req, res) {
// YOUR CODE
// Return account based on email
});
app.get('/account/deposit/:email/:amount', function (req, res) {
console.log("Request Deposit");
var check = db.get('account')
.find({ email: req.params.email })
.value()
if(check)
{
db.get('account')
.find({email: req.params.email})
.assign({ balance: req.params.amount })
.write()
var obj={status :"Woot! Success"}
res.send(obj);
}
else{
var obj={status :"Cannot deposit with this account"}
res.send(obj);
}
// Deposit amount for email
// return success or failure string
});
app.get('/account/withdraw/:email/:amount', function (req, res) {
console.log("Request Withdraw");
var check = db.get('account')
.find({ email: req.params.email })
.value()
if(check){
console.log("Request Withdraw" + JSON.stringify(check));
var amount = req.params.amount
var balance = check.balance
var newbalance = balance-amount
console.log("New Balance" + newbalance)
if(newbalance>=0){
db.get('account')
.find({email: req.params.email})
.assign({ balance: newbalance })
.write()
var obj={status :"Woot! Success! Your New Balance is:" + newbalance}
res.send(obj);
}
else{
var obj={status :"Not Enough Funds. Your Balance is:" + balance}
res.send(obj)
}
}
else{
var obj={status :"Account Not Found"}
res.send(obj)
}
// Withdraw amount for email
// return success or failure string
});
app.get('/account/transactions/:email', function (req, res) {
// YOUR CODE
// Return all transactions for account
});
app.get('/account/all', function (req, res) {
// YOUR CODE
// Return data for all accounts
});
app.listen(3000, function(){
console.log('Running on port 3000');
});