-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
177 lines (151 loc) · 6.51 KB
/
Copy pathapi.js
File metadata and controls
177 lines (151 loc) · 6.51 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import express from "express"
import bodyParser from "body-parser"
import { readFileSync } from "fs";
import { isBooleanObject } from "util/types";
import { type } from "os";
// Load datasets into memory
const asteroids_comets = JSON.parse(readFileSync("./json/asteroids_comets.json", "utf-8"));
const blackholes = JSON.parse(readFileSync("./json/blackholes.json", "utf-8"));
const constellations = JSON.parse(readFileSync("./json/constellations.json", "utf-8"));
const galaxies = JSON.parse(readFileSync("./json/galaxies.json", "utf-8"));
const planets = JSON.parse(readFileSync("./json/planets.json", "utf-8"));
const stars = JSON.parse(readFileSync("./json/stars.json", "utf-8"));
const typesOfAsteroids_Comets = new Set(asteroids_comets.map(o => o.type));
const typesOfBlackholes = new Set(blackholes.map(o => o.type));
const typesOfConstellations = new Set(constellations.map(o => o.type));
const typesOfGalaxies = new Set(galaxies.map(o => o.type));
const typesOfPlanets = new Set(planets.map(o => o.type));
const typesOfStars = new Set(stars.map(o => o.type));
const typeSets = {
asteroids_comets: typesOfAsteroids_Comets,
blackholes: typesOfBlackholes,
constellations: typesOfConstellations,
galaxies: typesOfGalaxies,
planets: typesOfPlanets,
stars: typesOfStars
};
const cosmicData = {
asteroids_comets : asteroids_comets,
blackholes : blackholes,
constellations : constellations,
galaxies : galaxies,
planets : planets,
stars : stars
}
function generateRandomObject(obj){
const keys = Object.keys(obj);//array containing the keys of the object
const randomKey = Math.trunc(Math.random() * keys.length)//gets a random index from the keys array
const randomDataSet = obj[keys[randomKey]];
const randomObject = randomDataSet[Math.trunc(Math.random() * randomDataSet.length)]
randomObject.category = keys[randomKey];
return randomObject;
}
function handlePagination(page,limit,results){
const pageNum = Math.max(1, parseInt(page) || 1);
const limitNum = Math.max(1, parseInt(limit) || 10);
const startIndex = (pageNum - 1) * limitNum
const endIndex = startIndex + limitNum;
return results.slice(startIndex,endIndex)
}
const API_URL = "http://localhost:4000"
const app = express();
const port = 4000;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
//Get a random cosmic-object from any category
app.get("/cosmic-objects/random",(req,res)=>{
const randomObjectToBeSent = generateRandomObject(cosmicData)
res.json(randomObjectToBeSent)
})
//Get a random object from a specific category
app.get('/cosmic-objects/random/:category',(req,res)=>{
const category = req.params.category.toLowerCase();
const dataSet = cosmicData[category];
if(!dataSet){
return res.status(404).json({error: "category not found"})
}
res.json(dataSet[Math.trunc(Math.random() * dataSet.length)])
})
// filter by type property and return and array objects that fulfil that condition
app.get('/cosmic-objects/filter/:category', (req, res) => {
const category = req.params.category.toLowerCase();
const { type , page = 1 , limit = 10 } = req.query;
console.log(type);
const dataSet = cosmicData[category];
if (!dataSet) {
return res.status(404).json({ error: "category not found" });
}
// Check if type is valid for this category
if (type) {
const validTypes = typeSets[category];
if (!validTypes || !validTypes.has(type)) {
return res.status(404).json({ error: "unavailable type for this category" });
}
}
// Apply filters dynamically
let results = dataSet.filter(obj => {
if (type && obj.type.toLowerCase() !== type.toLowerCase()) return false;
return true;
});
const paginatedResults = handlePagination(page,limit,results);
res.json(paginatedResults);
});
//Get a specific object by id
app.get('/cosmic-objects/:category/:id',(req,res)=>{
//catch the parameters
const category = req.params.category.toLowerCase();
const id = parseInt(req.params.id)
console.log(category , id);
//select the category
const dataSet = cosmicData[category];
if (!dataSet){
return res.status(404).json({error: "category not found"})
}
//find the object with the required id in that category
const thatObject = dataSet.find(object=> object.id === id);
if(!thatObject){
return res.status(404).json({error: "object with the specified id not found"})
}
res.json(thatObject);
})
//Get all objects of a specific category with pagination
app.get('/cosmic-objects/:category',(req,res)=>{
const category =req.params.category;
const { page = 1 , limit = 10 } = req.query;
const dataSet = cosmicData[category];
if(!dataSet){
return res.status(404).json({error: "category not found."})
}
const paginatedDataSet = handlePagination(page,limit,dataSet);
res.json(paginatedDataSet)
})
//Search by name the entire database for cosmic object that match the provided name
app.get('/cosmic-objects',(req,res)=>{
const { objectName , category , page = 1 , limit = 10 } = req.query;
console.log(objectName,category);
if(objectName){
if(category){//if category is provided
const dataSet = cosmicData[category];//pick the dataSet
if(!dataSet)//check if the dataset with the provided category exists
return res.status(404).json({error: "category not found."})
//search from a particular category and collect the matches
const matches = dataSet.filter((obj) => {
return obj.name.toLowerCase().includes(objectName.toLowerCase())})
res.json(handlePagination(page,limit,matches))
} else {
//search the entire database for matches
const matches = [];
for (const key in cosmicData){
const results = cosmicData[key].filter((obj) => {
return obj.name.toLowerCase().includes(objectName.toLowerCase())})
matches.push(...results)
}
res.json(handlePagination(page,limit,matches));
}
} else {
res.status(400).json({error: "objectName must be provided"})
}
})
app.listen(port,()=>{
console.log(`server is listening on port ${port}...\nclick ${API_URL} to open the browser.` );
})