def delete_comment(comment_id): try: query = ''' match (u:User)-[c:COMMENTED_ON]-(p:Post) where c.id = {} delete c ''' graph.run(query) return True except Exception as e: return False
def delete_post(post_id): try: query = ''' match (p:Post) where p.id={} del p '''.format(post_id) graph.run(query) return True except Exception as e: print(e)
def create(self): query = ''' match (u:User), (p:Post) where u.id={} and p.id={} create (u)-[c:COMMENTED_ON {{text: '{}', id: {} }}]->(p) return c '''.format(self.user_id, self.post_id, self.text, self.id) return graph.run(query)
def get_likes(post_id): query = ''' match (u:User)-[:LIKES]-(p:Post) where p.id='{}' return count(u) '''.format(post_id) result = graph.run(query) return result.data()[0]['count(u)']
def find_by_id(post_id): query = ''' match (p:Post) where p.id={} return p '''.format(post_id) result = graph.run(query) return result.data()[0]['p']
def get_posts_by_user(username): query = ''' match (u:User)-[:POSTED]->(p:Post) where u.name='{}' return p '''.format(username) result = graph.run(query) return result.data()
def get_friends(self): query = ''' match (u1:User)-[:FRIENDS_WITH]-(u2:User) where u1.username='******' return u2 '''.format(self.username) result = graph.run(query) return [User.neo4j_to_user(nuser) for nuser in result.data()]
def get_post_likers(self): query = ''' match (u:User)-[:LIKES]->(p:Post) where p.id='{}' return u '''.format(self.id) result = graph.run(query) return [ '{} {}'.format(i['firstname'], i['lastname']) for i in result() ]
def friends_with(self, username): query = ''' match (u1:User)-[rel]-(u2:User) where u1.username='******'and u2.username='******' return type(rel) as type '''.format(self.username, username) result = graph.run(query) if result.data()[0]['type'] == 'FRIENDS_WITH': return True return False
def update_post(self, text): try: query = ''' match (p:Post) where p.id='{}' set p.text='{}' return p '''.format(self.id, text) result = graph.run(query) return Post.neo4j_to_post(result.data()) except Exception as e: return False
def get_post_comments(post_id): try: query = ''' match (u:User)-[c:COMMENTED_ON]->(p:Post) where p.id={} return u.username as creator, c.text as text '''.format(post_id) print(query) result = graph.run(query) return result.data() except Exception as e: print(e) return None
from graph import graph graph.run(debug=True)