Orders

A Route is basically a backend REST API, where you write code for some DB Operation or anything else. You make request to this route to get some data or execute any other logic on the server.

In order to create a route, fill the following details in the form : -

  1. Name - name of the route.

  2. Path – endpoint URL of the route.

  3. Active – Check / Uncheck if ready / not ready to used.

  4. Route Type: - MS – route type is microservice. SITE – for serving static content. API – for serving data from database. CORE – core route is for system specific operations.

  5. Method: - POST – to post some data. GET – to get some data.

  6. Script – Code goes here.

  7. Click on ‘create’ button. That’s it, you have created a Route.

/getOrders

Below is the code for getOrders API. In this example we are calling a server script xe.Order which has a method called find which will return the list of all orders.

module.exports.getOrders = () => {
  return async (req, res) => {
    const domain = req.subdomains[0] || "dev";
    let _query = { domain };
    res.set("Cache-Control", "no-cache");
    const user = req.user;
    ob.log(_query);
    const orders = await xe.Order.find(_query);
    res.json(orders);
  };
};

/postOrder

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

module.exports.postOrder = () => {
  return async (req, res) => {
    const domain = req.subdomains[0] || "dev";
    const user = req.user;
    const {
      totalAmount,
      customerMobile,
      customerName,
      orderLines,
      studentClass,
      transactionDate
    } = req.body;
    const order = new xe.Order({
      domain,
      status: [{
        state: "Submitted",
        update_on: new Date()
      }],
      totalAmount,
      customerMobile,
      customerName,
      orderLines,
      studentClass,
      transactionDate : new Date(transactionDate),
      created_on: new Date(),
      updated_on: new Date(),
      created_by: user.user_name,
      updated_by: user.user_name
    });

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

Last updated