product api

PHOTO EMBED

Thu Mar 28 2024 08:50:59 GMT+0000 (Coordinated Universal Time)

Saved by @Jevin2090

get 
app.get('/products', (req, res) => {
  const products = [
    {
      id: 1,
      name: 'Furniture 1',
      price: 1000,
      size: 'L',
      fabricTexture: 'Smooth',
      woodTexture: 'Polished',
      woodType: 'Oak',
      fabricType: 'Cotton',
    },
    // ... more products
  ];

  res.send(products);
});

post
app.post('/products', (req, res) => {
  const newProduct = {
    id: Date.now(),
    name: req.body.name,
    price: req.body.price,
    size: req.body.size,
    fabricTexture: req.body.fabricTexture,
    woodTexture: req.body.woodTexture,
    woodType: req.body.woodType,
    fabricType: req.body.fabricType,
  };

  const products = [
    {
      id: 1,
      name: 'Furniture 1',
      price: 1000,
      size: 'L',
      fabricTexture: 'Smooth',
      woodTexture: 'Polished',
      woodType: 'Oak',
      fabricType: 'Cotton',
    },
    // ... more products
  ];

  products.push(newProduct);

  res.send(newProduct);
});

get by id 
app.get('/products/:id', (req, res) => {
  const products = [
    {
      id: 1,
      name: 'Furniture 1',
      price: 1000,
      size: 'L',
      fabricTexture: 'Smooth',
      woodTexture: 'Polished',
      woodType: 'Oak',
      fabricType: 'Cotton',
    },
    // ... more products
  ];

  const product = products.find((p) => p.id === parseInt(req.params.id));

  if (!product) {
    return res.status(404).send({ error: 'Product not found' });
  }

  res.send(product);
});

put
app.put('/products/:id', (req, res) => {
  const products = [
    {
      id: 1,
      name: 'Furniture 1',
      price: 1000,
      size: 'L',
      fabricTexture: 'Smooth',
      woodTexture: 'Polished',
      woodType: 'Oak',
      fabricType: 'Cotton',
      updated: false,
    },
    // ... more products
  ];

  const product = products.find((p) => p.id === parseInt(req.params.id));

  if (!product) {
    return res.status(404).send({ error: 'Product not found' });
  }

  product.name = req.body.name || product.name;
  product.price = req.body.price || product.price;
  product.size = req.body.size || product.size;
  product.fabricTexture = req.body.fabricTexture || product.fabricTexture;
  product.woodTexture = req.body.woodTexture || product.woodTexture;
  product.woodType = req.body.woodType || product.woodType;
  product.fabricType = req.body.fabricType || product.fabricType;

  res.send(product);
});
content_copyCOPY