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 details():
    con = connect()
    content = get_json(request)
    required_data = ["forum"]
    related = related_exists(content)
    if 'forum' in related:
        con.close()
        return json.dumps({"code": 3, "response": "error"})
    try:
        check_data(content, required_data)
        forum = query.show_forum(con, content["forum"], related)
    except Exception as e:
        con.close()
        return json.dumps({"code": 1, "response": (e.message)})
    con.close()
    return json.dumps({"code": 0, "response": forum})
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