def vote(vote_id, vote_type): DBconnect.exist(entity="Posts", identifier="id", value=vote_id) if vote_type == -1: DBconnect.update_query("UPDATE Posts SET dislikes=dislikes+1, points=points-1 where id = %s", (vote_id,)) else: DBconnect.update_query("UPDATE Posts SET likes=likes+1, points=points+1 where id = %s", (vote_id,)) return details(details_id=vote_id, related=[])
def remove_restore(thread_id, status): DBconnect.exist(entity="Threads", identifier="id", value=thread_id) DBconnect.update_query("UPDATE Threads SET isDeleted = %s WHERE id = %s", (status, thread_id, )) response = { "thread": thread_id } return response
def open_close_thread(id, isClosed): DBconnect.exist(entity="Threads", identifier="id", value=id) DBconnect.update_query("UPDATE Threads SET isClosed = %s WHERE id = %s", (isClosed, id, )) response = { "thread": id } return response
def vote(id, vote): DBconnect.exist(entity="Threads", identifier="id", value=id) if vote == -1: DBconnect.update_query("UPDATE Threads SET dislikes=dislikes+1, points=points-1 where id = %s", (id, )) else: DBconnect.update_query("UPDATE Threads SET likes=likes+1, points=points+1 where id = %s", (id, )) return details(id=id, related=[])
def remove_follow(email1, email2): follows = DBconnect.select_query( 'SELECT id FROM Followers WHERE follower = %s AND followee = %s', (email1, email2, ) ) if len(follows) != 0: DBconnect.update_query('DELETE FROM Followers WHERE follower = %s AND followee = %s', (email1, email2, )) else: raise Exception("No such following") return users.details(email1)
def save_forum(name, short_name, user): DBconnect.exist(entity="Users", identifier="email", value=user) forum = DBconnect.select_query( 'select id, name, short_name, user FROM Forums WHERE short_name = %s', (short_name, ) ) if len(forum) == 0: DBconnect.update_query('INSERT INTO Forums (name, short_name, user) VALUES (%s, %s, %s)', (name, short_name, user, )) forum = DBconnect.select_query( 'select id, name, short_name, user FROM Forums WHERE short_name = %s', (short_name, ) ) return forum_description(forum)
def remove_subscription(email, thread_id): DBconnect.exist(entity="Threads", identifier="id", value=thread_id) DBconnect.exist(entity="Users", identifier="email", value=email) subscription = DBconnect.select_query( 'select thread, user FROM Subscriptions WHERE user = %s AND thread = %s', (email, thread_id, ) ) if len(subscription) == 0: raise Exception("user " + email + " does not subscribe thread #" + str(thread_id)) DBconnect.update_query('DELETE FROM Subscriptions WHERE user = %s AND thread = %s', (email, thread_id, )) response = { "thread": subscription[0][0], "user": subscription[0][1] } return response
def add_follow(email1, email2): DBconnect.exist(entity="Users", identifier="email", value=email1) DBconnect.exist(entity="Users", identifier="email", value=email2) if email1 == email2: raise Exception("User with email=" + email1 + " can't follow himself") follows = DBconnect.select_query( 'SELECT id FROM Followers WHERE follower = %s AND followee = %s', (email1, email2, ) ) if len(follows) == 0: DBconnect.update_query('INSERT INTO Followers (follower, followee) VALUES (%s, %s)', (email1, email2, )) user = users.details(email1) return user
def save_subscription(email, thread_id): DBconnect.exist(entity="Threads", identifier="id", value=thread_id) DBconnect.exist(entity="Users", identifier="email", value=email) subscription = DBconnect.select_query( 'select thread, user FROM Subscriptions WHERE user = %s AND thread = %s', (email, thread_id, ) ) if len(subscription) == 0: DBconnect.update_query('INSERT INTO Subscriptions (thread, user) VALUES (%s, %s)', (thread_id, email, )) subscription = DBconnect.select_query( 'select thread, user FROM Subscriptions WHERE user = %s AND thread = %s', (email, thread_id, ) ) response = { "thread": subscription[0][0], "user": subscription[0][1] } return response
def save_user(email, username, about, name, optional): isAnonymous = 0 if "isAnonymous" in optional: isAnonymous = optional["isAnonymous"] try: user = DBconnect.select_query('select email, about, isAnonymous, id, name, username FROM Users WHERE email = %s', (email, )) if len(user) == 0: DBconnect.update_query( 'INSERT INTO Users (email, about, name, username, isAnonymous) VALUES (%s, %s, %s, %s, %s)', (email, about, name, username, isAnonymous, )) user = DBconnect.select_query('select email, about, isAnonymous, id, name, username FROM Users WHERE email = %s', (email, )) except Exception as e: raise Exception(e.message) return user_format(user)
def save_thread(forum, title, isClosed, user, date, message, slug, optional): DBconnect.exist(entity="Users", identifier="email", value=user) DBconnect.exist(entity="Forums", identifier="short_name", value=forum) isDeleted = 0 if "isDeleted" in optional: isDeleted = optional["isDeleted"] thread = DBconnect.select_query( 'select date, forum, id, isClosed, isDeleted, message, slug, title, user, dislikes, likes, points, posts ' 'FROM Threads WHERE slug = %s', (slug, ) ) if len(thread) == 0: DBconnect.update_query('INSERT INTO Threads (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 = DBconnect.select_query( 'select date, forum, id, isClosed, isDeleted, message, slug, title, user, dislikes, likes, points, posts ' 'FROM Threads 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], 'dislikes': thread[9], 'likes': thread[10], 'points': thread[11], 'posts': thread[12], } # Delete few extra elements del response["dislikes"] del response["likes"] del response["points"] del response["posts"] return response
def update_user(email, about, name): DBconnect.exist(entity="Users", identifier="email", value=email) DBconnect.update_query('UPDATE Users SET email = %s, about = %s, name = %s WHERE email = %s', (email, about, name, email, )) return details(email)
def update_thread(id, slug, message): DBconnect.exist(entity="Threads", identifier="id", value=id) DBconnect.update_query('UPDATE Threads SET slug = %s, message = %s WHERE id = %s', (slug, message, id, )) return details(id=id, related=[])
def update(update_id, message): DBconnect.exist(entity="Posts", identifier="id", value=update_id) DBconnect.update_query("UPDATE Posts SET message = %s WHERE id = %s", (message, update_id)) return details(details_id=update_id, related=[])
def remove_restore(post_id, status): DBconnect.exist(entity="Posts", identifier="id", value=post_id) DBconnect.update_query("UPDATE Posts SET isDeleted = %s WHERE Posts.id = %s", (status, post_id)) return {"post": post_id}