File size: 3,403 Bytes
b89a86e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
 * Script to add placeholder images to existing products and categories
 * Uses Lorem Picsum for free placeholder images
 */

const API_BASE = 'http://localhost:7860';

// Generate random Lorem Picsum image URL
function getPlaceholderImageUrl(width = 400, height = 300, category = 'all') {
  const randomId = Math.floor(Math.random() * 1000) + 1;
  return `https://picsum.photos/${width}/${height}?random=${randomId}`;
}

// Get category-specific image URL
function getCategoryImageUrl(categoryName) {
  const randomId = Math.floor(Math.random() * 1000) + 1;
  return `https://picsum.photos/200/200?random=${randomId}`;
}

// Get product images array (3 images per product)
function getProductImages() {
  const images = [];
  for (let i = 0; i < 3; i++) {
    const randomId = Math.floor(Math.random() * 1000) + 1;
    images.push(`https://picsum.photos/400/400?random=${randomId}`);
  }
  return images;
}

async function updateCategories() {
  try {
    console.log('Fetching categories...');
    const response = await fetch(`${API_BASE}/api/categories`);
    const categories = await response.json();
    
    console.log(`Found ${categories.length} categories`);
    
    for (const category of categories) {
      if (!category.imageUrl) {
        const imageUrl = getCategoryImageUrl(category.name);
        
        console.log(`Updating category "${category.name}" with image: ${imageUrl}`);
        
        const updateResponse = await fetch(`${API_BASE}/api/categories/${category.id}`, {
          method: 'PUT',
          headers: {
            'Content-Type': 'application/json',
          },
          body: JSON.stringify({
            imageUrl: imageUrl
          })
        });
        
        if (updateResponse.ok) {
          console.log(`โœ“ Updated category: ${category.name}`);
        } else {
          console.error(`โœ— Failed to update category: ${category.name}`);
        }
      }
    }
  } catch (error) {
    console.error('Error updating categories:', error);
  }
}

async function updateProducts() {
  try {
    console.log('Fetching products...');
    const response = await fetch(`${API_BASE}/api/products`);
    const products = await response.json();
    
    console.log(`Found ${products.length} products`);
    
    for (const product of products) {
      if (!product.images || product.images.length === 0) {
        const images = getProductImages();
        
        console.log(`Updating product "${product.title}" with ${images.length} images`);
        
        // Since the API expects file uploads, we'll need to use the storage interface directly
        // For now, we'll log what we would update
        console.log(`Would update product "${product.title}" with images:`, images);
      }
    }
  } catch (error) {
    console.error('Error updating products:', error);
  }
}

async function main() {
  console.log('๐Ÿ–ผ๏ธ  Adding placeholder images to products and categories...\n');
  
  await updateCategories();
  console.log('\n');
  await updateProducts();
  
  console.log('\nโœ… Finished adding placeholder images!');
}

// Only run if called directly
if (typeof window === 'undefined' && typeof require !== 'undefined') {
  main().catch(console.error);
}

// Export for use in browser/client
if (typeof module !== 'undefined') {
  module.exports = { 
    getPlaceholderImageUrl, 
    getCategoryImageUrl, 
    getProductImages 
  };
}