fixed cars_make
This commit is contained in:
parent
f477877e91
commit
f64e1f1f56
@ -48,28 +48,36 @@ exports.getModelsByMake = async (req, res) => {
|
||||
};
|
||||
|
||||
|
||||
|
||||
exports.getYearsByModel = async (req, res) => {
|
||||
try {
|
||||
const { model } = req.query;
|
||||
const docs = await CarModel.find({ model: model })
|
||||
.select("years")
|
||||
.lean();
|
||||
|
||||
if (docs.length === 0) {
|
||||
return res.status(404).json({ message: "No years found for this model" });
|
||||
}
|
||||
|
||||
const years = docs[0].years.map((year) => ({
|
||||
label: year,
|
||||
}));
|
||||
|
||||
return res.status(200).json(years);
|
||||
} catch (error) {
|
||||
console.error("Server error retrieving years:", error);
|
||||
return res.status(500).json({
|
||||
message: "Server error retrieving years",
|
||||
error: error.message,
|
||||
});
|
||||
try {
|
||||
const { make, model } = req.query;
|
||||
if (!make || !model) {
|
||||
return res
|
||||
.status(400)
|
||||
.json({ message: "Missing required `make` and/or `model` query parameter" });
|
||||
}
|
||||
}
|
||||
|
||||
// Grab all distinct year values for that make+model combo
|
||||
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);
|
||||
|
||||
} catch (error) {
|
||||
console.error("Server error retrieving years:", error);
|
||||
return res.status(500).json({
|
||||
message: "Server error retrieving years",
|
||||
error: error.message,
|
||||
});
|
||||
}
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user