diff --git a/main.py b/main.py index eb8ab99..172b939 100644 --- a/main.py +++ b/main.py @@ -157,6 +157,7 @@ def get_addtag_request(): @socketio.on('addtag') def request_addtag(data): global finished + global message join_room(session[id]) if len(users.queue) > 0: user = users.queue[len(users.queue) - 1] @@ -174,7 +175,10 @@ def request_addtag(data): finished = None c.execute(f"SELECT * FROM users WHERE id ={user[0]}") username = c.fetchall()[0][1] - socketio.emit("finished", f"Added tag to {username}", to=session[id]) #print tagid + if message == "Tag already exists": + socketio.emit("finished", f"{message}") + else: + socketio.emit("finished", f"Added {message} to {username}", to=session[id]) #print tagid else: socketio.emit("busy", "busy", to=session[id]) else: @@ -196,6 +200,7 @@ def get_removetag_request(): @socketio.on('removetag') def request_removetag(data): global finished + global message join_room(session[id]) if len(users.queue) > 0: queue_item = users.queue[len(users.queue) - 1] @@ -215,7 +220,10 @@ def request_removetag(data): finished = None c.execute(f"SELECT * FROM users WHERE id ={user}") username = c.fetchall()[0][1] - socketio.emit("finished", f"removed tag from {username}") + if message == "Tag does not exist": + socketio.emit("finished", f"{message}") + else: + socketio.emit("finished", f"removed {message} from {username}") else: socketio.emit("busy", "busy", to=session[id]) else: @@ -226,6 +234,7 @@ def request_removetag(data): @app.route("/api/tag_id", methods=['GET']) def get_id(): global finished + global messages tag_id = int(request.args.get("id")) c.execute(f"SELECT * FROM tags WHERE tagid ={tag_id}") @@ -238,9 +247,19 @@ def get_id(): state = queue_item[1] print(state) if state == "add": - c.execute(f"INSERT OR IGNORE INTO tags (tagid, userid) VALUES ({tag_id}, {user})") + c.execute(f"SELECT * FROM tags WHERE tagid={tag_id}") + if c.fetchall() != []: + message = "Tag already exists" + else: + c.execute(f"INSERT OR IGNORE INTO tags (tagid, userid) VALUES ({tag_id}, {user})") + message = tag_id elif state == "remove": - c.execute(f"DELETE FROM tags WHERE (tagid = {tag_id} AND userid = {user}) ") + c.execute(f"SELECT * FROM tags WHERE (tagid = {tag_id} AND userid = {user})") + if c.fetchall != []: + c.execute(f"DELETE FROM tags WHERE (tagid = {tag_id} AND userid = {user}) ") + message = tag_id + else: + message = "Tag does not exist" conn.commit() finished = queue_item socketio.emit("update", "update")