[object Object]

← back to Saloonwallpaper

wire catalog from dw_unified — admin CRUD module + PG read-path; fix /api/sliders to use site rails

7984bcfb08948517f2bc0deaeacc8d7ddb46931d · 2026-05-19 09:09:54 -0700 · Steve Abrams

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

Files touched

Diff

commit 7984bcfb08948517f2bc0deaeacc8d7ddb46931d
Author: Steve Abrams <steve@designerwallcoverings.com>
Date:   Tue May 19 09:09:54 2026 -0700

    wire catalog from dw_unified — admin CRUD module + PG read-path; fix /api/sliders to use site rails
    
    Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
---
 server.js | 48 ++++++++++++++++++++++++++++++------------------
 1 file changed, 30 insertions(+), 18 deletions(-)

diff --git a/server.js b/server.js
index b280da8..2e2cfa6 100644
--- a/server.js
+++ b/server.js
@@ -10,16 +10,10 @@ const fs = require('fs');
 const PORT = process.env.PORT || 9889;
 const DW_SHOPIFY = 'https://designerwallcoverings.com';
 const __SITE = path.basename(__dirname);
-let DATA_RAW;
-try {
-  const raw = fs.readFileSync(path.join(__dirname, 'data', 'products.json'), 'utf8');
-  DATA_RAW = JSON.parse(raw);
-  if (!Array.isArray(DATA_RAW)) throw new Error('products.json must be an array');
-} catch (e) {
-  console.error(`[${__SITE}] FATAL: could not load products.json — ${e.message}`);
-  console.error(`[${__SITE}] Starting with empty catalog. Run pull script to populate data/products.json.`);
-  DATA_RAW = [];
-}
+const catalog = require('../_shared/admin-catalog');
+const siteCfg = JSON.parse(fs.readFileSync(path.join(__dirname, 'site.config.json'), 'utf8'));
+const SITE_SLUG = siteCfg.slug || __SITE;
+const SITE_RAILS = Array.isArray(siteCfg.rails) ? siteCfg.rails : [];
 
 function isJunk(p) {
   if (!p.image_url || !p.image_url.trim()) return true;
@@ -32,9 +26,10 @@ function isJunk(p) {
   return false;
 }
 
-const PRODUCTS = DATA_RAW.filter(p => !isJunk(p));
-const DROPPED = DATA_RAW.length - PRODUCTS.length;
-console.log(`Loaded ${DATA_RAW.length}, kept ${PRODUCTS.length}, dropped ${DROPPED}`);
+// Catalog now lives in dw_unified.microsite_products — populated async at
+// startup (see bottom of file). Falls back to data/products.json if PG is down.
+let PRODUCTS = [];
+let DROPPED = 0;
 
 const app = express();
 // DW STANDARD: sort by color/style/sku/title for any product-rendering site.
@@ -157,9 +152,8 @@ app.get('/api/products', (req, res) => {
 });
 
 app.get('/api/sliders', (req, res) => {
-  const SLIDER_AESTHETICS = ["moody","leather","deco","jungle","chinoiserie","jacquard"];
   const out = [];
-  for (const a of SLIDER_AESTHETICS) {
+  for (const a of SITE_RAILS) {
     const items = PRODUCTS.filter(p => p.aesthetic === a).slice(0, 12);
     if (items.length >= 4) out.push({ aesthetic: a, items });
   }
@@ -199,6 +193,24 @@ ${urls.map(u => `  <url><loc>https://saloonwallpaper.com${u}</loc><changefreq>we
   res.type('application/xml').send(xml);
 });
 
-app.listen(PORT, '127.0.0.1', () => {
-  console.log(`saloonwallpaper listening on http://127.0.0.1:${PORT}`);
-});
+// Admin catalog CRUD — /admin/catalog (basic-auth) + /api/admin/* REST.
+catalog.mount(app, { siteSlug: SITE_SLUG, rails: SITE_RAILS });
+
+// Load the catalog from dw_unified, then start listening. PG down → JSON fallback.
+(async () => {
+  try {
+    const rows = await catalog.getProducts(SITE_SLUG);
+    PRODUCTS = rows.filter(p => !isJunk(p));
+    DROPPED = rows.length - PRODUCTS.length;
+    console.log(`[${__SITE}] loaded ${rows.length} from dw_unified, kept ${PRODUCTS.length}, dropped ${DROPPED}`);
+  } catch (e) {
+    console.error(`[${__SITE}] PG catalog load failed (${e.message}) — falling back to data/products.json`);
+    try {
+      const raw = JSON.parse(fs.readFileSync(path.join(__dirname, 'data', 'products.json'), 'utf8'));
+      PRODUCTS = (Array.isArray(raw) ? raw : []).filter(p => !isJunk(p));
+    } catch (_) { PRODUCTS = []; }
+  }
+  app.listen(PORT, '127.0.0.1', () => {
+    console.log(`saloonwallpaper listening on http://127.0.0.1:${PORT}`);
+  });
+})();

← f6da570 Add noopener,noreferrer to window.open external nav link  ·  back to Saloonwallpaper  ·  add grid/list view toggle + sortable list columns (Pattern/S 6cdb67b →