Created
July 10, 2024 19:13
-
-
Save BrunoAssis/1ba70fdfd2dd0973dddddff58dcd9792 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, request, jsonify | |
from myapp import db | |
app = Flask(__name__) | |
@app.route('/user', methods=['POST']) | |
def create(): | |
data = request.json | |
username = data.get('username') | |
email = data.get('email') | |
password = data.get('password') | |
new_user = { | |
'id': gen_id(username), | |
'username': username, | |
'email': email, | |
'password': password | |
} | |
db.insert_user(new_user) | |
people = db.fetch_all_users() # Example output: [{'id': '123', 'username': 'user1', 'email': '[email protected]', 'password': 'mypassword'}, ...] | |
for user1 in people: | |
for user2 in people: | |
if user1 != user2 and user1['username'] == user2['username']: | |
db.delete_user(new_user) | |
return jsonify({"error": "Username already exists"}), 400 | |
return jsonify({"user": new_user}), 200 | |
def gen_id(u): | |
return sum([ord(c) for c in u]) | |
if __name__ == '__main__': | |
app.run(port=80) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment