-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
135 lines (121 loc) · 2.79 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
const express = require("express");
const app = express();
const cors = require("cors");
const pool = require("./db");
const path = require("path");
const PORT = process.env.PORT || 5000;
// process.env.PORT
// Middleware
app.use(cors());
app.use(express.json());
if (process.env.NODE_ENV === "production") {
// serve static content
app.use(express.static(path.join(__dirname, "frontend/build")));
}
// Routes //
// get all users
app.get("/getcustomers", async (req, res) => {
console.log("query");
try {
const allCustomers = await pool.query(
"SELECT * FROM customers ORDER BY c_id"
);
console.log(allCustomers.rows);
res.json(allCustomers.rows);
res.json(mock_return);
} catch (err) {
console.log(err.message);
}
});
// add transaction
app.post("/transfer", async (req, res) => {
const { sender_name, receiver_name, amount } = req.body;
// console.log(sender_name, receiver_name, amount);
try {
const newTransaction = await pool.query(
"INSERT INTO transfers (sender, receiver, transfer_amount) VALUES ($1, $2, $3) RETURNING *",
[sender_name, receiver_name, amount]
);
const addAmount = await pool.query(
"UPDATE customers SET balance = balance + $1 WHERE name = $2",
[amount, receiver_name]
);
const subtractAmount = await pool.query(
"UPDATE customers SET balance = balance - $1 WHERE name = $2",
[amount, sender_name]
);
res.json(newTransaction);
} catch (err) {
console.log(err.message);
}
});
// get all transactions
app.get("/gethistory", async (req, res) => {
const allCustomers = await pool.query("SELECT * FROM transfers");
res.json(allCustomers.rows);
});
app.listen(PORT, () => {
console.log(`server has started at port ${PORT}`);
});
const mock_return = [
{
c_id: 1,
name: 'Ermengarde Nickolls',
email: 'enickolls0@yahoo.com',
balance: 10684
},
{
c_id: 2,
name: 'Harmon Pughsley',
email: 'hpughsley1@cam.ac.uk',
balance: 41251
},
{
c_id: 3,
name: 'Noel Di Gregorio',
email: 'ndi2@addthis.com',
balance: 26718
},
{
c_id: 4,
name: 'Berty Balham',
email: 'bbalham3@nytimes.com',
balance: 42490
},
{
c_id: 5,
name: 'Kitty Troppmann',
email: 'ktroppmann4@symantec.com',
balance: 8592
},
{
c_id: 6,
name: 'Barrett Shankle',
email: 'bshankle5@sciencedaily.com',
balance: 65750
},
{
c_id: 7,
name: 'Luella Lapham',
email: 'llapham6@baidu.com',
balance: 28375
},
{
c_id: 8,
name: 'Tori Clyant',
email: 'tclyant7@vinaora.com',
balance: 54420
},
{
c_id: 9,
name: 'Nickey Boulding',
email: 'nboulding8@google.ru',
balance: 38044
},
{
c_id: 10,
name: 'Allina Simao',
email: 'asimao9@youtu.be',
balance: 39923
}
]