forked from JacobRajah/Stockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
160 lines (143 loc) · 4.28 KB
/
server.js
File metadata and controls
160 lines (143 loc) · 4.28 KB
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
const express = require('express');
const bodyParser = require('body-parser');
require('dotenv').config(); //set env
var path = require('path');
const port = process.env.PORT || 5000;
const app = express();
app.use(bodyParser.json());
const data = require('./server/accessDropbase');
/* Here users can see a list of their transactions with
username stock, stock price bought, # of shares,
company used to buy. Store fake todays stock to calc
revenue so we can show profits */
let transactions = [
{
"username": "foobar",
"stock": "AMZN",
"price_bought": 3175,
"date_bought": "2020-01-08",
"shares": 0.5,
"application": "Bank",
"transaction": "buy"
},
{
"username": "foobar",
"stock": "AMZN",
"price_bought": 3100,
"date_bought": "2020-01-11",
"shares": 2,
"application": "Bank",
"transaction": "buy"
},
{
"username": "foobar",
"stock": "MSFT",
"price_bought": 218,
"date_bought": "2020-01-11",
"shares": 2,
"application": "Bank",
"transaction": "buy"
},
{
"username": "foobar",
"stock": "AMZN",
"price_bought": 3145,
"date_bought": "2020-01-11",
"shares": 1,
"application": "Bank",
"transaction": "buy"
},
{
"username": "foobar",
"stock": "NFLX",
"price_bought": 511,
"date_bought": "2020-01-13",
"shares": 2,
"application": "Bank",
"transaction": "buy"
},
{
"username": "foobar",
"stock": "AMZN",
"price_bought": 3100,
"date_bought": "2020-01-11",
"shares": 2,
"application": "Bank",
"transaction": "buy"
},
]
let today_prices = {
"AMZN": 3200,
"NVDA": 514.38,
"AMD": 88.21,
"FB": 251.36,
"MSFT": 212.65,
"GOOGL": 1727.62,
"NFLX": 497.98
}
/* Functions for when using built version of React scripts.
if you want to test production execute <npm run build> then
execute <node server.js>*/
app.use(express.static(path.join(__dirname, 'client/build')));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'client/build', 'index.html'));
});
/* Place all get and post paths here: */
/* Base Example for getting data*/
app.get('/stockData', (req, res) => {
data.getStockData('googl').then(resp => res.send(resp)
).catch(err => console.log(err))
});
/* Client specifies the metric in the url */
app.post('/stockReq/:metric', (req, res) => {
const stockSearch = (req.body).stock
var metric = req.params.metric
data.getStockData(stockSearch, metric).then(resp => res.send(resp)
).catch(err => console.log(err))
});
/* User adds transaction using form */
app.post('/transaction', (req, res) => {
let username = "foobar";
let stock = (req.body).stock;
let price_bought = (req.body).price_bought;
let date_bought = (req.body).date_bought;
let shares = (req.body).shares;
let application = (req.body).application;
let transaction = (req.body).transaction;
transactions.push({
username: username,
stock: stock,
price_bought: price_bought,
date_bought: date_bought,
shares: shares,
application: application,
transaction: transaction
})
});
/* Get all transactions */
app.get('/transactions', (req, res) => {
let trans = data.getTransactions(transactions);
res.send(trans);
});
/* Get 5 transactions */
app.get('/transactions/5', (req, res) => {
let trans = data.getTransactions(transactions);
res.send(trans.slice(0,5));
});
/* Get all transactions */
app.get('/transactionsReturns', (req, res) => {
data.getTransactionReturns(transactions).then(resp => res.send(resp)
).catch(err => console.log(err))
});
/* Get all transaction returns */
app.get('/returns', (req, res) => {
let trans = data.getTransactionReturns(transactions, today_prices);
res.send(trans);
});
/* Client requests basic data for a stock */
app.post('/basicStock', (req, res) => {
const stockSearch = (req.body).stock
data.getStockBasics(stockSearch).then(resp => res.send(resp)
).catch(err => console.log(err))
})
app.listen(port, () => console.log(`Server started on port ${port}`));