Skip to content

Commit 0c1edea

Browse files
author
jcob
committed
add endpoint using express and mongo
1 parent df6d15a commit 0c1edea

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

server/database/app.js

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,27 +48,47 @@ app.get('/fetchReviews', async (req, res) => {
4848

4949
// Express route to fetch reviews by a particular dealer
5050
app.get('/fetchReviews/dealer/:id', async (req, res) => {
51-
try {
52-
const documents = await Reviews.find({dealership: req.params.id});
53-
res.json(documents);
54-
} catch (error) {
55-
res.status(500).json({ error: 'Error fetching documents' });
56-
}
51+
try {
52+
const documents = await Reviews.find({dealership: req.params.id});
53+
res.json(documents);
54+
} catch (error) {
55+
res.status(500).json({ error: 'Error fetching documents' });
56+
}
5757
});
5858

5959
// Express route to fetch all dealerships
6060
app.get('/fetchDealers', async (req, res) => {
61-
//Write your code here
61+
//Write your code here
62+
try {
63+
const documents = await Dealerships.find();
64+
res.json(documents);
65+
} catch (error) {
66+
res.status(500).json({ error: 'Error fetching documents' });
67+
}
6268
});
6369

6470
// Express route to fetch Dealers by a particular state
6571
app.get('/fetchDealers/:state', async (req, res) => {
66-
//Write your code here
67-
});
72+
//Write your code here
73+
try {
74+
const stateParam = req.params.state;
75+
const documents = await Dealerships.find({ state: new RegExp(`^${stateParam}$`, 'i') });
76+
res.json(documents);
77+
} catch (error) {
78+
console.error('Error fetching dealers by state:', error);
79+
res.status(500).json({ error: 'Error fetching dealers by state' });
80+
}
81+
});
6882

6983
// Express route to fetch dealer by a particular id
7084
app.get('/fetchDealer/:id', async (req, res) => {
71-
//Write your code here
85+
//Write your code here
86+
try {
87+
const documents = await Dealerships.find({id: req.params.id});
88+
res.json(documents);
89+
} catch (error) {
90+
res.status(500).json({ error: 'Error fetching documents' });
91+
}
7292
});
7393

7494
//Express route to insert review

0 commit comments

Comments
 (0)