def get_post_of_friend(cls, user_id): user_id = 'sn_users/' + str(user_id) #query with graph # FOR post IN 1..2 OUTBOUND @user_id sn_friend, sn_user_post # câu query này sẽ select theo 2 cạnh của sn_friend và sn_user_post với giá trị user_id và select hết tất cả các item select đc gồm cả user và post # để chỉ lấy được các post thì t phải filter ra các item có user_type == null vì post ko có user_type chỉ user mới có user_type query_string = "FOR post IN 1..2 OUTBOUND @user_id sn_friend, sn_user_post OPTIONS {bfs: true, uniqueVertices: 'global'} " \ "FILTER post.user_type == NULL " \ "SORT post.created_at DESC " \ "LET user = (FOR user IN sn_users FILTER user._key == TO_STRING(post.user_id) LIMIT 1 " \ "RETURN user)[0] " \ "RETURN merge(post,{user})" #query with vertex and edge # query_string = "LET friends = (" \ # "FOR user IN OUTBOUND @user_id sn_friend OPTIONS {bfs: true, uniqueVertices: 'global'} " \ # "RETURN user._id) " \ # "LET post=(" \ # "FOR friend IN friends " \ # "FOR post IN OUTBOUND friend sn_user_post OPTIONS {bfs: true, uniqueVertices: 'global'}" \ # "SORT post.created_at DESC " \ # "RETURN post) " \ # "RETURN post " parameter = {'user_id': user_id} result = ArangoCore.execute_query(query_string, parameter) return result
def get_download_history(cls, download_id): try: query_string = "LET downloads =(FOR download IN sn_user_download FILTER download._key==@download_id LIMIT 1 RETURN download) RETURN downloads[0]" parameter = {'download_id': download_id} result = ArangoCore.execute_query(query_string, parameter) return result[0] if len(result) > 0 else None except Exception as exception: raise exception
def get_data_of_user(cls, user_id): user_id = 'sn_users/' + str(user_id) query_string = "FOR data IN OUTBOUND @user_id sn_user_data OPTIONS {bfs: true, uniqueVertices: 'global'} " \ "SORT data.created_at DESC " \ "RETURN data" parameter = {'user_id': user_id} result = ArangoCore.execute_query(query_string, parameter) return result
def get_user_channels_by_user_id(cls, user_id): try: start_id = 'rv_users/{0}'.format(user_id) query_string = "FOR v, e IN OUTBOUND @start_id rv_user_channels OPTIONS {bfs: true, uniqueVertices: 'global'} RETURN e.name " parameter = {'start_id': str(start_id)} return ArangoCore.execute_query(query_string, parameter) except Exception as exception: raise exception
def get_post_of_user(cls, user_id): user_id = 'sn_users/' + str(user_id) query_string = "FOR post IN OUTBOUND @user_id sn_user_post OPTIONS {bfs: true, uniqueVertices: 'global'} " \ "SORT post.created_at DESC " \ "LET user = (FOR user IN sn_users FILTER user._key == TO_STRING(post.user_id) LIMIT 1 " \ "RETURN user)[0] " \ "RETURN merge(post,{user})" parameter = {'user_id': user_id} result = ArangoCore.execute_query(query_string, parameter) return result
def get_commnet(cls, post_id): post_id = 'sn_posts/' + str(post_id) query_string = "FOR comment IN OUTBOUND @post_id sn_post_comment OPTIONS {bfs: true, uniqueVertices: 'global'} " \ "SORT comment.created_at ASC " \ "LET user = (FOR user IN sn_users FILTER user._key == TO_STRING(comment.user_id) LIMIT 1 " \ "RETURN user)[0] " \ "RETURN merge(comment,{user})" parameter = {'post_id': post_id} result = ArangoCore.execute_query(query_string, parameter) return result
def get_data(cls, data_id, get_name=False): try: if get_name: query_string = "FOR data IN sn_datas FILTER data.id == @data_id LIMIT 1 RETURN data.name" else: query_string = "FOR data IN sn_datas FILTER data.id == @data_id LIMIT 1 RETURN data" parameter = {'data_id': data_id} result = ArangoCore.execute_query(query_string, parameter) return result[0] if len(result) > 0 else None except Exception as exception: raise exception
def get_download_history_of_user(cls, user_id): try: user_id = 'sn_users/' + user_id query_string = "LET historys = (FOR v,history IN OUTBOUND @user_id sn_user_download " \ "SORT history.created_at DESC " \ "RETURN merge(history,{infor:v})) " \ "FOR history IN historys " \ "FOR data IN sn_datas " \ "FILTER data.id == history.infor.data_id " \ "RETURN merge(history,{data:data}) " parameter = {'user_id': user_id} return ArangoCore.execute_query(query_string, parameter) except Exception as exception: raise exception
def get_user_channels_by_band_id(cls, band_id): try: start_id = 'rv_users/{0}'.format(band_id) query_string = "FOR user IN INBOUND @start_id rv_user_follows OPTIONS {bfs: true, uniqueVertices: 'global'} " query_string += "LET channels = (FOR v, e IN OUTBOUND user._id rv_user_channels OPTIONS {bfs: true, uniqueVertices: 'global'} RETURN e.name) " query_string += "RETURN {'user_id': user.id, 'channels': channels}" parameter = {'start_id': str(start_id)} channels = [] user_channels = ArangoCore.execute_query(query_string, parameter) for user_channel in user_channels: channels += user_channel['channels'] return channels except Exception as exception: raise exception