Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejavascript
function customAdd(request, response) {
  pjs.defineTable("orderline", { keyed: true, add: true });

  var qty = parseInt(request.body.qty) || 1;

  // Make sure the QTY is >= 1
  request.body.qty = qty < 1 ? 1 : qty;

  var result = orderline.write(null, request.body);
  response.send(result);
}

function findOne(request, response) {
  pjs.defineTable("orderline", { keyed: true, read: true });
  var order = request.order || request.query.order || request.param.order;
  var line = request.line || request.query.line || request.param.line;
  var data = orderline.getRecord(order, line);
  if (response)
    response.send(data);
  else
    return data;
} 
module.exports = {
  useSQL: true,
  dbObject: "order_line",
  uniqueKey: [
    { field: "order_id", ascending: true },
    { field: "line_id", ascending: true }
  ],
 
  findOne: findOne,

  routes: {
    add: { path:"/orderdetails", handler: customAdd },      // notice the handler property is set to this "customAdd" function.
    get: "/orderdetails/:order/:line",
    findOne: { method: "get", path:"/orderdetails/find"},   // notice that handler property is not set, but the name "findOne" is an exported function, so it will auto connect them.
    getList: "/orderdetails/:order",
    update: "/orderdetails/:order/:line",
    delete: {path: "/orderdetails/:order/:line", authorize:"apiAuthenticate.js"}, // notice the authorize property is set to a javascript file.  This file is your fileyours to create and maintain. It will be called before this action.
  }
}

...

Code Block
languagejavascript
function changeQuantity(request, response) {
  var result = null;

  pjs.defineTable("orderline", { keyed: true, read: true, update: true, delete: true });
  var qty = parseInt(request.body.qty) || 0;

  // If QTY is <= 0 then delete it, else update it
  if (qty <= 0)
    result = orderline.delete([request.params.order, request.params.line]);
  else {
    orderline.getRecord([request.params.order, request.params.line]);
    result = orderline.update(null, request.body);
  }

  response.send(result);
}
 
module.exports = {
  ...
  changeQuantity: changeQuantity,    // This is only be needed if you want to expose this function to other modules

  routes: {
    ...
    updQty: {method: "patch", path: "/orderdetails/:order/:line", handler: changeQuantity }
  }
}


Code Block
titleExample of apiAuthentication.js
function authenticate(request, response, next) {
 
  const Fiber = require('profoundjs-fibers');
 
  // Do what is needed to authenticate/authorize the user:: pjs.query, pjs.call, ...
 
  // For example, say a request contains an API key in a header
  //  And you have a table that contains API keys and values for API Users
  //  This authentication logic might look something like this:
  let filter = pjs.data.createCondition("apiKey", "=", request.headers["api-key"]);
  let user = pjs.data.get("apiUsers", filter, 1);
 
  if (!user)
    throw new pjs.NotAuthorizedError();
 
  Fiber.current.session.user = user.name;
 
  // "next() -- is what will call your webservice
  next();
}
 
exports.run = authenticate;