def get_facts_by_title(title): query = ''' MATCH (fact:Fact) WHERE fact.title = {title} RETURN fact ''' return graph.run(query, title=title)
def get_questions_by_tag(tag): query = ''' MATCH (question:Question)<-[:TAGGED]-(t:Tag) WHERE t.name = {tag} RETURN question ''' return graph.run(query, tag=tag)
def get_questions_by_title(title): query = ''' MATCH (question:Question) WHERE question.title = {title} RETURN question ''' return graph.run(query, title=title)
def get_facts_by_text(text): query = ''' MATCH (fact:Fact) WHERE fact.text CONTAINS {text} RETURN fact ''' return graph.run(query, text=text)
def get_questions_by_text(text): query = ''' MATCH (question:Question) WHERE question.text CONTAINS {text} RETURN question ''' return graph.run(query, text=text)
def get_fact_from_id(fact_id): query = ''' MATCH (fact:Fact) WHERE fact.id = {fact_id} RETURN fact ''' return graph.run(query, fact_id=fact_id)
def delete_fact(self, fact_id): query = ''' MATCH (user:User)-[:PUBLISHED]->(fact:Fact) WHERE user.username = {username} AND fact.id = {fact_id} DETACH DELETE (fact) ''' return graph.run(query, username=self.username, fact_id=fact_id)
def delete_question(self, question_id): query = ''' MATCH (user:User)-[:PUBLISHED]->(question:Question) WHERE user.username = {username} AND question.id = {question_id} DETACH DELETE (question) ''' return graph.run(query, username=self.username, question_id=question_id)
def get_node_from_id(primary_label, node_id): query = f''' MATCH (node:{primary_label}) WHERE node.id = '{node_id}' RETURN node ''' return graph.run(query) #, primary_label=primary_label, node_id=node_id)
def get_question_tags_from_id(question_id): query = ''' MATCH (question:Question)<-[:TAGGED]-(tag:Tag) WHERE question.id = {question_id} RETURN COLLECT(tag.name) AS tags ''' return graph.run(query, question_id=question_id)
def get_fact_tags_from_id(fact_id): query = ''' MATCH (fact:Fact)<-[:TAGGED]-(tag:Tag) WHERE fact.id = {fact_id} RETURN COLLECT(tag.name) AS tags ''' return graph.run(query, fact_id=fact_id)
def get_question_from_id(question_id): query = ''' MATCH (question:Question) WHERE question.id = {question_id} RETURN question ''' return graph.run(query, question_id=question_id)
def get_facts_by_tag(tag): query = ''' MATCH (fact:Fact)<-[:TAGGED]-(t:Tag) WHERE t.name = {tag} RETURN fact ''' return graph.run(query, tag=tag)
def get_recent_questions(self): query = ''' MATCH (user:User)-[:PUBLISHED]->(question:Question)<-[:TAGGED]-(tag:Tag) WHERE user.username = {username} RETURN question, COLLECT(tag.name) AS tags ORDER BY question.timestamp DESC LIMIT 5 ''' return graph.run(query, username=self.username)
def get_recent_facts(self): query = ''' MATCH (user:User)-[:PUBLISHED]->(fact:Fact)<-[:TAGGED]-(tag:Tag) WHERE user.username = {username} RETURN fact, COLLECT(tag.name) AS tags ORDER BY fact.timestamp DESC LIMIT 5 ''' return graph.run(query, username=self.username)
def get_todays_recent_facts(): query = ''' MATCH (user:User)-[:PUBLISHED]->(fact:Fact)<-[:TAGGED]-(tag:Tag) WHERE fact.date = {today} RETURN user.username AS username, fact, COLLECT(tag.name) AS tags ORDER BY fact.timestamp DESC LIMIT 5 ''' return graph.run(query, today=date())
def get_todays_recent_questions(): query = ''' MATCH (user:User)-[:PUBLISHED]->(question:Question)<-[:TAGGED]-(tag:Tag) WHERE question.date = {today} RETURN user.username AS username, question, COLLECT(tag.name) AS tags ORDER BY question.timestamp DESC LIMIT 5 ''' return graph.run(query, today=date())
def get_related_questions(primary_label, node_id): query = f''' MATCH (node:{primary_label})-[:RELATED_TO]-(question:Question) WHERE node.id = '{node_id}' RETURN question ORDER BY question.timestamp DESC LIMIT 5 ''' return graph.run(query)
def get_related_facts(primary_label, node_id): query = f''' MATCH (node:{primary_label})-[:RELATED_TO]-(fact:Fact) WHERE node.id = '{node_id}' RETURN fact ORDER BY fact.timestamp DESC LIMIT 5 ''' return graph.run(query)
def naive_rule(username): """ Given a username, automatically select a random fact or question that has not been viewed yet. Returns the node containing the fact or question. """ query = ''' MATCH (post) WHERE post:Fact OR post:Question MATCH (user:User) WHERE NOT (user)-[:VIEWED]->(post) AND user.username = {username} RETURN post ''' results = graph.run(query, username=username) results = [x for x in results] try: return random.choice(results) except IndexError: pass