Skip to content

Instantly share code, notes, and snippets.

@ablatner88
Last active September 11, 2023 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ablatner88/4ed7781c4370b01404fac05de89a7630 to your computer and use it in GitHub Desktop.
Save ablatner88/4ed7781c4370b01404fac05de89a7630 to your computer and use it in GitHub Desktop.
OrientDB Server-side function for creating a new order, populating the information and edge relationships. For use with the Intro to HTTP REST API tutorial.
var graph = orient.getGraph();
// Check valid parameters
if( ProductID == "" || BuyerID == "" ){
response.send(404, "Product or Buyer Missing", "text/plain", "Error: Missing ProductID or BuyerID Parameter" );
}
// Query for objects
var product = graph.getVertex(ProductID);
var buyer = graph.getVertex(BuyerID);
// Extract information
var productPrice = product.getProperty('Price');
var buyerPaymentInformation = buyer.getProperty('PaymentInformation');
var buyerShippingInformation = buyer.getProperty('Address');
// Check for valid lookups
if( product == null || buyer == null ){
response.send(404, "Product or Buyer Not Found", "text/plain", "Error: Invalid ProductID or BuyerID" );
} else {
//db.begin();
try{
// Create and save the new order
var order = graph.addVertex("class:Orders");
order.setProperty("PaymentInformation", buyerPaymentInformation);
order.setProperty("ShippingInformation", buyerShippingInformation);
order.setProperty("TotalPrice", productPrice);
// Commit so that we have a valid rid
graph.commit();
var OrderID = order.getProperty('@rid');
// Add Edges
// HasItem Edge from Order to Product
// HasOrder Edge from OUser to Order
var ItemInOrder = graph.addEdge(null, order, product, "ItemInOrder");
var HasOrder = graph.addEdge(null, buyer, order, "HasOrder");
graph.commit();
return "success";
}catch ( err ){
graph.rollback();
response.send(500, "Error on creating new order", "text/plain", err.toString() );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment