def open_close_thread(id, isClosed): sql.update_query("UPDATE thread SET isClosed = %s WHERE id = %s", (isClosed, id, )) response = { "thread": id } return response
def save_forum(name, short_name, user): sql.update_query('INSERT INTO forum (name, short_name, user) VALUES (%s, %s, %s)', (name, short_name, user, )) forum = sql.select_query( 'select id, name, short_name, user FROM forum WHERE short_name = %s', (short_name, ) ) return forum_description(forum)
def dec_posts_count(post): thread = sql.select_query("SELECT thread FROM post WHERE id = %s", (post, )) try: sql.update_query("UPDATE thread SET posts = posts - 1 WHERE id = %s", (thread[0][0], )) except Exception as e: print(e.message) return
def vote(id, vote): try: if vote == -1: sql.update_query("UPDATE thread SET dislikes=dislikes+1, points=points-1 where id = %s", (id, )) else: sql.update_query("UPDATE thread SET likes=likes+1, points=points+1 where id = %s", (id, )) except Exception as e: print(e.message) return details(id=id, related=[])
def remove_restore(thread_id, status): if status == 1: posts = 0 else: posts = sql.select_query("SELECT COUNT(id) FROM post WHERE thread = %s", (thread_id))[0][0] sql.update_query("UPDATE thread SET isDeleted = %s, posts = %s WHERE id = %s", (status,posts,thread_id)) sql.update_query("UPDATE post SET isDeleted = %s WHERE thread = %s", (status,thread_id)) response = { "thread": thread_id } return response
def save_user(email, username, about, name, optional): isAnonymous = 0 if "isAnonymous" in optional: isAnonymous = optional["isAnonymous"] query = sql.update_query( 'INSERT INTO user (email, about, name, username, isAnonymous) VALUES (%s, %s, %s, %s, %s)', (email, about, name, username, isAnonymous, )) if query == STATUS_CODE['ALREADY_EXISTS']: raise Exception(query) user = sql.select_query('select email, about, isAnonymous, id, name, username FROM user WHERE email = %s', (email, )) return user_format(user)
def save_thread(forum, title, isClosed, user, date, message, slug, optional): isDeleted = 0 if "isDeleted" in optional: isDeleted = optional["isDeleted"] sql.update_query('INSERT INTO thread (forum, title, isClosed, user, date, message, slug, isDeleted) ' 'VALUES (%s, %s, %s, %s, %s, %s, %s, %s)', (forum, title, isClosed, user, date, message, slug, isDeleted, )) thread = sql.select_query( 'select date, forum, id, isClosed, isDeleted, message, slug, title, user, dislikes, likes, points, posts ' 'FROM thread WHERE slug = %s', (slug, ) ) thread = thread[0] response = { '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], } return response
def update_thread(id, slug, message): sql.update_query('UPDATE thread SET slug = %s, message = %s WHERE id = %s', (slug, message, id, )) return details(id=id, related=[])
def inc_posts_count(post): thread = sql.select_query("SELECT thread FROM post WHERE id = %s", (post, )) sql.update_query("UPDATE thread SET posts = posts + 1 WHERE id = %s", (thread[0][0], )) return
def update_user(email, about, name): sql.update_query('UPDATE user SET email = %s, about = %s, name = %s WHERE email = %s', (email, about, name, email, )) return details(email)
def remove_follow(email1, email2): sql.update_query('DELETE FROM follower WHERE follower = %s AND followee = %s', (email1, email2, )) return user.details(email1)
def add_follow(email1, email2): sql.update_query('INSERT INTO follower (follower, followee) VALUES (%s, %s)', (email1, email2, )) return user.details(email1)
def vote(vote_id, vote_type): if vote_type == -1: sql.update_query("UPDATE post SET dislikes=dislikes+1, points=points-1 where id = %s", (vote_id, )) else: sql.update_query("UPDATE post SET likes=likes+1, points=points+1 where id = %s", (vote_id, )) return details(details_id=vote_id, related=[])
def update(update_id, message): sql.update_query('UPDATE post SET message = %s WHERE id = %s', (message, update_id, )) return details(details_id=update_id, related=[])
def remove_restore(post_id, status): sql.update_query("UPDATE post SET isDeleted = %s WHERE post.id = %s", (status, post_id, )) return { "post": post_id }