fixed cars_make
This commit is contained in:
parent
f477877e91
commit
f64e1f1f56
@ -48,23 +48,31 @@ exports.getModelsByMake = async (req, res) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
exports.getYearsByModel = async (req, res) => {
|
exports.getYearsByModel = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { model } = req.query;
|
const { make, model } = req.query;
|
||||||
const docs = await CarModel.find({ model: model })
|
if (!make || !model) {
|
||||||
.select("years")
|
return res
|
||||||
.lean();
|
.status(400)
|
||||||
|
.json({ message: "Missing required `make` and/or `model` query parameter" });
|
||||||
if (docs.length === 0) {
|
|
||||||
return res.status(404).json({ message: "No years found for this model" });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const years = docs[0].years.map((year) => ({
|
// Grab all distinct year values for that make+model combo
|
||||||
label: year,
|
const rawYears = await CarModel.distinct("year", { make, model });
|
||||||
}));
|
if (rawYears.length === 0) {
|
||||||
|
return res
|
||||||
|
.status(404)
|
||||||
|
.json({ message: "No years found for this make/model combination" });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert numbers to strings, sort descending, and wrap as { label }
|
||||||
|
const payload = rawYears
|
||||||
|
.map((y) => y.toString())
|
||||||
|
.sort((a, b) => Number(b) - Number(a))
|
||||||
|
.map((y) => ({ label: y }));
|
||||||
|
|
||||||
|
return res.status(200).json(payload);
|
||||||
|
|
||||||
return res.status(200).json(years);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Server error retrieving years:", error);
|
console.error("Server error retrieving years:", error);
|
||||||
return res.status(500).json({
|
return res.status(500).json({
|
||||||
@ -72,4 +80,4 @@ exports.getYearsByModel = async (req, res) => {
|
|||||||
error: error.message,
|
error: error.message,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
Loading…
x
Reference in New Issue
Block a user