-
Notifications
You must be signed in to change notification settings - Fork 17
/
server.js
122 lines (104 loc) · 4 KB
/
server.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
const express = require('express')
const Ticker = require('./data')
const port = process.env.PORT || 3005
const app = express()
const fs = require('fs')
app.get('/', (req, res) => {
res.send('working')
res.end()
})
app.get('/keyword/:search/limit/:max', (req, res) => {
console.log("--------/keyword-with-Limit-applied-----------"+req.params.search+"---limit:"+req.params.max)
let search = req.params.search;
let maxResults = Number.parseInt(req.params.max)
console.log(maxResults)
if(!isNaN(maxResults)){
search = search.toLowerCase();
res.setHeader('Content-Type', 'application/json');
const g =Ticker.TickerSymbols.filter( f =>
(JSON.stringify(f).toLowerCase().indexOf(search) !== -1)
)
if(maxResults > g.length || maxResults == '0'){
maxResults = g.length
}
let count = 0;
limited = []
while (count < maxResults && count < g.length){
limited.push(g[count])
count++
}
res.status(200).json({results:[limited],message:""});
}else{
console.log("Limit must be a number")
res.status(400).json({results:[],message:"Limit must be a number"});
}
res.end()
})
app.get('/keyword/:search', (req, res) => {
console.log("--------/keyword-----------"+req.params.search)
let search = req.params.search;
search = search.toLowerCase();
res.setHeader('Content-Type', 'application/json');
const g =Ticker.TickerSymbols.filter( f =>
(JSON.stringify(f).toLowerCase().indexOf(search) !== -1)
)
res.status(200).json({results:g,message:""});
res.end()
})
app.get('/add/:symbol/:name', (req, res) => {
// Example
// {"symbol":"A","name":"Agilent Technologies Inc."}
console.log("--------/symbol-----------"+req.params.symbol)
console.log("--------/name-----------"+req.params.name)
let symbol = req.params.symbol;
let name = req.params.name;
symbol = symbol.toLowerCase();
res.setHeader('Content-Type', 'application/json');
const newArray = Ticker.TickerSymbols;
// CHECK IF SYMBOL ALREADY EXISTS:
const isDuplicate = newArray.filter( f =>
(f.symbol.toLowerCase() === symbol)
)
if (!isDuplicate.length){
// ADD NEW OBJECT
const new_Symbol = {symbol:symbol.toUpperCase(), name:name};
newArray.push(new_Symbol)
// SORT
newArray.sort((a,b) => (a.symbol > b.symbol) ? 1 : ((b.symbol > a.symbol) ? -1 : 0))
// WRITE TO FILE
fs.writeFile('./data.js', `const TickerSymbols = ${JSON.stringify(newArray)} \n\nexports.TickerSymbols = TickerSymbols;`, (err) => {
if (err) throw err;
console.log('The file has been saved!');
});
res.status(200).json({results:[new_Symbol],message:"Added, please restart server"});
} else {
console.log(isDuplicate)
res.status(400).json({results:[],message:"Symbol already found"});
}
res.end()
})
app.get('/delete/:symbol', (req, res) => {
console.log("--------/symbol-----------"+req.params.symbol)
let symbol = req.params.symbol;
symbol = symbol.toLowerCase();
res.setHeader('Content-Type', 'application/json');
const newArray = Ticker.TickerSymbols.filter( f =>
f.symbol.toLowerCase() !== symbol
)
// CHECK IF VALUE IS NOT FOUND
if (Ticker.TickerSymbols.length === newArray.length){
res.status(400).json({results:[],message:"Requested Symbol not Found"});
} else {
fs.writeFile('./data.js', `const TickerSymbols = ${JSON.stringify(newArray)} \n\nexports.TickerSymbols = TickerSymbols;`, (err) => {
if (err) throw err;
console.log('The file has been saved!');
});
res.status(200).json({results:[],message:`Deleted ${symbol} , please restart server`});
}
res.end()
})
// TODO: app.post route to add multiple symbols
app.listen(port, err => {
if (err) throw err
console.log(`> Ready On Server http://localhost:${port}`)
})