Database Reference
In-Depth Information
.WithParams(new {title=content.title, url=content.url, tagstr=content.tagstr })
.ForEach(" (tagName in {tags} | " +
"MERGE (t:Tag {wordPhrase:tagName})" +
" MERGE (c)-[:HAS]->(t) " +
" )")
.WithParam("tags", tags)
.With("user, c")
.Return(() => Return.As<MappedContent>(" { contentId: c.contentId, title: c.title,
url: c.url," +
" tagstr: c.tagstr, timestamp: c.timestamp, userNameForPost: user.username, owner: true } "))
.Results.Single();
return mappedContent;
}
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
method in the
SocialController
. This method then calls the delete method in
ContentService
, shown in Listing 7-35.
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, then the node and its relationships can be removed from the graph.
Listing 7-35.
The
delete
Method for
ContentService
public void delete(string contentId, string username)
{
_graphClient.Cypher
.Match("(u:User { username: {u} }), (c:Content { contentId: {contentId} })")
.WithParams(new { u = username, contentId = contentId})
.With("u,c")
.Match(" (u)-[:CURRENTPOST]->(c)-[:NEXTPOST]->(nextPost) ")
.Where("nextPost is not null ")
.CreateUnique(" (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 ")
.CreateUnique(" (before)-[:NEXTPOST]->(after) ")
.With(" count(before) as cnt ")
.Match(" (c:Content { contentId: {contentId} })-[r]-() ")
.Delete("c,r")
.ExecuteWithoutResults();
}