26 lines
628 B
JavaScript
26 lines
628 B
JavaScript
|
|
const db = require("../utils/database");
|
||
|
|
|
||
|
|
exports.getAllCategory = async (req, res) => {
|
||
|
|
try {
|
||
|
|
const [data, fields] = await db.execute(`SELECT * FROM Category`);
|
||
|
|
|
||
|
|
// Format as { ID: "", ID: "" }
|
||
|
|
const formattedData = {};
|
||
|
|
data.forEach((row) => {
|
||
|
|
formattedData[row.CategoryID] = row.Name;
|
||
|
|
});
|
||
|
|
|
||
|
|
res.json({
|
||
|
|
success: true,
|
||
|
|
message: "Categories fetched successfully",
|
||
|
|
data: formattedData,
|
||
|
|
});
|
||
|
|
} catch (error) {
|
||
|
|
console.error("Error fetching categories:", error);
|
||
|
|
return res.status(500).json({
|
||
|
|
success: false,
|
||
|
|
error: "Database error occurred",
|
||
|
|
});
|
||
|
|
}
|
||
|
|
};
|