-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
332 lines (279 loc) · 9.29 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import express from "express";
import bodyParser from "body-parser";
import axios from "axios";
import moment from "moment";
import htm from "htm";
import vhtml from "vhtml";
const html = htm.bind(vhtml);
import { plot } from 'svg-line-chart'
const app = express();
const port = 3000;
const API_URL = "https://api.frankfurter.app";
app.set('view engine', 'ejs');
app.use(express.static("public"));
app.use(bodyParser.urlencoded({ extended: true }));
app.get("/", (req, res) => {
res.render("index.ejs")
});
app.get("/latest_rates", async (req, res) => {
try {
const response = await axios.get(API_URL+"/latest");
const result = response.data;
res.render("latest.ejs", { data: result.rates });
} catch (error) {
console.error("Failed to make request:", error.message);
res.render("latest.ejs", {
error: error.message,
});
}
});
app.post("/latest_rates", async (req, res) => {
try {
const currency = req.body.currencies;
const response = await axios.get(API_URL+`/latest?from=${currency}`);
const result = response.data;
res.render("latest.ejs", {
data: result.rates,
});
} catch (error) {
console.error("Failed to make request:", error.message);
res.render("latest.ejs", {
error: "No currencies that match your criteria.",
});
}
});
app.get("/conversion", async(req, res) => {
try {
const response = await axios.get(API_URL+"/latest");
const result = response.data;
res.render("conversion.ejs", { data: result.rates });
} catch (error) {
console.error("Failed to make request:", error.message);
res.render("conversion.ejs", {
error: error.message,
});
}
});
app.post("/conversion", async(req, res) => {
try {
const from_currency = req.body.currenciesF;
const to_currency=req.body.currenciesT;
const amount=req.body.amount;
const response2=await axios.get(API_URL+"/latest");
const result2=response2.data;
const checkDataType = +amount;
if ((from_currency==to_currency) && (isNaN(checkDataType))){
const statement2="Amount needs to be a number."
const statement1="Please select 2 different currencies. "
res.render("conversion.ejs", {
data: result2.rates,
info:statement1,
info_correctNum:statement2
});
}
else if (from_currency==to_currency){
const statement="Please select 2 different currencies. "
res.render("conversion.ejs", {
data: result2.rates,
info:statement
});
}
else if (isNaN(checkDataType)){
const statement="Amount needs to be a number."
res.render("conversion.ejs", {
data: result2.rates,
info_correctNum:statement
});
}
else{
const response = await axios.get(API_URL+`/latest?amount=${amount}&from=${from_currency}&to=${to_currency}`);
const result = response.data;
res.render("conversion.ejs", {
data: result2.rates,
From:from_currency,
To:to_currency,
amount:result.amount,
converted:Object.values(result.rates)[0],
});
}} catch (error) {
console.error("Failed to make request:", error.message);
res.render("conversion.ejs", {
error: "No currencies that match your criteria.",
});
}
});
app.get("/historical_rates", async(req, res) => {
try {
const response = await axios.get(API_URL+"/latest");
const result = response.data;
res.render("historical.ejs", { data: result.rates });
} catch (error) {
console.error("Failed to make request:", error.message);
res.render("historical.ejs", {
error: error.message,
});
}
});
app.post("/historical_rates", async(req, res) => {
try {
const curr_currency = req.body.currency;
const base_currency=req.body.base;
const start_date=req.body.start_date;
const end_date=req.body.end_date;
const response2=await axios.get(API_URL+"/latest");
const result2=response2.data;
const validStartDate = moment(start_date,'YYYY-MM-DD',true).isValid();
const validEndDate = moment(end_date,'YYYY-MM-DD',true).isValid();
var validTime_StartDate=false;
var validTime_EndDate=false;
if (validStartDate==true){
validTime_StartDate = moment(start_date).isAfter();
}
if (validEndDate==true){
validTime_EndDate = moment(end_date).isAfter();
}
if ((curr_currency==base_currency) && (validStartDate==false || validEndDate==false)){
const dateFormat_Error="Invalid Date Entered."
const statement="Selected Currency and Base Currency can't be the same. Please select 2 different currencies. "
res.render("historical.ejs", {
data: result2.rates,
info:statement,
info_dateFormat: dateFormat_Error
});
}
else if ((curr_currency==base_currency) && (validTime_EndDate==true || validTime_StartDate==true)){
const dateFormat_Error="Invalid Date Entered."
const statement="Selected Currency and Base Currency can't be the same. Please select 2 different currencies. "
res.render("historical.ejs", {
data: result2.rates,
info:statement,
info_dateFormat: dateFormat_Error
});
}
else if ((curr_currency==base_currency) && (start_date>=end_date)){
const statement="Selected Currency and Base Currency can't be the same. Please select 2 different currencies. "
const date_Error="Selected Initial Date can't be lesser than or equal to End Date. Please select Initial date atleast 5 days lesser than End Date for chart to be displayed."
res.render("historical.ejs", {
data: result2.rates,
info:statement,
info_date: date_Error
});
}
else if(validTime_EndDate==true|| validTime_StartDate==true){
const dateFormat_Error="Invalid Date Entered."
res.render("historical.ejs", {
data: result2.rates,
info_dateFormat: dateFormat_Error
});
}
else if (curr_currency==base_currency){
const statement="Selected Currency and Base Currency can't be the same. Please select 2 different currencies. "
res.render("historical.ejs", {
data: result2.rates,
info:statement
});
}
else if (validStartDate==false || validEndDate==false){
const dateFormat_Error="Invalid Date Entered."
res.render("historical.ejs", {
data: result2.rates,
info_dateFormat: dateFormat_Error
});
}
else if(start_date>=end_date){
const date_Error="Selected Initial Date can't be lesser than or equal to End Date. Please select Initial date atleast 5 days lesser than End Date for chart to be displayed."
res.render("historical.ejs", {
data: result2.rates,
info_date: date_Error
});
}
else{
const response = await axios.get(API_URL+`/${start_date}..${end_date}?from=${base_currency}&to=${curr_currency}`);
const result = response.data;
const arr1=Object.keys(result.rates);
const arr2=Object.values(result.rates).map(rate => rate[curr_currency]);
const x = arr1.map((d) => new Date(d));
const y = arr2;
if (arr1.length <5){
const dateGap_Error="Please select Initial date atleast 5 days lesser than End Date for chart to be displayed."
res.render("historical.ejs", {
data: result2.rates,
info_dateGap: dateGap_Error
});
}
// chart is a html string that can be rendered by the browser
const chart = plot(html)(
{ x, y },
{
props: {
style: "display:block; margin-left: auto; margin-right: auto;",
},
margin: 0.001,
width: 100,
height: 40,
title: "A line chart",
polygon: {
fill: 'none',
style: 'fill:url(#polygrad);',
strokeWidth: 0.01,
stroke: "white",
},
line: {
fill: "none",
strokeWidth: 0.1,
stroke: "black",
},
polygonGradient: {
offSet1: "0%",
stopColor1: "#ffffff00",
offSet2: "100%",
stopColor2: "#ffffff00",
},
xAxis: {
strokeWidth: 0.1,
stroke: "black",
},
yAxis: {
strokeWidth: 0.1,
stroke: "black",
},
xLabel: {
fontSize: 0.8,
name: "DATE",
writingMode: "vertical-lr",
},
yLabel: {
fontSize: 0.8,
name: "CURRENCY",
locale: "en-US",
},
xGrid: {
strokeWidth: 0.05,
stroke: "lightgrey",
},
yGrid: {
strokeWidth: 0.05,
stroke: "lightgrey",
},
yNumLabels: 10
}
)
res.render("historical.ejs", {
data: result2.rates,
currency:curr_currency,
base:base_currency,
startDate:start_date,
endDate:end_date,
history:result.rates,
chart:chart
});
}}catch (error) {
console.error("Failed to make request:", error.message);
res.render("historical.ejs", {
error: "No currencies that match your criteria.",
});
}
});
app.listen(port, () => {
console.log(`Server running on port: ${port}`);
});