updateweb
This commit is contained in:
@@ -19,9 +19,23 @@ router.get(
|
||||
"/products",
|
||||
asyncHandler(async (req, res) => {
|
||||
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`
|
||||
`SELECT p.id, p.name, p.slug, p.shortdescription, p.description, p.price,
|
||||
p.category, p.stockquantity, p.sku, p.weight, p.dimensions,
|
||||
p.material, p.isfeatured, p.isbestseller, p.createdat,
|
||||
json_agg(
|
||||
json_build_object(
|
||||
'id', pi.id,
|
||||
'image_url', pi.image_url,
|
||||
'color_variant', pi.color_variant,
|
||||
'alt_text', pi.alt_text,
|
||||
'is_primary', pi.is_primary
|
||||
) ORDER BY pi.display_order, pi.created_at
|
||||
) FILTER (WHERE pi.id IS NOT NULL) as images
|
||||
FROM products p
|
||||
LEFT JOIN product_images pi ON pi.product_id = p.id
|
||||
WHERE p.isactive = true
|
||||
GROUP BY p.id
|
||||
ORDER BY p.createdat DESC`
|
||||
);
|
||||
sendSuccess(res, { products: result.rows });
|
||||
})
|
||||
@@ -33,22 +47,80 @@ router.get(
|
||||
asyncHandler(async (req, res) => {
|
||||
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`,
|
||||
`SELECT p.id, p.name, p.slug, p.shortdescription, p.price, p.category,
|
||||
json_agg(
|
||||
json_build_object(
|
||||
'image_url', pi.image_url,
|
||||
'color_variant', pi.color_variant,
|
||||
'alt_text', pi.alt_text
|
||||
) ORDER BY pi.display_order, pi.created_at
|
||||
) FILTER (WHERE pi.id IS NOT NULL) as images
|
||||
FROM products p
|
||||
LEFT JOIN product_images pi ON pi.product_id = p.id
|
||||
WHERE p.isactive = true AND p.isfeatured = true
|
||||
GROUP BY p.id
|
||||
ORDER BY p.createdat DESC
|
||||
LIMIT $1`,
|
||||
[limit]
|
||||
);
|
||||
sendSuccess(res, { products: result.rows });
|
||||
})
|
||||
);
|
||||
|
||||
// Get single product
|
||||
// Get single product by ID or slug
|
||||
router.get(
|
||||
"/products/:id",
|
||||
"/products/:identifier",
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await query(
|
||||
"SELECT * FROM products WHERE id = $1 AND isactive = true",
|
||||
[req.params.id]
|
||||
);
|
||||
const { identifier } = req.params;
|
||||
|
||||
// Check if identifier is a UUID
|
||||
const isUUID =
|
||||
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(
|
||||
identifier
|
||||
);
|
||||
|
||||
// Try to find by ID first, then by slug if not UUID
|
||||
let result;
|
||||
if (isUUID) {
|
||||
result = await query(
|
||||
`SELECT p.*,
|
||||
json_agg(
|
||||
json_build_object(
|
||||
'id', pi.id,
|
||||
'image_url', pi.image_url,
|
||||
'color_variant', pi.color_variant,
|
||||
'alt_text', pi.alt_text,
|
||||
'display_order', pi.display_order,
|
||||
'is_primary', pi.is_primary
|
||||
) ORDER BY pi.display_order, pi.created_at
|
||||
) FILTER (WHERE pi.id IS NOT NULL) as images
|
||||
FROM products p
|
||||
LEFT JOIN product_images pi ON pi.product_id = p.id
|
||||
WHERE p.id = $1 AND p.isactive = true
|
||||
GROUP BY p.id`,
|
||||
[identifier]
|
||||
);
|
||||
} else {
|
||||
// Try both ID and slug for non-UUID identifiers
|
||||
result = await query(
|
||||
`SELECT p.*,
|
||||
json_agg(
|
||||
json_build_object(
|
||||
'id', pi.id,
|
||||
'image_url', pi.image_url,
|
||||
'color_variant', pi.color_variant,
|
||||
'alt_text', pi.alt_text,
|
||||
'display_order', pi.display_order,
|
||||
'is_primary', pi.is_primary
|
||||
) ORDER BY pi.display_order, pi.created_at
|
||||
) FILTER (WHERE pi.id IS NOT NULL) as images
|
||||
FROM products p
|
||||
LEFT JOIN product_images pi ON pi.product_id = p.id
|
||||
WHERE (p.id = $1 OR p.slug = $1) AND p.isactive = true
|
||||
GROUP BY p.id`,
|
||||
[identifier]
|
||||
);
|
||||
}
|
||||
|
||||
if (result.rows.length === 0) {
|
||||
return sendNotFound(res, "Product");
|
||||
@@ -126,7 +198,7 @@ router.get(
|
||||
"/pages",
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await query(
|
||||
`SELECT id, title, slug, content, metatitle, metadescription, isactive, createdat
|
||||
`SELECT id, title, slug, pagecontent as content, metatitle, metadescription, isactive, createdat
|
||||
FROM pages WHERE isactive = true ORDER BY createdat DESC`
|
||||
);
|
||||
sendSuccess(res, { pages: result.rows });
|
||||
@@ -138,7 +210,7 @@ router.get(
|
||||
"/pages/:slug",
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await query(
|
||||
"SELECT * FROM pages WHERE slug = $1 AND isactive = true",
|
||||
"SELECT id, title, slug, pagecontent as content, metatitle, metadescription FROM pages WHERE slug = $1 AND isactive = true",
|
||||
[req.params.slug]
|
||||
);
|
||||
|
||||
@@ -176,4 +248,45 @@ router.get(
|
||||
})
|
||||
);
|
||||
|
||||
// Get all team members (public)
|
||||
router.get(
|
||||
"/team-members",
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await query(
|
||||
"SELECT id, name, position, bio, image_url FROM team_members ORDER BY display_order ASC, created_at DESC"
|
||||
);
|
||||
sendSuccess(res, { teamMembers: result.rows });
|
||||
})
|
||||
);
|
||||
|
||||
// Get menu items (public)
|
||||
router.get(
|
||||
"/menu",
|
||||
asyncHandler(async (req, res) => {
|
||||
const result = await query(
|
||||
"SELECT settings FROM site_settings WHERE key = 'menu'"
|
||||
);
|
||||
|
||||
if (result.rows.length === 0) {
|
||||
return sendSuccess(res, { items: [] });
|
||||
}
|
||||
|
||||
// Parse JSON settings if it's a string
|
||||
let settings = result.rows[0].settings;
|
||||
if (typeof settings === "string") {
|
||||
try {
|
||||
settings = JSON.parse(settings);
|
||||
} catch (e) {
|
||||
logger.error("Failed to parse menu settings:", e);
|
||||
return sendSuccess(res, { items: [] });
|
||||
}
|
||||
}
|
||||
|
||||
const items = settings.items || [];
|
||||
// Filter only visible items for public
|
||||
const visibleItems = items.filter((item) => item.visible !== false);
|
||||
sendSuccess(res, { items: visibleItems });
|
||||
})
|
||||
);
|
||||
|
||||
module.exports = router;
|
||||
|
||||
Reference in New Issue
Block a user