Advertisement
sourav8256

Untitled

Jul 31st, 2023
796
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // routes/products.js
  2. const express = require('express');
  3. const router = express.Router();
  4. const Product = require('../models/product');
  5.  
  6. // Define routes for products
  7. router.get('/', async (req, res) => {
  8.   try {
  9.     const products = await Product.find().populate('category', 'name');
  10.     res.json(products);
  11.   } catch (error) {
  12.     res.status(500).json({ message: 'Error fetching products', error: error.message });
  13.   }
  14. });
  15.  
  16. router.get('/:id', async (req, res) => {
  17.   try {
  18.     const product = await Product.findById(req.params.id).populate('category', 'name');
  19.     if (!product) {
  20.       return res.status(404).json({ message: 'Product not found' });
  21.     }
  22.     res.json(product);
  23.   } catch (error) {
  24.     res.status(500).json({ message: 'Error fetching product', error: error.message });
  25.   }
  26. });
  27.  
  28. router.post('/', async (req, res) => {
  29.   try {
  30.     const { name, price, category } = req.body;
  31.     const product = new Product({ name, price, category });
  32.     await product.save();
  33.     res.status(201).json(product);
  34.   } catch (error) {
  35.     res.status(500).json({ message: 'Error creating product', error: error.message });
  36.   }
  37. });
  38.  
  39. router.put('/:id', async (req, res) => {
  40.   try {
  41.     const { name, price, category } = req.body;
  42.     const product = await Product.findByIdAndUpdate(
  43.       req.params.id,
  44.       { name, price, category },
  45.       { new: true }
  46.     ).populate('category', 'name');
  47.     if (!product) {
  48.       return res.status(404).json({ message: 'Product not found' });
  49.     }
  50.     res.json(product);
  51.   } catch (error) {
  52.     res.status(500).json({ message: 'Error updating product', error: error.message });
  53.   }
  54. });
  55.  
  56. router.delete('/:id', async (req, res) => {
  57.   try {
  58.     const product = await Product.findByIdAndRemove(req.params.id);
  59.     if (!product) {
  60.       return res.status(404).json({ message: 'Product not found' });
  61.     }
  62.     res.json({ message: 'Product deleted successfully' });
  63.   } catch (error) {
  64.     res.status(500).json({ message: 'Error deleting product', error: error.message });
  65.   }
  66. });
  67.  
  68. module.exports = router;
  69.  
  70.  
  71. // routes/categories.js
  72. const express = require('express');
  73. const router = express.Router();
  74. const Category = require('../models/category');
  75.  
  76. // Define routes for categories
  77. router.get('/', async (req, res) => {
  78.   try {
  79.     const categories = await Category.find();
  80.     res.json(categories);
  81.   } catch (error) {
  82.     res.status(500).json({ message: 'Error fetching categories', error: error.message });
  83.   }
  84. });
  85.  
  86. router.get('/:id', async (req, res) => {
  87.   try {
  88.     const category = await Category.findById(req.params.id);
  89.     if (!category) {
  90.       return res.status(404).json({ message: 'Category not found' });
  91.     }
  92.     res.json(category);
  93.   } catch (error) {
  94.     res.status(500).json({ message: 'Error fetching category', error: error.message });
  95.   }
  96. });
  97.  
  98. router.post('/', async (req, res) => {
  99.   try {
  100.     const { name } = req.body;
  101.     const category = new Category({ name });
  102.     await category.save();
  103.     res.status(201).json(category);
  104.   } catch (error) {
  105.     res.status(500).json({ message: 'Error creating category', error: error.message });
  106.   }
  107. });
  108.  
  109. router.put('/:id', async (req, res) => {
  110.   try {
  111.     const { name } = req.body;
  112.     const category = await Category.findByIdAndUpdate(
  113.       req.params.id,
  114.       { name },
  115.       { new: true }
  116.     );
  117.     if (!category) {
  118.       return res.status(404).json({ message: 'Category not found' });
  119.     }
  120.     res.json(category);
  121.   } catch (error) {
  122.     res.status(500).json({ message: 'Error updating category', error: error.message });
  123.   }
  124. });
  125.  
  126. router.delete('/:id', async (req, res) => {
  127.   try {
  128.     const category = await Category.findByIdAndRemove(req.params.id);
  129.     if (!category) {
  130.       return res.status(404).json({ message: 'Category not found' });
  131.     }
  132.     res.json({ message: 'Category deleted successfully' });
  133.   } catch (error) {
  134.     res.status(500).json({ message: 'Error deleting category', error: error.message });
  135.   }
  136. });
  137.  
  138. module.exports = router;
  139.  
  140.  
  141.  
  142.  
  143. // routes/cart.js
  144. const express = require('express');
  145. const router = express.Router();
  146. const CartItem = require('../models/cartItem');
  147.  
  148. // Define routes for the cart
  149. router.get('/', async (req, res) => {
  150.   try {
  151.     // Logic to fetch the current user's shopping cart contents
  152.     // You may need to implement authentication and identify the current user
  153.     const cartItems = await CartItem.find().populate('product', 'name price');
  154.     res.json(cartItems);
  155.   } catch (error) {
  156.     res.status(500).json({ message: 'Error fetching cart items', error: error.message });
  157.   }
  158. });
  159.  
  160. router.post('/:productId', async (req, res) => {
  161.   try {
  162.     const productId = req.params.productId;
  163.     // Logic to add the product with productId to the shopping cart
  164.     // You may need to implement authentication and identify the current user
  165.     const cartItem = new CartItem({ product: productId, quantity: 1 });
  166.     await cartItem.save();
  167.     res.status(201).json(cartItem);
  168.   } catch (error) {
  169.     res.status(500).json({ message: 'Error adding product to the cart', error: error.message });
  170.   }
  171. });
  172.  
  173. router.put('/:productId', async (req, res) => {
  174.   try {
  175.     const productId = req.params.productId;
  176.     const quantity = req.body.quantity;
  177.     // Logic to update the quantity of a product in the shopping cart
  178.     // You may need to implement authentication and identify the current user
  179.     const cartItem = await CartItem.findOneAndUpdate(
  180.       { product: productId },
  181.       { quantity },
  182.       { new: true }
  183.     ).populate('product', 'name price');
  184.     if (!cartItem) {
  185.       return res.status(404).json({ message: 'Product not found in the cart' });
  186.     }
  187.     res.json(cartItem);
  188.   } catch (error) {
  189.     res.status(500).json({ message: 'Error updating cart item', error: error.message });
  190.   }
  191. });
  192.  
  193. router.delete('/:productId', async (req, res) => {
  194.   try {
  195.     const productId = req.params.productId;
  196.     // Logic to remove a product from the shopping cart
  197.     // You may need to implement authentication and identify the current user
  198.     const cartItem = await CartItem.findOneAndRemove({ product: productId }).populate(
  199.       'product',
  200.       'name'
  201.     );
  202.     if (!cartItem) {
  203.       return res.status(404).json({ message: 'Product not found in the cart' });
  204.     }
  205.     res.json({ message: 'Product removed from the cart successfully' });
  206.   } catch (error) {
  207.     res.status(500).json({ message: 'Error removing product from the cart', error: error.message });
  208.   }
  209. });
  210.  
  211. module.exports = router;
  212.  
  213.  
  214.  
  215. // app.js
  216. const express = require('express');
  217. const bodyParser = require('body-parser');
  218. const mongoose = require('mongoose');
  219.  
  220. const app = express();
  221. const PORT = process.env.PORT || 3000;
  222.  
  223. app.use(bodyParser.json());
  224. app.use(bodyParser.urlencoded({ extended: true }));
  225.  
  226. // Connect to MongoDB using Mongoose
  227. const MONGODB_URI = 'mongodb://localhost:27017/ecommerce_db'; // Replace with your MongoDB URI
  228. mongoose.connect(MONGODB_URI, {
  229.   useNewUrlParser: true,
  230.   useUnifiedTopology: true,
  231. });
  232.  
  233. // Include the products, categories, and cart routes
  234. const productsRoutes = require('./routes/products');
  235. const categoriesRoutes = require('./routes/categories');
  236. const cartRoutes = require('./routes/cart');
  237.  
  238. app.use('/products', productsRoutes);
  239. app.use('/categories', categoriesRoutes);
  240. app.use('/cart', cartRoutes);
  241.  
  242. // Start the server
  243. app.listen(PORT, () => {
  244.   console.log(`Server is running on port ${PORT}`);
  245. });
  246.  
  247.  
  248.  
  249. Models
  250.  
  251.  
  252.  
  253. // models/product.js
  254. const mongoose = require('mongoose');
  255.  
  256. const productSchema = new mongoose.Schema({
  257.   name: { type: String, required: true },
  258.   price: { type: Number, required: true },
  259.   category: { type: mongoose.Schema.Types.ObjectId, ref: 'Category' },
  260. });
  261.  
  262. const Product = mongoose.model('Product', productSchema);
  263.  
  264. module.exports = Product;
  265.  
  266.  
  267.  
  268. // models/category.js
  269. const mongoose = require('mongoose');
  270.  
  271. const categorySchema = new mongoose.Schema({
  272.   name: { type: String, required: true },
  273. });
  274.  
  275. const Category = mongoose.model('Category', categorySchema);
  276.  
  277. module.exports = Category;
  278.  
  279.  
  280.  
  281. // models/cartItem.js
  282. const mongoose = require('mongoose');
  283.  
  284. const cartItemSchema = new mongoose.Schema({
  285.   product: { type: mongoose.Schema.Types.ObjectId, ref: 'Product', required: true },
  286.   quantity: { type: Number, default: 1 },
  287.   // Add other cart item-related fields as needed
  288. });
  289.  
  290. const CartItem = mongoose.model('CartItem', cartItemSchema);
  291.  
  292. module.exports = CartItem;
  293.  
  294.  
  295.  
  296.  
  297. - node_modules/
  298. - routes/
  299.   - products.js
  300.   - categories.js
  301.   - cart.js
  302. - models/
  303.   - product.js
  304.   - category.js
  305.   - cartItem.js
  306. - app.js
  307. - package.json
  308. - package-lock.json
  309.  
  310.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement