-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
103 lines (82 loc) · 2.52 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
var express = require('express');
var app = express();
var cors = require('cors');
var dal = require('./dal.js');
const e = require('express');
// used to serve static files from public directory
app.use(express.static('public'));
app.use(cors());
// create user account
app.get('/account/create/:name/:email/:password', function (req, res) {
// check if account exists
dal.find(req.params.email).
then((users) => {
// if user exists, return error message
if(users.length > 0){
console.log('User already in exists');
res.send('User already in exists');
}
else{
// else create user
dal.create(req.params.name,req.params.email,req.params.password).
then((user) => {
console.log(user);
res.send(user);
});
}
});
});
// login user
app.get('/account/login/:email/:password', function (req, res) {
dal.find(req.params.email).
then((user) => {
// if user exists, check password
if(user.length > 0){
if (user[0].password === req.params.password){
res.send(user[0]);
}
else{
res.send('Login failed: wrong password');
}
}
else{
res.send('Login failed: user not found');
}
});
});
// find user account
app.get('/account/find/:email', function (req, res) {
dal.find(req.params.email).
then((user) => {
console.log(user);
res.send(user);
});
});
// find one user by email - alternative to find
app.get('/account/findOne/:email', function (req, res) {
dal.findOne(req.params.email).
then((user) => {
console.log(user);
res.send(user);
});
});
// update - deposit/withdraw amount
app.get('/account/update/:email/:amount', function (req, res) {
var amount = Number(req.params.amount);
dal.update(req.params.email, amount).
then((response) => {
console.log(response);
res.send(response);
});
});
// all accounts
app.get('/account/all', function (req, res) {
dal.all().
then((docs) => {
console.log(docs);
res.send(docs);
});
});
const port = process.env.PORT || 3000
app.listen(port);
console.log('NOW! Running on port: ' + port);