Database Reference
In-Depth Information
Listing 8-37. Consumption Route to Show a List of Products and the Product Trail of the Current User
# show products and products VIEWED by user
$app->get('/consumption', $isLoggedIn, function() use ($app){
# get products by page
$products = Product::getProducts(0);
$next = true;
$nextPageUrl = "/consumption/10";
$productTrail = Product::getProductTrail($_SESSION['username']);
$app->view()->setData(array('products'=>$products,
'next'=>$next,
'nextPageUrl'=>$nextPageUrl,
'productTrail'=>$productTrail,
'title'=>'Consumption'));
$app->render('graphs/consumption/index.mustache');
})->name('consumption');
The sample application used the createUserViewAndReturnViews method in the Product class to first find
the product being viewed and then create an explicit relationship type called VIEWED . As you might have noticed,
this is the first relationship type in the application that also contains properties. In this case, we are creating a
timestamp with a date and string value of the timestamp. The query, provided in Listing 8-38, checks to see if a VIEWED
relationship already exists between the user and the product using MERGE .
In the MERGE section of the query, if the result of the MERGE is zero matches, then a relationship is created with key
value pairs on the new relationship, specifically dateAsStr and timestamp. Finally, the query uses MATCH to return the
existing product views.
Listing 8-38. Add consumption_add Route and create_user_view_and_return_views Method
// add a product via VIEWED relationship and return VIEWED products
$app->get('/consumption/add/:productNodeId', function($productNodeId) use ($app){
#save the view and return the full list of views
$productTrail=Product::createUserViewAndReturnViews(
$_SESSION['username'],$productNodeId);
$app->response->headers->set('Content-Type', 'application/json');
echo '{"productTrail": ' . json_encode($productTrail) . '}';
})->name('consumption-add');
// the method to add a user view of a product and return all views
public static function createUserViewAndReturnViews($username,$productNodeId){
$productNodeId = intval($productNodeId);
# create timestamp and string display
$ts = time();
$timestampAsStr = date('n/d/Y',$ts) . ' at ' . date('g:i A',$ts);
Search WWH ::




Custom Search