How do you conditionally do a SET (or anything else) based on the result of a previous line? So for example the query below (which doesn't work atm), where I'm trying to update something based on if the count is 1 (and not do it otherwise). Query doesn't like statements after THEN so that's where the error is occurring. I've tried also using WHERE clauses but no luck as of yet.
Is there any other way to accomplish what I'm trying to do below in a single query?
MATCH (n:Thing { name: 'asdf' })
OPTIONAL MATCH (p:Thing)-[:HAS_CHILD]->(n)
OPTIONAL MATCH (c)<-[:HAS_CHILD]-(p:Thing)
WITH c, p, n,
CASE count(c)
WHEN 1
THEN SET p.btogstate = 'closed'
DETACH DELETE n
Update
Tried the following based on a similar post but still no luck:
MATCH (n:Thing { name: 'asdf' })
OPTIONAL MATCH (p:Thing)-[:HAS_CHILD]->(n)
OPTIONAL MATCH (c)<-[:HAS_CHILD]-(p:Thing)
WITH c, p, n,
CASE WHEN count(c) = 1 then [1] else [] end as countOne
FOREACH (x in countOne | SET p.btogstate = 'closed'
DETACH DELETE n
Update 2 (final)
While cybersam's solution worked in a particular situation, the following was the only thing that ended up working for my particular situation for reasons that I still don't understand, so here it is for anyone that might run into similar weirdness. The main difference is that instead of using count to see if there was more than one child for the parent node, I just did another match to see if there was anything else that was a child of the parent node that wasn't the originally matched child (i.e. where n<>y). If there was not (i.e. y is null) then update the property, if there was (i.e. y is not null) then don't do anything other than delete n:
MATCH (n:Thing { name: 'asdf' })
OPTIONAL MATCH (p:Thing)-[:HAS_CHILD]->(n)
with p, n
OPTIONAL MATCH (p)-[:HAS_CHILD]->(y)
WHERE n<>y
WITH n, y, p,
CASE WHEN y IS NULL then [1] else [] end as countOne
FOREACH (x in countOne | SET p.btogstate = 'closed')
DETACH DELETE n;