Database Reference
In-Depth Information
Listing 10-29.
The
follow
Route and
follow service
Method
# follow a user & return the updated list of users being followed
get '/follow/:username' do
content_type :json
json :following => follow(neo,request.cookies[graphstoryUserAuthKey],params[:username])
end
# the follow method in the User class
def follow(neo,currentusername,username)
cypher = " MATCH (user1:User {username:{cu}} ), (user2:User {username:{u}} ) " +
" CREATE UNIQUE user1-[:FOLLOWS]->user2 " +
" WITH user1" +
" MATCH (user1)-[f:FOLLOWS]->(users)" +
" RETURN users.firstname as firstname, users.lastname as lastname, "+
" users.username as username " +
" ORDER BY users.username"
results=neo.execute_query(cypher, {:cu => currentusername, :u => username} )
results["data"].map {|row| Hash[*results["columns"].zip(row).flatten] }
end
The unfollow feature for the
FOLLOWS
relationships uses a nearly identical application flow as follows feature. In
the
unfollow
method, shown in Listing 10-30, the controller passes in two arguments—the current username and
username to be unfollowed. As with the
follow
method, once the operation is completed, the next part of the query
then runs a
MATCH
on the users being followed to return the full list of followers ordered by the username.
Listing 10-30.
The
unfollow
Route and
unfollow service
Method
# unfollow a user & return the updated list of users being followed
get '/unfollow/:username' do
content_type :json
json :following => unfollow(neo,request.cookies[graphstoryUserAuthKey],params[:username])
end
# the unfollow method in the User class
def unfollow(neo,currentusername,username)
cypher = "MATCH (user1:User {username:{cu}} )-[f:FOLLOWS]->(user2:User {username:{u}} ) " +
" DELETE f " +
" WITH user1" +
" MATCH (user1)-[f:FOLLOWS]->(users)" +
" RETURN users.firstname as firstname, users.lastname as lastname, "+
" users.username as username " +
" ORDER BY users.username"
results=neo.execute_query(cypher, {:cu => currentusername, :u => username} )
results["data"].map {|row| Hash[*results["columns"].zip(row).flatten] }
end