Database Reference
In-Depth Information
To search for users to follow, the UserController uses the Searchbyusername method, which calls the
searchNotFollowing in UserService . The first part of the WHERE clause in searchNotFollowing returns users whose
username matches on a wildcard String value (Listing 7-26). The second part of the WHERE clause in searchNotFollowing
checks to make sure the users in the MATCH clause are not already being followed by the current user.
Listing 7-26. searchNotFollowing Method in the UserService
// UserService
public List<User> searchNotFollowing(String currentusername, String username)
{
username = username.ToLower() + ".*";
List<User> following = _graphClient.Cypher
.Match(" (n:User), (user { username:{c}}) ")
.WithParam("c", currentusername.ToLower())
.Where(" (n.username =~ {u} AND n <> user) ")
.AndWhere("(NOT (user)-[:FOLLOWS]->(n)) ")
.WithParam("u", username)
.Return(n => n.As<User>())
.OrderBy("n.username")
.Results.ToList<User>();
return following;
}
The searchByUsername in {PROJECTROOT}/Content//js/graphstory.js uses an AJAX request and formats the
response in renderSearchByUsername . If the list contains users, it will be displayed in the center of the page under the
search form, as shown in Figure 7-5 . Otherwise, the response will display “No Users Found”.
Once the search returns results, the next action would be to click on the “Add as Friend” link, which will call the
addfriend method in graphstory.js . This will perform an AJAX request to the follow method in the UserController
and call follow in UserService . The follow method in UserService , shown in Listing 7-27, will create the
relationship between the two users by first finding each entity via the MATCH clause and then use 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 7-27. The follow Method
// UserService
// follows a user and also returns the list of users being followed
public List<User> follow(String currentusername, String username)
{
List<User> following = _graphClient.Cypher
.Match(" (user1:User {username:{cu}} ), (user2:User {username:{u}} ) ")
.WithParams(new { cu = currentusername.ToLower(), u = username.ToLower() })
.CreateUnique("user1-[:FOLLOWS]->user2")
.With(" user1 ")
.Match(" (user1)-[f:FOLLOWS]->(users) ")
Search WWH ::




Custom Search