Products

getProducts

Below is the code for getProductsAPI. In this example we are calling a server script xe.Product which has a method called find which will return the list of all products.

module.exports.getProducts = () => {
  return async (req, res) => {
    const domain = req.subdomains[0] || "dev";
    let _query = { domain };
    const query = req.query;
    if (query && Object.keys(query).length) {
      delete query.nd;
      // _query = Object.assign(_query, ob.querystring.parse(query));
      _query = { ..._query, ...query};
    }
    res.set('Cache-Control', 'no-cache');
    const user = req.user;
    ob.log(_query);
    const products = await xe.Product.find(_query);
    res.json(products);
  };
};

postProduct

Below is the code for postProduct API. In this example allows user to make a product. In this code we retrieve the form values storing product details and pass it to a server script called xe.Product which has a method called save which will save the product details to the database.


module.exports.postProduct = () => {
  return async (req, res) => {
    const domain = req.subdomains[0] || "dev";
    const { title, description, stock, category } = req.body;
    const article = new xe.Product({
      sku: "1234",
      title,
      name: title,
      description,
      domain,
      type: "String",
      category,
      // total_quantity: 100,
      last_stock_price: 10,
      unit: "String",
      created_on: new Date(),
      updated_on: new Date(),
      created_by: "String",
      updated_by: "String",
      stock
    });

    // Insert the article in our MongoDB database
    await article.save();
    res.json({ success: true });
  };
};

Last updated