-
Notifications
You must be signed in to change notification settings - Fork 515
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Week 18: mongoDB API #519
base: master
Are you sure you want to change the base?
Week 18: mongoDB API #519
Conversation
server.js
Outdated
app.get("/avocado-sales/region/:region", async (req, res) => { | ||
const { region } = req.params; | ||
try { | ||
const sales = await AvocadoSale.find({ region: new RegExp(region, "i") }); | ||
if (sales.length === 0) { | ||
return res.status(404).send("No sales data found for this region"); | ||
} | ||
res.json(sales); | ||
} catch (error) { | ||
console.error("Error retrieving sales by region:", error); | ||
res.status(500).send("Server error"); | ||
} | ||
}); | ||
|
||
// Route to get sales by date | ||
app.get("/avocado-sales/date/:date", async (req, res) => { | ||
const { date } = req.params; | ||
try { | ||
const sales = await AvocadoSale.find({ date }); | ||
if (sales.length === 0) { | ||
return res.status(404).send("No sales data found for this date"); | ||
} | ||
res.json(sales); | ||
} catch (error) { | ||
console.error("Error retrieving sales by date:", error); | ||
res.status(500).send("Server error"); | ||
} | ||
}); | ||
|
||
// Route to get sales by price range | ||
app.get("/avocado-sales/price-range", async (req, res) => { | ||
const { min, max } = req.query; | ||
try { | ||
const sales = await AvocadoSale.find({ | ||
averagePrice: { $gte: Number(min) || 0, $lte: Number(max) || Infinity }, | ||
}); | ||
if (sales.length === 0) { | ||
return res.status(404).send("No sales data found in this price range"); | ||
} | ||
res.json(sales); | ||
} catch (error) { | ||
console.error("Error retrieving sales by price range:", error); | ||
res.status(500).send("Server error"); | ||
} | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These should all be query params under your main endpoint to make it more RESTful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I was a bit quick on the button there, I can't seem to get your deployed link working 👀 Can you please have a look?
Sorry that I'm going back and forth (and GitHub's UI doesn't let me remove my previous comments 😅). I cannot get your deployed link to work. I get "Server error". Can you get it to work? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See comment
https://project-mongo-api-uref.onrender.com