예제 #1
0
def post_question(qtitle, qbody, tags_list, userId):
    if len(tags_list) != 0:
        tags = "{" + tags_list[0]
        for tag in tags_list[1:]:
            tags += ", " + tag
        tags += "}"

    qtitle = repr(qtitle)  # for '\n' character, converting into raw string
    qbody = repr(qbody)
    insert_question = gql('''
        mutation {
            insert_Questions(
                objects: {
                    Title: "''' + qtitle + '''",
                    Body: "''' + qbody + '''",
                    Tags: "''' + tags + '''",
                    UserId: ''' + str(userId) + '''
                }
            ) {
                returning {
                    Id
                }
            }
        }
    ''')
    print('new id = ')
    print(insert_question)
    pprint.pprint(insert_question)
    try:
        mutation_result = client.execute(insert_question)
        print("Question Id: ", mutation_result)
    except Exception as e:
        print(e)
예제 #2
0
def post_question(qtitle, qbody, tags_list, userId):
    if len(tags_list) != 0:
        tags = "{" + tags_list[0]
        for tag in tags_list[1:]:
            tags += ", " + tag
        tags += "}"

    insert_question = gql('''
        mutation {
            insert_Questions(
                objects: {
                    Title: "''' + qtitle + '''",
                    Body: "''' + qbody + '''",
                    Tags: "''' + tags + '''",
                    UserId: ''' + str(userId) + '''
                }
            ) {
                returning {
                    Id
                }
            }
        }
    ''')

    try:
        mutation_result = client.execute(insert_question)
        print("Question Id: ", mutation_result)
    except Exception as e:
        print(e)
def query_question_for_page(Id):
    query_question = gql('''{
            Questions_by_pk(Id: ''' + str(Id) + ''') {
                Id
                Title
                Body
                VoteCount
                created_at
                Question_Answers(order_by: {VoteCount: desc}) {
                    Body
                    VoteCount
                    created_at
                    Answer_User {
                        Id
                        EmailId
                        FirstName
                        LastName
                    }
                }
                Question_User {
                    Id
                    EmailId
                    FirstName
                    LastName
                }
            }
        }
    ''')
    question = client.execute(query_question)
    question = question[
        'Questions_by_pk']  # "by_pk" means to query an entry by the private key
    pprint.pprint(question)
    return question
예제 #4
0
def questions_by_user(UserId):

    #select Title, Id from Questions where UserId = str(id)
    query_question = gql('''
    #     query {
    #         Questions(_like: ''' + str(UserId) + ''') {
    #             Id
    #             Title
    #         }
    #     }

    query {
        Questions(where: {UserId: { _eq: ''' + str(UserId) + '''}}) {
            Id
            Title
        }
    }


    ''')

    questions = client.execute(query_question)
    questions = questions['Questions']
    # pprint.pprint(questions)
    return questions
예제 #5
0
def print_users():
    query_users = gql('''
        query {
            Users {
                Id
                FirstName
                LastName
                EmailId
            }
        }
    ''')
    users = client.execute(query_users)
    for user in users["Users"]:
        print (user)
예제 #6
0
def user_exists(emailId):
    query_user = gql('''
        query {
            Users (where: {EmailId: { _like: "''' + emailId + '''"}}) {
                EmailId
            }
        }
    ''')
    user = client.execute(query_user)
    if len(user["Users"]) == 0:
        return False
    if emailId != user["Users"][0]["EmailId"]:
        return False
    return True
예제 #7
0
def get_login_details(emailId):
    query_user = gql('''
        query {
            Users (where: {EmailId: { _like: "''' + emailId + '''"}}) {
                EmailId,
                FirstName,
                LastName,
                Password,
                Id
            }
        }
    ''')
    user = client.execute(query_user)
    if len(user["Users"]) == 0:
        return None
    return user["Users"][0]
예제 #8
0
def query_question_for_list(Id):
    query_question = gql('''
        query {
            Questions_by_pk(Id: ''' + str(Id) + ''') {
                Title
                Body
                VoteCount
            }
        }
    ''')
    question = client.execute(query_question)
    question = question[
        'Questions_by_pk']  # "by_pk" means to query an entry by the private key
    print("Title:", question['Title'])
    print("Body:", question['Body'])
    print("VoteCount:", question['VoteCount'])
예제 #9
0
def downvote_answer(aId):
    downvote = gql('''
        mutation {
            update_Answers(
                where: {
                    Id: { _eq: ''' + str(aId) + ''' }
                }, 
                _inc: { VoteCount: -1 }
            ) {
                returning { VoteCount }
            }
        }
    ''')

    try:
        mutation_result = client.execute(downvote)
        print (mutation_result)
    except Exception as e:
        print (e)
예제 #10
0
def upvote_question(qId):
    upvote = gql('''
        mutation {
            update_Questions(
                where: {
                    Id: { _eq: ''' + str(qId) + ''' }
                }, 
                _inc: { VoteCount: 1 }
            ) {
                returning { VoteCount }
            }
        }
    ''')

    try:
        mutation_result = client.execute(upvote)
        print(mutation_result)
    except Exception as e:
        print(e)
예제 #11
0
def register_user(emailId, firstName, lastName, password):
    insert_users = gql('''
        mutation {
            insert_Users (
                objects:{
                    EmailId: "''' + emailId + '''",
                    FirstName: "''' + firstName + '''",
                    LastName: "''' + lastName + '''",
                    Password: "******"
                }
            ) {
                returning {
                    Id
                }
            }
        }
    ''')
    mutation_result = client.execute(insert_users)
    print(mutation_result)
def query_question_for_list(Id):
    query_question = gql('''
        query {
            Questions_by_pk(Id: ''' + str(Id) + ''') {
                Id
                Title
                Body
                VoteCount
            }
        }
    ''')
    question = client.execute(query_question)
    question = question['Questions_by_pk']  # "by_pk" means to query an entry by the private key
    return question
    # print ("Title:", question['Title'])
    # print ("Body:", question['Body'])
    # print ("VoteCount:", question['VoteCount'])

# if __name__ == "__main__":
    # for id in id_list:
    #     query_question_for_list(id)
예제 #13
0
def post_answer(qId, aBody, userId):
    insert_answer = gql('''
        mutation {
            insert_Answers(
                objects: {
                    QuestionId: ''' + str(qId) + ''',
                    Body: "''' + aBody + '''",
                    UserId: ''' + str(userId) + '''
                }
            ) {
                returning {
                    Id
                }
            }
        }
    ''')

    try:
        mutation_result = client.execute(insert_answer)
        print("Answer Id: ", mutation_result)
    except Exception as e:
        print(e)