Database Reference
In-Depth Information
The searchByUsername in {PROJECTROOT}/app/public/js/graphstory.js uses an AJAX request and formats
the response in renderSearchByUsername . If the list contains users, it is displayed in the center of the page under the
search form, as shown in Figure 9-9 . Otherwise, the response displays “No Users Found”.
Once the search returns results, the next action is to click on the “Add as Friend” link, which calls the addfriend
method in graphstory.js . This performs an AJAX request to the follow method in the UserController and calls
follow in UserService . The follow method in UserService , shown in Listing 9-26, will create the relationship
between the two users by first finding each entity via the MATCH clause and then using the CreateUnique clause to
create the directed FOLLOWS relationship. 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 9-26. The follow Route and follow service Method
# follow a user
@route('/follow/<username>', method='GET')
def follow(username):
following = User().follow(graph_db, request.get_cookie(graphstoryUserAuthKey), username)
response.content_type = 'application/json'
return dumps({"following": User().users_results_as_array(following)})
# the follow method in the User class
def follow(self, graph_db, currentusername, username):
query = neo4j.CypherQuery(graph_db,
" 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")
params = {"cu": currentusername,"u": username}
result = query.execute(**params)
return result
The unfollow feature for the FOLLOWS relationships uses a nearly identical application flow as follows feature.
In the unfollow method, shown in Listing 9-27, the controller passes in two arguments—the current username and
username to be unfollowed. As with the follows 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 9-27. The unfollow Route and unfollow Method
@route('/unfollow/<username>', method='GET')
def unfollow(username):
following = User().unfollow(graph_db, request.get_cookie(graphstoryUserAuthKey), username)
response.content_type = 'application/json'
return dumps({"following": User().users_results_as_array(following)})
 
Search WWH ::




Custom Search