diff --git a/ktvmanager/lib/database.py b/ktvmanager/lib/database.py index 1e247d7..074587f 100644 --- a/ktvmanager/lib/database.py +++ b/ktvmanager/lib/database.py @@ -148,9 +148,14 @@ def add_account(user_id: int) -> Response: user_id: The ID of the user. Returns: - A Flask JSON response confirming the account was added. + A Flask JSON response confirming the account was added or an error message. """ data = request.form + + # Check if account already exists + if account_exists(user_id, data["username"], data["stream"]): + return jsonify({"error": "Account already exists for this user"}), 409 + res = single_account_check(data, get_latest_urls_from_dns()) encrypted_password = encrypt_password(data["password"]) query = "INSERT INTO userAccounts (username, stream, streamURL, expiaryDate, password, userID, maxConnections) VALUES (%s, %s, %s, %s, %s, %s, %s)" @@ -167,6 +172,23 @@ def add_account(user_id: int) -> Response: return jsonify(result) +def account_exists(user_id: int, username: str, stream: str) -> bool: + """Check if an account with the same username and stream already exists for the user. + + Args: + user_id: The ID of the user. + username: The username of the account. + stream: The stream of the account. + + Returns: + True if the account exists, False otherwise. + """ + query = "SELECT COUNT(*) as count FROM userAccounts WHERE userID = %s AND username = %s AND stream = %s" + params = (user_id, username, stream) + result = _execute_query(query, params) + return result[0]["count"] > 0 + + def update_expiry_date(username: str, stream: str, expiry_date: str) -> None: """Updates the expiry date of an account.