Database Reference
In-Depth Information
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" id="updateUser" class="btn btn-default">Update User</button>
</div>
</div>
</form>
User Edit Route
The
App
class contains a route with the path
/user/edit
, which takes the
JSON
object argument. The
User
object
is converted from a
JSON
string and returns a
User
object as
JSON
. The response could be used to update the form
elements, but because the values are already set within the form, there is no need to update the values. In this case,
the application uses the
JSON
response to let the user know if the update succeeded or not via a standard
JavaScript
alert message (Listing 10-23).
Listing 10-23.
/user/edit
Route
# edit user's first/last name
put '/user/edit' do
user = JSON.parse(request.body.read)
update_user(neo,user,request.cookies[graphstoryUserAuthKey])
json user
end
User Update Method
To complete the update, the controller layer calls the
update_user
method in
User
class. Because the object being
passed into the update method did nothing more than modify the first and last name of an existing entity, you can use
the
SET
clause via Cypher to update the properties in the graph, as shown in Listing 10-24. This Cypher statement also
makes use of the
MATCH
clause to retrieve the User node.
Listing 10-24.
The
update_user
Method in the User Class
def update_user(neo,user,username)
cypher = "MATCH (user:User {username:{u}} ) " +
"SET user.firstname = {fn}, user.lastname = {ln}"
neo.execute_query(cypher, {:fn => user["firstname"],:ln => user["lastname"], :u => username} )
end
Connecting Users
A common feature in social media applications is to allow users to connect to each other through an explicit
relationship. In the sample application, we use the directed relationship type called
FOLLOWS
. By going to the “Friends”
page within the social graph section, you can see the list of the users the current user is following, search for new
friends to follow, add them and remove friends the current user is following. The user management section of the
App
class contains each of the routes to control the flow for these features, specifically the routes that cover
friends
,
search_by_user_name
,
follow
, and
unfollow
.