Database Reference
In-Depth Information
Deleting a Status Update
As with the “edit” option, when status updates are displayed, the current user's status updates contain a link to
“Delete” the status. Once clicked, it asks if you want it deleted (no regrets!) and, if accepted, generates an AJAX GET
request to call the delete_content route and corresponding method in the Content class, shown in Listing 9-31.
The Cypher in the delete method begins by finding the user and content that will be used in the rest of the query.
In the first MATCH , you can determine if this status update is the CURRENTPOST by checking to see if it is related to a
NEXTPOST . If this relationship pattern matches, make the NEXTPOST into the CURRENTPOST with CREATE UNIQUE .
Next, the query will ask if the status update is somewhere the middle of the list, which is performed by
determining if the status update has incoming and outgoing NEXTPOST relationships. If the pattern is matched, then
connect the before and after status updates via NEXTPOST .
Regardless of the status update's location in the linked list, retrieve it and its relationships and then delete the
node along with all of its relationships.
To recap, if one of the relationship patterns matches, replace that pattern with the nodes on either side of the
status update in question. Once that has been performed, the node and its relationships can be removed from the
graph.
Listing 9-31. delete_content Route and delete_content Method for a Status Update
# delete a status update
@route('/posts/delete/<contentId>', method='GET')
def delete_content(contentId):
#delete the status update
delete = Content().delete_content(graph_db, request.get_cookie(graphstoryUserAuthKey),
contentId)
# set response type
response.content_type = 'application/json'
# return the response
return {"msg": "ok"}
# delete a status update
def delete_content(self, graph_db, username, contentId):
query = neo4j.CypherQuery(graph_db,
" MATCH (u:User { username: {u} }), (c:Content { contentId: {contentId} }) " +
" WITH u,c " +
" MATCH (u)-[:CURRENTPOST]->(c)-[:NEXTPOST]->(nextPost) " +
" WHERE nextPost is not null " +
" CREATE UNIQUE (u)-[:CURRENTPOST]->(nextPost) " +
" WITH count(nextPost) as cnt " +
" MATCH (before)-[:NEXTPOST]->(c:Content { contentId: {contentId}})-[:NEXTPOST]->(after) " +
" WHERE before is not null AND after is not null " +
" CREATE UNIQUE (before)-[:NEXTPOST]->(after) " +
" WITH count(before) as cnt " +
" MATCH (c:Content { contentId: {contentId} })-[r]-() " +
" DELETE c, r")
params = {"u": username, "contentId": contentId}
result = query.execute(**params)
return result
 
Search WWH ::




Custom Search