New User creation endpoint
This commit is contained in:
102
API/Tiri.py
102
API/Tiri.py
@@ -20,61 +20,79 @@ def get_db_connection():
|
|||||||
except mysql.connector.Error as err:
|
except mysql.connector.Error as err:
|
||||||
print(f"Error: {err}")
|
print(f"Error: {err}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# NEEDS FIXING
|
# NEEDS FIXING
|
||||||
@app.route('/api/tiropitAddUser', methods=['POST'])
|
@app.route('/api/tiropitAddUser', methods=['POST'])
|
||||||
def tiropit_add_user():
|
def tiropit_add_user():
|
||||||
data = request.form
|
data = request.json
|
||||||
picture = request.files.get('ProfilePicture')
|
|
||||||
user_id = str(uuid.uuid4())
|
|
||||||
user_name = data.get('UserName')
|
|
||||||
steam_name = data.get('SteamName')
|
|
||||||
steam_id = data.get('SteamID') # treat this as string
|
|
||||||
level = int(data.get('Level', 0))
|
|
||||||
awards = data.get('Awards', '[]')
|
|
||||||
user_games = data.get('UserGames', '[]')
|
|
||||||
warn_level = int(data.get('WarnLevel', 1))
|
|
||||||
banned = data.get('Banned')
|
|
||||||
admin_level = int(data.get('AdminLevel', 1))
|
|
||||||
user_discord = data.get('UserDiscord')
|
|
||||||
user_PIN = data.get('UserPIN')
|
|
||||||
friend_code = data.get('FirendCode')
|
|
||||||
|
|
||||||
if not picture:
|
if not data or "UserName" not in data or "UserPIN" not in data:
|
||||||
return jsonify({"error": "No profile picture provided"}), 400
|
return jsonify({"error": "Account MUST include at least UserName & UserPIN"}), 400
|
||||||
if not user_name or not steam_name or not steam_id:
|
|
||||||
return jsonify({"error": "UserName, SteamName, and SteamID are required"}), 400
|
user_pin = data.get("UserPIN")
|
||||||
|
friend_code = data.get("FriendCode")
|
||||||
|
|
||||||
if not friend_code or len(friend_code) != 5:
|
if not friend_code or len(friend_code) != 5:
|
||||||
return jsonify({"error": "FirendCode must be 5 characters long"}), 400
|
return jsonify({"error": "FirendCode must be 5 characters long"}), 400
|
||||||
if not user_PIN or len(user_PIN) != 4:
|
if not user_pin or len(user_pin) != 4:
|
||||||
return jsonify({"error": "UserPIN must be 4 characters long"}), 400
|
return jsonify({"error": "UserPIN must be 4 characters long"}), 400
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
conn = get_db_connection()
|
|
||||||
Cursor = conn.cursor()
|
connection = get_db_connection()
|
||||||
|
if connection is None:
|
||||||
|
return jsonify({"error": "Failed to contact db"}), 500
|
||||||
|
|
||||||
try:
|
try:
|
||||||
Cursor.execute("SELECT 1 FROM users WHERE FriendCode = %s", (friend_code,))
|
cursor = connection.cursor(dictionary:true)
|
||||||
if Cursor.fetchone():
|
user_data = {
|
||||||
return jsonify({"error": "FriendCode already exists"}), 409
|
|
||||||
|
"UserID" : str(uuid.uuid4()),
|
||||||
profile_pic = picture.read()
|
"UserName" : data['UserName'],
|
||||||
|
"UserPIN" : user_pin,
|
||||||
|
"SteamName" : data.get('SteamName', ''),
|
||||||
|
"Level" : data.get('Level', 0),
|
||||||
|
"Awards" : json.dumps(data.get('Awards', '[]')),
|
||||||
|
"ProfilePicture" : data.get('ProfilePicture', ''),
|
||||||
|
"UserGames" : json.dumps(data.get('UserGames', '[]')),
|
||||||
|
"WarnLevel" : data.get('WarnLevel', 0),
|
||||||
|
"Banned" : data.get('Banned', False),
|
||||||
|
"AdminLevel" : 0,
|
||||||
|
"UserDiscord" : data.get('UserDiscord', ""),
|
||||||
|
"SteamID" : data.get('SteamID', '') ,
|
||||||
|
"FriendCode" : friend_code,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Cursor.execute("""
|
|
||||||
INSERT INTO users (UserID, UserName, SteamName, Level, Awards, ProfilePicture, UserGames, WarnLevel, Banned, AdminLevel, SteamID, UserDiscord, UserPIN, FriendCode)
|
|
||||||
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
|
query = """
|
||||||
""", (
|
INSERT INTO users (UserID, UserName, SteamName, Level, Awards, UserGames, WarnLevel, Banned, AdminLevel, SteamID, UserDiscord, UserPIN, FriendCode)
|
||||||
user_id, user_name, steam_name, level, awards, profile_pic,
|
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
|
||||||
user_games, warn_level, banned, admin_level, steam_id, user_discord, user_PIN, friend_code
|
"""
|
||||||
))
|
|
||||||
conn.commit()
|
cursor.execute(query, tuple(user_data.values()))
|
||||||
except mysql.connector.Error as e:
|
connection.commit()
|
||||||
return jsonify({"error": str(e)}), 500
|
|
||||||
|
return jsonify(
|
||||||
|
{"message":"User Created",
|
||||||
|
"UserID": user_data['UserID']}, 201
|
||||||
|
)
|
||||||
|
except mysql.connector.Error as err:
|
||||||
|
connection.rollback()
|
||||||
|
if err.errno == 1062:
|
||||||
|
return jsonify({"error": "User already exists"}), 409
|
||||||
finally:
|
finally:
|
||||||
Cursor.close()
|
if connection.is_connected():
|
||||||
conn.close()
|
cursor.close()
|
||||||
|
connection.close()
|
||||||
return jsonify({"message": "User added successfully", "UserID": user_id}), 201
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
app.run(host="0.0.0.0", port=5000, debug=True)
|
||||||
|
|||||||
Reference in New Issue
Block a user