-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
162 lines (137 loc) · 3.69 KB
/
app.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
const { google } = require('googleapis');
const express = require('express');
const ejs = require('ejs');
const app = express();
require('dotenv').config();
app.set("view engine", "ejs");
let sheets;
const setup = async () => {
const auth = await google.auth.getClient({ scopes: ['https://www.googleapis.com/auth/spreadsheets'] });
sheets = google.sheets({ version: 'v4', auth });
}
const getData = async () => {
const rangeSpecies = `Sheet1!A2:K`;
try {
const response = await Promise.all([
sheets.spreadsheets.values.get({
spreadsheetId: process.env.SHEET_ID,
range: rangeSpecies,
}),
]);
const speciesResponse = response[0].data.values;
var speciesData = speciesResponse.map(function (x) {
return {
_id: x[0],
name: x[1],
scientificName: x[2],
conservationStatus: x[3],
order: x[4],
population: x[5],
height: x[6],
weight: x[7],
length: x[8],
places: x[9],
description: x[10]
};
});
return speciesData;
} catch (e) {
console.log(e);
}
}
app.use(express.json());
app.use(express.static('public'));
app.get('/vulnerable', (req, res) => {
const status = 'vulnerable';
let selectedData = [];
const data = getData();
data.then((result) => {
for (let i = 0; i < result.length; i++) {
if (result[i].conservationStatus) {
if (result[i].conservationStatus.toLowerCase() === status) {
selectedData[i] = result[i];
}
}
}
res.render("explore", { result: selectedData, status: "Vulnerable" });
})
});
app.get('/endangered', (req, res) => {
const status = 'endangered';
let selectedData = [];
const data = getData();
data.then((result) => {
for (let i = 0; i < result.length; i++) {
if (result[i].conservationStatus) {
if (result[i].conservationStatus.toLowerCase() === status) {
selectedData[i] = result[i];
}
}
}
res.render("explore", { result: selectedData, status: "Endangered" });
})
});
app.get('/near-threatened', (req, res) => {
const status = 'near threatened';
let selectedData = [];
const data = getData();
data.then((result) => {
for (let i = 0; i < result.length; i++) {
if (result[i].conservationStatus) {
if (result[i].conservationStatus.toLowerCase() === status) {
selectedData[i] = result[i];
}
}
}
res.render("explore", { result: selectedData, status: "Near Threatened" });
})
});
app.get('/least-concern', (req, res) => {
const status = 'least concern';
let selectedData = [];
const data = getData();
data.then((result) => {
for (let i = 0; i < result.length; i++) {
if (result[i].conservationStatus) {
if (result[i].conservationStatus.toLowerCase() === status) {
selectedData[i] = result[i];
}
}
}
res.render("explore", { result: selectedData, status: "Least Concern" });
})
});
app.get('/critically-endangered', (req, res) => {
const status = 'critically endangered'
let selectedData = [];
const data = getData();
data.then((result) => {
for (let i = 0; i < result.length; i++) {
if (result[i].conservationStatus) {
if (result[i].conservationStatus.toLowerCase() === status) {
selectedData[i] = result[i];
}
}
}
res.render("explore", { result: selectedData, status: "Critically Endangered" });
})
});
app.get('/explore', (req, res) => {
const data = getData();
data.then(function (result) {
res.render("explore", { result: result, status: "All" });
})
});
app.get('/:id', (req, res) => {
const data = getData();
data.then(function (result) {
res.render("info", { result: result, _id: req.params.id });
})
});
app.get("/", (req, res) => {
res.render("index");
});
app.listen(process.env.PORT || 8080, () => {
setup();
console.log(`Server is listening on port ${process.env.PORT || 8080}`);
});