diff --git a/routes/products.js b/routes/products.js index 526d305..c506034 100644 --- a/routes/products.js +++ b/routes/products.js @@ -1,15 +1,36 @@ +// const express = require('express'); +// const router = express.Router(); +// const Product = require('../models/Product'); +// const auth = require('../middleware/auth'); + +// // GET /api/products - Returns all products (JWT protected) +// router.get('/', auth, async (req, res) => { +// try { +// const products = await Product.find(); +// res.json(products); +// } catch (err) { +// res.status(500).json({ error: 'Failed to fetch products' }); +// } +// }); + +// module.exports = router; + + + const express = require('express'); const router = express.Router(); const Product = require('../models/Product'); -const auth = require('../middleware/auth'); +const authenticateToken = require('../middleware/auth'); // adjust path if needed -// GET /api/products - Returns all products (JWT protected) -router.get('/', auth, async (req, res) => { +// GET /api/products?category=Electronics +router.get('/', authenticateToken, async (req, res) => { try { - const products = await Product.find(); + const category = req.query.category; + const filter = category ? { category } : {}; + const products = await Product.find(filter); res.json(products); } catch (err) { - res.status(500).json({ error: 'Failed to fetch products' }); + res.status(500).json({ error: 'Server error' }); } }); diff --git a/seed/products.js b/seed/products.js index 0bf4ae4..0c3021b 100644 --- a/seed/products.js +++ b/seed/products.js @@ -1,19 +1,41 @@ -require('dotenv').config(); -const mongoose = require('mongoose'); -const Product = require('../models/Product'); +require("dotenv").config(); +const mongoose = require("mongoose"); +const Product = require("../models/Product"); const seedProducts = [ - { name: 'iPhone 14', category: 'Electronics', price: 999 }, - { name: 'Nike Air Max', category: 'Footwear', price: 120 }, - { name: 'Sony Headphones', category: 'Electronics', price: 199 } + // { name: 'iPhone 14', category: 'Electronics', price: 999 }, + // { name: 'Nike Air Max', category: 'Footwear', price: 120 }, + // { name: 'Sony Headphones', category: 'Electronics', price: 199 } + + { name: "iPhone 14", category: "Electronics", price: 999 }, + { name: "Nike Air Max", category: "Footwear", price: 120 }, + { name: "Sony Headphones", category: "Electronics", price: 199 }, + + { name: "Samsung Galaxy S23", category: "Electronics", price: 899 }, + { name: "Dell XPS 13", category: "Computers", price: 1299 }, + { name: "Apple Watch Series 8", category: "Wearables", price: 399 }, + { name: "Logitech MX Master 3", category: "Accessories", price: 99 }, + { name: "Kindle Paperwhite", category: "Electronics", price: 139 }, + { name: "Canon EOS M50", category: "Electronics", price: 649 }, + { name: "Bose QuietComfort 45", category: "Electronics", price: 329 }, + { name: "Asus ROG Strix G16", category: "Computers", price: 1599 }, + { name: "Google Pixel 8", category: "Electronics", price: 799 }, + { name: "Anker PowerCore 20000", category: "Accessories", price: 59 }, + { name: "Samsung Galaxy Watch 6", category: "Wearables", price: 349 }, + { name: "Razer BlackWidow V3", category: "Accessories", price: 139 }, + { name: "HP Spectre x360", category: "Computers", price: 1399 }, + { name: "Meta Quest 3", category: "Wearables", price: 499 }, + { name: "Beats Studio Pro", category: "Electronics", price: 349 } + ]; -mongoose.connect(process.env.MONGODB_URI) +mongoose + .connect(process.env.MONGODB_URI) .then(async () => { - console.log('MongoDB connected'); + console.log("MongoDB connected"); await Product.deleteMany(); // Optional: clears old data await Product.insertMany(seedProducts); - console.log('Sample products inserted'); + console.log("Sample products inserted"); process.exit(); }) - .catch(err => console.log(err)); + .catch((err) => console.log(err));