-
Notifications
You must be signed in to change notification settings - Fork 0
/
appController.js
344 lines (312 loc) · 12.5 KB
/
appController.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
333
334
335
336
337
338
339
340
341
342
343
344
const express = require('express');
const appService = require('./appService');
const router = express.Router();
// ----------------------------------------------------------
// API endpoints
// Modify or extend these routes based on your project's needs.
router.get('/blackholes', async (req, res) => {
try {
const blackholes = await appService.getBlackHoleNames();
res.json({ blackholes });
} catch (err) {
res.status(500).send('Error fetching data');
}
});
router.get('/stars/:name', async (req, res) => {
const blackHoleName = req.params.name;
console.log(`Received request for stars: ${blackHoleName}`);
try {
const stars = await appService.getStarNames(blackHoleName);
res.json({ stars });
} catch (err) {
console.error('Error fetching stars:', err.message); // Log error message
res.status(500).json({ error: 'Error fetching stars' }); // Return JSON error response
}
});
router.get('/check-db-connection', async (req, res) => {
const isConnect = await appService.testOracleConnection();
if (isConnect) {
res.send('connected');
} else {
res.send('unable to connect');
}
});
// pass the name into router, then request API getBlackHoleDetails to get content of certain black hole
router.get('/blackholes/:name', async (req, res) => {
const blackHoleName = req.params.name;
// console.log(`Received request for black hole: ${blackHoleName}`); // Log the received parameter, this is for testing
try {
const blackhole = await appService.getBlackHoleDetails(blackHoleName);
if (blackhole) {
res.json({ blackhole });
} else {
res.status(404).send('Black hole not found'); // For test, the bug made me thought that I didn't pass the name properly
}
} catch (err) {
console.error('Error fetching black hole details:', err);
res.status(500).send('Error fetching data');
}
});
// pass the name into router, then request API getBlackHoleDetails to get content of certain black hole
router.get('/searchStars/:name', async (req, res) => {
const starName = req.params.name;
console.log(`Received request for star: ${starName}`); // Log the received parameter for debugging
try {
const star = await appService.getStarDetails(starName);
if (star) {
res.json({ star });
} else {
res.status(404).send('Star not found'); // Log and send error if the star is not found
}
} catch (err) {
console.error('Error fetching star details:', err);
res.status(500).send('Error fetching data');
}
});
router.post('/addblackholes', async (req, res) => {
const {name, mass, rotation_period, diameter} = req.body;
const values = name + ',' + mass + ', ' + rotation_period;
const otherValues = name + ',' + mass + ', ' + diameter;
console.log('Received new black hole:', name); // Log the received data, for testing
try {
await appService.insertTuple('BlackHole_Diameter', otherValues);
await appService.insertTuple('BlackHole', values);
res.send('Black hole created');
} catch (err) {
console.error('Error creating black hole:', err);
res.status(500).send('Error creating black hole');
}
});
router.post("/insert_Planet", async (req, res) => {
const {name, ed, mass, ESI, method, period, density} = req.body;
const values = name + ',' + ed + ',' + mass + ',' + ESI + ',' + method + ',' + period;
const otherValues = ed + ',' + mass + ',' + density;
console.log(values, otherValues);
try {
await appService.insertTuple('Planet_Densities', otherValues);
await appService.insertTuple('Planet', values);
res.send('Planet inserted');
} catch (err) {
console.error('Error inserting planet:', err);
res.status(500).send('Error inserting planet');
}
});
router.post("/insert_StarOrbitingPlanet", async (req, res) => {
const {planet_name, orbital_period, distance_to_centre, star_name,
SP_eccentricity, ed, mass, ESI, method, period, density } = req.body;
const values = planet_name + ',' + ed + ',' + mass + ',' + ESI + ',' + method + ',' + period;
const otherValues = ed + ',' + mass + ',' + density;
const values2 = planet_name + ',' + orbital_period + ',' + distance_to_centre + ',' + "NULL" + ',' + "NULL" + ',' + star_name + ',' + SP_eccentricity + ',' +"NULL"+ ',' + "NULL";
console.log(values);
try {
await appService.insertTuple('Planet_Densities', otherValues);
await appService.insertTuple('Planet', values);
await appService.insertTuple('OrbitingPlanet', values2);
res.send('Orbiting planet inserted');
} catch (err) {
console.error('Error inserting orbiting planet:', err);
res.status(500).send('Error inserting orbiting planet');
}
});
router.post("/insert_Star", async (req, res) => {
const {star_name, R2_star_name, luminosity, rotation_period, diameter, mass,
orbital_eccentricity, orbital_period, distance_to_companion, black_hole_name} = req.body;
console.log(star_name, R2_star_name, luminosity, rotation_period, diameter, mass,
orbital_eccentricity, orbital_period, distance_to_companion, black_hole_name)
try {
await appService.insert_Star(star_name, R2_star_name, luminosity, rotation_period, diameter, mass,
orbital_eccentricity, orbital_period, distance_to_companion, black_hole_name);
res.send('Star inserted');
} catch (err) {
console.error('Error inserting star:', err);
res.status(500).send('Error inserting star');
}
});
router.post("/delete_Star", async (req, res) => {
const {star_name, black_hole_name} = req.body;
try {
await appService.delete_Star(star_name, black_hole_name);
res.send('Star deleted');
} catch (err) {
console.error('Error deleting star:', err);
res.status(500).send('Error deleting star');
}
});
router.post("/countBHstars", async (req, res) => {
const {black_hole_name} = req.body;
try {
const values = await appService.aggregate_db("BlackHole, Star", "COUNT","DISTINCT star_name","",null,"BlackHole.black_hole_name = '" + black_hole_name + "' AND BlackHole.black_hole_name = Star.black_hole_name");
const count = values[0][0];
console.log(count);
res.json({ count });
} catch (err) {
console.error('Error counting stars:', err);
res.status(500).send('Error counting stars');
}
});
router.get('/getStarWithPlanets', async (req, res) => {
// given a star, return that star with its planets using outer join
/* Example return:
[
[
"Alpha Centauri A",
1227000000,
2.187e+30,
"Earth",
365.25,
"Sun"
]
]
attributes:
Star name, star diameter, star mass, planet_name, planet orbital_period, R2_star_name
*/
const {star_name} = req.query;
try {
const starData = await appService.getStarWithPlanets(star_name);
console.log('Received star data:', starData);
if (starData && starData.length > 0) {
res.json(starData);
} else {
console.log('No data found for the specified star.');
}
} catch (err) {
console.log('Error retrieving star and planet data');
}
});
router.get('/getPlanet', async (req, res) => {
// Given a planet name, return all its data and the celestial body it orbits
/*
Example return:
[
[
"Earth",
12742,
5.972e+24,
0.93,
"Radial Velocity",
24,
"Alpha Centauri A",
null,
null,
"Moon"
]
]
attributes:
planet_name, equatorial_diameter, mass, ESI, discovery_method, rotation_period,
star_name, white_dwarf_name, neutron_star_name, moon_name
In this example, Earth is orbiting the sun Alpha Centauri A, and has moon named Moon.
*/
const {planet_name} = req.query;
try {
const planetData = await appService.getPlanet(planet_name);
console.log('Received planet data:', planetData);
if (planetData && planetData.length > 0) {
res.json(planetData);
} else {
console.log('No data found for the specified planet.');
res.status(404).send('No data found for the specified planet.');
}
} catch (err) {
console.log('Error retrieving planet data:', err);
res.status(500).send('An error occurred while retrieving planet data.');
}
});
router.get('/starPlanetCount', async (req, res) => {
/*
Example return:
[
["Alpha Centauri B", 3],
["Sun", 7],
]
Attributes: star_name, number of planets
*/
const min = parseInt(req.query.min, 10);
try {
const starData = await appService.starPlanetCount(min);
console.log('Received star data:', starData);
if (starData && starData.length > 0) {
res.json(starData);
} else {
res.status(404).send('No stars found with more than the specified number of planets.');
}
} catch (err) {
console.error('Error retrieving star and planet count:', err);
res.status(500).send('An error occurred while retrieving star data.');
}
});
router.post('/findAsteroidStar', async (req, res) => {
try {
const star = await appService.divide_db("Star", "Asteroid", "star_name", "star_name", "star_name")
return res.json({star});
}
catch (err) {
console.error('Error executing query:', err);
res.status(500).send('An error occurred while retrieving data.');
}
});
router.get('/selectFromDB', async (req, res) => {
/*
PROJECTION and SELECTION
4 inputs:
table: string example: "Star"
colums: string "star_name, mass"
query: string, optional "star_name"
params: array, ["Sun"]
*/
const { table, columns, query, params } = req.query;
try {
if (!table || !columns) {
return res.status(400).send('Table and columns parameters are required.');
}
const paramsArray = params ? JSON.parse(params) : [];
const result = await appService.select_From_db(table, columns, query, paramsArray);
console.log('Query result:', result);
if (result && result.length > 0) {
res.json(result);
} else {
res.status(404).send('No data found for the specified query.');
}
} catch (err) {
console.error('Error executing query:', err);
res.status(500).send('An error occurred while retrieving data.');
}
});
router.get('/blackholeAvgDiameter', async (req, res) => {
// get average diameter, group by mass, only for those mass are greater than the given mass
// minMass: string, minimum mass of blackholes queried
// example return:
/*
[
[25,35],
[30,65]
]
Attributes: mass, avg_diameter
*/
const {minMass} = req.query;
if (!minMass || isNaN(minMass)) {
return res.status(400).send('Invalid or missing minMass parameter.');
}
try {
const result = await appService.blackhole_AVG_diameter(parseFloat(minMass));
if (result && result.length > 0) {
res.json(result);
} else {
res.status(404).send('No data found for the specified mass.');
}
} catch (err) {
console.error('Error retrieving black hole average diameter:', err);
}
});
router.post('/updateStar', async (req, res) => { // Change to POST
const { star_name, R2_star_name, luminosity, rotation_period, diameter, mass,
orbital_eccentricity, orbital_period, distance_to_companion, black_hole_name } = req.body; // Use req.body
try {
await appService.update_Star(star_name, R2_star_name, luminosity, rotation_period, diameter, mass,
orbital_eccentricity, orbital_period, distance_to_companion, black_hole_name);
res.send('Star updated');
} catch (err) {
console.error('Error updating star:', err);
res.status(500).send('An error occurred while updating star.');
}
});
module.exports = router;