def show_forum(connect, short_name, related):
    forum = DB_connect.select_query(connect,
        'SELECT id, name, short_name, user FROM Forum WHERE short_name = %s LIMIT 1;', (short_name, ))
    if len(forum) == 0:
        raise ("No forum with short_name: " + short_name)
    forum = forum_description(forum)
    if "user" in related:
        forum["user"] = users.show_user(connect, forum["user"])
    return forum
def details():
    con = connect()
    request_data = get_json(request)
    required_data = ["user"]
    try:
        check_data(request_data, required_data)
        user = query.show_user(connect = con, email = request_data["user"])
    except Exception as e:
        con.close()
        return json.dumps({"code" : 1, "response": (e.message)})
    con.close()
    return json.dumps({"code": 0, "response": user})
def posts_list(connect, entity, params, identifier, related=[]):
    query = (
        "SELECT date, dislikes, forum, id, isApproved, isDeleted, isEdited, isHighlighted, isSpam, likes, message,  \
            parent, (likes-dislikes) as points, thread, user FROM Post WHERE "
        + entity
        + " = %s "
    )
    print ("step2")
    parameters = [identifier]
    if "since" in params:
        query += " AND date >= %s"
        parameters.append(params["since"])

    if "order" in params:
        query += " ORDER BY date " + params["order"]
    else:
        query += " ORDER BY date DESC "

    if "limit" in params:
        query += " LIMIT " + str(params["limit"])

    post_ids = DB_connect.select_query(connect, query, parameters)
    post_list = []
    for post in post_ids:
        pf = {
            "date": str(post[0]),
            "dislikes": post[1],
            "forum": post[2],
            "id": post[3],
            "isApproved": bool(post[4]),
            "isDeleted": bool(post[5]),
            "isEdited": bool(post[6]),
            "isHighlighted": bool(post[7]),
            "isSpam": bool(post[8]),
            "likes": post[9],
            "message": post[10],
            "parent": post[11],
            "points": post[12],
            "thread": post[13],
            "user": post[14],
        }
        if "user" in related:
            pf["user"] = users.show_user(connect, pf["user"])
        if "forum" in related:
            pf["forum"] = forums.show_forum(connect, pf["forum"], [])
        if "thread" in related:
            pf["thread"] = threads.show_thread(connect, pf["thread"], [])
        post_list.append(pf)
    return post_list
def show_thread(connect, thread, related):
    thread_all = DB_connect.select_query(connect, 
                'SELECT  date, forum, id,  isClosed,  isDeleted, message, slug,  title,  user, dislikes, likes, posts, (likes - dislikes) as points \
                FROM Thread WHERE id = %s LIMIT 1;', (thread, ) )
    if len(thread_all) == 0:
         raise Exception('No thread with id=' + str(thread))
    thread = thread_description(thread_all) 
    thread["dislikes"] = thread_all[0][9]   
    thread["likes"]    = thread_all[0][10]
    thread["posts"]    = thread_all[0][11]
    thread["points"]   = thread_all[0][12]
    if "user" in related:
        thread["user"] = users.show_user(connect, thread["user"])
    if "forum" in related:
        thread["forum"] = forums.show_forum(connect, thread["forum"], [])
    return thread
def threads_list(connect, entity, params, identifier, related=[]):
    query = "SELECT date, forum, id, isClosed, isDeleted, message, slug, title, user, dislikes, likes, (likes - dislikes) as points, posts  \
            FROM Thread WHERE " + entity + " = %s "
    parameters = [identifier]
    if "since" in params:
        query += " AND date >= %s"
        parameters.append(params["since"])

    if "order" in  params:
        query += " ORDER BY date " + str(params["order"])
    else:
        query += " ORDER BY date DESC "

    if "limit" in params:
        
        query += " LIMIT " + str(params["limit"])

    thread_ids = DB_connect.select_query(connect, query, parameters)
    thread_list = []
    for thread in thread_ids:
        thr = {
            'date': str(thread[0]),
            'forum': thread[1],
            'id': thread[2],
            'isClosed': bool(thread[3]),
            'isDeleted': bool(thread[4]),
            'message': thread[5],
            'slug': thread[6],
            'title': thread[7],
            'user': thread[8],
            'dislikes': thread[9],
            'likes': thread[10],
            'points': thread[11],
            'posts': thread[12],
        }
        if "user" in related:
            thr["user"] = users.show_user(connect,thr["user"])
        if "forum" in related:
            thr["forum"] = forums.show_forum(connect, thr["forum"], [])    
        thread_list.append(thr)    
    return thread_list
def show_post(connect, post, related):
    post_all = DB_connect.select_query(
        connect,
        "SELECT  date, forum, id,  isApproved, isDeleted, isEdited, isHighlighted, isSpam, message, \
                parent, thread, user,  dislikes, likes, (likes - dislikes) as points \
                FROM Post WHERE id = %s LIMIT 1;",
        (post,),
    )
    if len(post_all) == 0:
        raise Exception("No post with id=" + str(post))
    post = post_description(post_all)
    post["dislikes"] = post_all[0][12]
    post["likes"] = post_all[0][13]
    post["points"] = post_all[0][14]
    if "user" in related:
        print ("useeeeer")
        post["user"] = users.show_user(connect, post["user"])
    if "thread" in related:
        post["thread"] = threads.show_thread(connect, post["thread"], [])
    if "forum" in related:
        post["forum"] = forums.show_forum(connect, post["forum"], [])
    return post