updateweb
This commit is contained in:
@@ -1,6 +1,13 @@
|
||||
const express = require("express");
|
||||
const { query } = require("../config/database");
|
||||
const { requireAuth } = require("../middleware/auth");
|
||||
const { cache } = require("../middleware/cache");
|
||||
const {
|
||||
invalidateProductCache,
|
||||
invalidateBlogCache,
|
||||
invalidatePortfolioCache,
|
||||
invalidateHomepageCache,
|
||||
} = require("../utils/cacheInvalidation");
|
||||
const logger = require("../config/logger");
|
||||
const { asyncHandler } = require("../middleware/errorHandler");
|
||||
const {
|
||||
@@ -252,6 +259,10 @@ router.put(
|
||||
"/products/:id",
|
||||
requireAuth,
|
||||
asyncHandler(async (req, res) => {
|
||||
console.log("=== UPDATE PRODUCT API CALLED ===");
|
||||
console.log("Product ID:", req.params.id);
|
||||
console.log("Request body:", JSON.stringify(req.body, null, 2));
|
||||
|
||||
const {
|
||||
name,
|
||||
shortdescription,
|
||||
@@ -269,6 +280,8 @@ router.put(
|
||||
images,
|
||||
} = req.body;
|
||||
|
||||
console.log("Images to save:", images);
|
||||
|
||||
// Generate slug if name is provided
|
||||
const slug = name ? generateSlug(name) : null;
|
||||
|
||||
@@ -344,16 +357,27 @@ router.put(
|
||||
return sendNotFound(res, "Product");
|
||||
}
|
||||
|
||||
console.log("Product updated in database:", result.rows[0].id);
|
||||
|
||||
// Update images if provided
|
||||
if (images && Array.isArray(images)) {
|
||||
console.log("Updating images, count:", images.length);
|
||||
|
||||
// Delete existing images for this product
|
||||
await query("DELETE FROM product_images WHERE product_id = $1", [
|
||||
req.params.id,
|
||||
]);
|
||||
const deleteResult = await query(
|
||||
"DELETE FROM product_images WHERE product_id = $1",
|
||||
[req.params.id]
|
||||
);
|
||||
console.log("Deleted existing images, count:", deleteResult.rowCount);
|
||||
|
||||
// Insert new images
|
||||
for (let i = 0; i < images.length; i++) {
|
||||
const img = images[i];
|
||||
console.log(
|
||||
`Inserting image ${i + 1}/${images.length}:`,
|
||||
img.image_url
|
||||
);
|
||||
|
||||
await query(
|
||||
`INSERT INTO product_images (
|
||||
product_id, image_url, color_variant, color_code, alt_text, display_order, is_primary, variant_price, variant_stock
|
||||
@@ -371,6 +395,9 @@ router.put(
|
||||
]
|
||||
);
|
||||
}
|
||||
console.log("All images inserted successfully");
|
||||
} else {
|
||||
console.log("No images to update");
|
||||
}
|
||||
|
||||
// Fetch complete product with images
|
||||
@@ -396,6 +423,12 @@ router.put(
|
||||
[req.params.id]
|
||||
);
|
||||
|
||||
console.log("Final product with images:", completeProduct.rows[0]);
|
||||
console.log("=== PRODUCT UPDATE COMPLETE ===");
|
||||
|
||||
// Invalidate product cache
|
||||
invalidateProductCache();
|
||||
|
||||
sendSuccess(res, {
|
||||
product: completeProduct.rows[0],
|
||||
message: "Product updated successfully",
|
||||
@@ -655,10 +688,15 @@ router.post(
|
||||
ispublished,
|
||||
pagedata,
|
||||
} = req.body;
|
||||
|
||||
// Generate readable ID from slug
|
||||
const pageId = `page-${slug}`;
|
||||
|
||||
const result = await query(
|
||||
`INSERT INTO pages (title, slug, content, pagecontent, metatitle, metadescription, ispublished, isactive, pagedata, createdat)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, NOW()) RETURNING *`,
|
||||
`INSERT INTO pages (id, title, slug, content, pagecontent, metatitle, metadescription, ispublished, isactive, pagedata, createdat)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, NOW()) RETURNING *`,
|
||||
[
|
||||
pageId,
|
||||
title,
|
||||
slug,
|
||||
content,
|
||||
|
||||
Reference in New Issue
Block a user