Database Reference
In-Depth Information
Listing 10-39. Add consumption_add Route and create_user_view_and_return_views Method
#add a product via VIEWED relationship and return VIEWED products
get '/consumption/add/:productNodeId' do
# productNodeId to integer
productNodeId=params[:productNodeId].to_i
#create or update the user view and
#return the product trail as JSON
json :productTrail => create_user_view_and_return_views(neo,
request.cookies[graphstoryUserAuthKey],
productNodeId)
end
# the method to add a user view of a product and return all views
def create_user_view_and_return_views(neo,username,productNodeId)
# create timestamp and string display
time = Time.now
tsAsInt = time.to_i
timestampAsStr= time.strftime("%m/%d/%Y") +
" at " + time.strftime("%l:%M %p")
cypher = " MATCH (p:Product), (u:User { username:{u} })" +
" WHERE id(p) = {productNodeId}" +
" WITH u,p" +
" MERGE (u)-[r:VIEWED]->(p)" +
" SET r.dateAsStr={timestampAsStr}, r.timestamp={ts}" +
" WITH u " +
" MATCH (u)-[r:VIEWED]->(p)" +
" RETURN p.title as title, r.dateAsStr as dateAsStr" +
" ORDER BY r.timestamp desc"
results=neo.execute_query(cypher, {:u => username,
:productNodeId => productNodeId,
:timestampAsStr => timestampAsStr,
:ts => ts} )
results["data"].map {|row| Hash[*results["columns"].zip(row).flatten] }
end
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.
Filtering Consumption for Users
One practical use of the consumption model is to create a content trail for users, as shown in Figure 10-14 . As a user
clicks on items in the scrolling product stream, the interaction is captured using create_user_view_and_return_
views , which ultimately returns a List of relationship objects of the VIEWED type.
 
Search WWH ::




Custom Search