2025-12-13 22:34:11 -06:00
|
|
|
const express = require("express");
|
|
|
|
|
const { query } = require("../config/database");
|
2025-12-13 17:53:34 -06:00
|
|
|
const router = express.Router();
|
|
|
|
|
|
2025-12-13 22:34:11 -06:00
|
|
|
// Get all products
|
|
|
|
|
router.get("/products", async (req, res) => {
|
2025-12-13 17:53:34 -06:00
|
|
|
try {
|
2025-12-13 22:34:11 -06:00
|
|
|
const result = await query(
|
|
|
|
|
"SELECT id, name, description, shortdescription, price, imageurl, images, category, color, stockquantity, isactive, createdat FROM products WHERE isactive = true ORDER BY createdat DESC"
|
2025-12-13 17:53:34 -06:00
|
|
|
);
|
2025-12-13 22:34:11 -06:00
|
|
|
res.json({
|
|
|
|
|
success: true,
|
|
|
|
|
products: result.rows,
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Products API error:", error);
|
|
|
|
|
res.status(500).json({ success: false, message: "Server error" });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Get featured products
|
|
|
|
|
router.get("/products/featured", async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const limit = parseInt(req.query.limit) || 4;
|
|
|
|
|
const result = await query(
|
|
|
|
|
"SELECT id, name, description, price, imageurl, images FROM products WHERE isactive = true ORDER BY createdat DESC LIMIT $1",
|
|
|
|
|
[limit]
|
|
|
|
|
);
|
|
|
|
|
res.json({
|
|
|
|
|
success: true,
|
|
|
|
|
products: result.rows,
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Featured products error:", error);
|
|
|
|
|
res.status(500).json({ success: false, message: "Server error" });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Get single product
|
|
|
|
|
router.get("/products/:id", async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const result = await query(
|
|
|
|
|
"SELECT * FROM products WHERE id = $1 AND isactive = true",
|
|
|
|
|
[req.params.id]
|
|
|
|
|
);
|
|
|
|
|
if (result.rows.length === 0) {
|
|
|
|
|
return res
|
|
|
|
|
.status(404)
|
|
|
|
|
.json({ success: false, message: "Product not found" });
|
|
|
|
|
}
|
|
|
|
|
res.json({
|
|
|
|
|
success: true,
|
|
|
|
|
product: result.rows[0],
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Product detail error:", error);
|
|
|
|
|
res.status(500).json({ success: false, message: "Server error" });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Get site settings
|
|
|
|
|
router.get("/settings", async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const result = await query("SELECT * FROM sitesettings LIMIT 1");
|
|
|
|
|
res.json({
|
|
|
|
|
success: true,
|
|
|
|
|
settings: result.rows[0] || {},
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Settings error:", error);
|
|
|
|
|
res.json({ success: true, settings: {} });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Get homepage sections
|
|
|
|
|
router.get("/homepage/sections", async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const result = await query(
|
|
|
|
|
"SELECT * FROM homepagesections ORDER BY displayorder ASC"
|
|
|
|
|
);
|
|
|
|
|
res.json({
|
|
|
|
|
success: true,
|
|
|
|
|
sections: result.rows,
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Homepage sections error:", error);
|
|
|
|
|
res.status(500).json({ success: false, message: "Server error" });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Get portfolio projects
|
|
|
|
|
router.get("/portfolio/projects", async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const result = await query(
|
|
|
|
|
"SELECT id, title, description, imageurl, categoryid, createdat FROM portfolioprojects ORDER BY createdat DESC"
|
2025-12-13 17:53:34 -06:00
|
|
|
);
|
2025-12-13 22:34:11 -06:00
|
|
|
res.json({
|
|
|
|
|
success: true,
|
|
|
|
|
projects: result.rows,
|
2025-12-13 17:53:34 -06:00
|
|
|
});
|
|
|
|
|
} catch (error) {
|
2025-12-13 22:34:11 -06:00
|
|
|
console.error("Portfolio error:", error);
|
|
|
|
|
res.status(500).json({ success: false, message: "Server error" });
|
2025-12-13 17:53:34 -06:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2025-12-13 22:34:11 -06:00
|
|
|
// Get blog posts
|
|
|
|
|
router.get("/blog/posts", async (req, res) => {
|
2025-12-13 17:53:34 -06:00
|
|
|
try {
|
2025-12-13 22:34:11 -06:00
|
|
|
const result = await query(
|
|
|
|
|
"SELECT id, title, slug, excerpt, content, imageurl, ispublished, createdat FROM blogposts WHERE ispublished = true ORDER BY createdat DESC"
|
2025-12-13 17:53:34 -06:00
|
|
|
);
|
2025-12-13 22:34:11 -06:00
|
|
|
res.json({
|
|
|
|
|
success: true,
|
|
|
|
|
posts: result.rows,
|
2025-12-13 17:53:34 -06:00
|
|
|
});
|
|
|
|
|
} catch (error) {
|
2025-12-13 22:34:11 -06:00
|
|
|
console.error("Blog posts error:", error);
|
|
|
|
|
res.status(500).json({ success: false, message: "Server error" });
|
2025-12-13 17:53:34 -06:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
module.exports = router;
|